Ansible剧本可以--key-file在命令行上使用来指定用于ssh连接的密钥。
ansible-playbook -i hosts playbook.yml --key-file "~/.ssh/mykey.pem"
是否可以指定此键在剧本文件中的位置,而不是--key-file在命令行上使用?
因为我想将此密钥的位置写入一个var.yaml文件,该文件将由ansible剧本使用读取vars_files:。
以下是我的配置的一部分:
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
我试过ansible_ssh_private_key_file在下添加vars。但这在我的机器上不起作用。
vars_files:
  - vars.yml
vars:
  ansible_ssh_private_key_file: "{{ key1 }}"
tasks:
  - name: Echo a hello message
    command: echo hello
如果我ansible-playbook用playbook.yml上面的方法运行。我收到以下错误:
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可以。