【发布时间】: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