【问题标题】:Specifying ssh key in ansible playbook file在 ansible playbook 文件中指定 ssh 密钥
【发布时间】:2017-11-27 18:52:01
【问题描述】:

Ansible playbook 可以在命令行使用--key-file 指定用于 ssh 连接的密钥。

ansible-playbook -i hosts playbook.yml --key-file "~/.ssh/mykey.pem"

是否可以在 playbook 文件中指定此键的位置,而不是在命令行中使用 --key-file

因为我想将此键的位置写入var.yaml 文件,该文件将由带有vars_files: 的ansible playbook 读取。

以下是我的部分配置:

vars.yml 文件

key1: ~/.ssh/mykey1.pem
key2: ~/.ssh/mykey2.pem

playbook.yml 文件

---

- hosts: myHost
  remote_user: ubuntu
  key_file: {{ key1 }}  # This is not a valid syntax in ansible. Does there exist this kind of directive which allows me to specify the ssh key used for this connection?
  vars_files:
    - vars.yml
  tasks:
    - name: Echo a hello message
      command: echo hello

我尝试在vars 下添加ansible_ssh_private_key_file。但它在我的机器上不起作用。

vars_files:
  - vars.yml
vars:
  ansible_ssh_private_key_file: "{{ key1 }}"
tasks:
  - name: Echo a hello message
    command: echo hello

如果我用上面的playbook.yml 运行ansible-playbook。我收到以下错误:

TASK [Gathering Facts] ******************************************************************************************************************************
Using module file /usr/local/lib/python2.7/site-packages/ansible/modules/system/setup.py
<192.168.5.100> ESTABLISH SSH CONNECTION FOR USER: ubuntu
<192.168.5.100> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ubuntu -o ConnectTimeout=10 -o ControlPath=/Users/myName/.ansible/cp/2d18691789 192.168.5.100 '/bin/sh -c '"'"'echo ~ && sleep 0'"'"''
<192.168.5.100> (255, '', 'Permission denied (publickey).\r\n')
fatal: [192.168.5.100]: UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey).\r\n",
    "unreachable": true
}
    to retry, use: --limit @/Users/myName/playbook.retry

我在 ssh 命令中找不到我的密钥文件的名称。很奇怪。

【问题讨论】:

  • 我认为--private-key=~/.ssh/keys/id_rsa 会起作用。
  • @zx1986 --private-key key_file_path 也为我工作。

标签: ansible


【解决方案1】:

您要查找的变量名称是ansible_ssh_private_key_file

您应该将其设置为 'vars' 级别:

  • 在库存文件中:

    myHost ansible_ssh_private_key_file=~/.ssh/mykey1.pem
    myOtherHost ansible_ssh_private_key_file=~/.ssh/mykey2.pem
    
  • host_vars:

    # host_vars/myHost.yml
    ansible_ssh_private_key_file: ~/.ssh/mykey1.pem
    
    # host_vars/myOtherHost.yml
    ansible_ssh_private_key_file: ~/.ssh/mykey2.pem
    
  • 如果您对一组主机使用相同的密钥,则在 group_vars 文件中

  • 在剧中条目的vars 部分:

    - hosts: myHost
       remote_user: ubuntu
       vars_files:
         - vars.yml
       vars:
         ansible_ssh_private_key_file: "{{ key1 }}"
       tasks:
         - name: Echo a hello message
           command: echo hello
    
  • 在播放条目(任务)中设置事实

    - name: 'you name it'
       ansible.builtin.set_fact:
         ansible_ssh_private_key_file: "{{ key1 }}"
    

Inventory documentation

【讨论】:

  • vars 下写入ansible_ssh_private_key_file 在我的机器上不起作用。很奇怪。
  • 我不想在清单中指定密钥。因为我无法从库存文件中加载vars.yml
  • 确切:来自vars 我认为为时已晚。如果您不想将其放入库存中,则应使用 host_varsgroup_vars 文件。
  • 看到将私钥保存在保险库中的最佳实践会很有趣,因此可以将私钥推入 git repo 而不会感到那么糟糕。
  • @fabiog 你说得对,我只是用 Ansible 2.9 到 1.8 对其进行了测试,所有(主要)版本都使用 vars: 部分中定义的私钥。
【解决方案2】:

您可以使用 ansible.cfg 文件,它应该如下所示(您可能还需要包含其他参数):

[defaults]
inventory = <PATH TO INVENTORY FILE>
remote_user = <YOUR USER>
private_key_file =  <PATH TO KEY_FILE>

希望这可以节省您的打字时间

【讨论】:

  • 对于具有项目特定配置和密钥的愚蠢实验室环境,这是迄今为止最好的解决方案。
【解决方案3】:

如果您使用ansible-playbook -vvv 运行您的剧本,您将看到正在运行的实际命令,因此您可以检查密钥是否实际包含在 ssh 命令中(您可能会发现问题出在错误的用户名而不是比丢失的键)。

我同意 Brian 的上述评论(以及 zigam 的编辑),即 vars 部分为时已晚。我还测试了像这样在主机的动态定义中包含密钥

# fails
- name: Add all instance public IPs to host group
  add_host: hostname={{ item.public_ip }} groups=ec2hosts ansible_ssh_private_key_file=~/.aws/dev_staging.pem
  loop: "{{ ec2.instances }}"

但这也失败了。

所以这不是一个答案。只是一些调试帮助和不要尝试的事情。

【讨论】:

    猜你喜欢
    • 2020-01-15
    • 2018-11-14
    • 2016-12-04
    • 1970-01-01
    • 2017-02-27
    • 2016-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多