Ansible playbook可以在命令行上使用--key-file
指定用于ssh连接的密钥。
ansible-playbook -i hosts init_system.yml --key-file "/home/ops/.ssh/id_rsa"
ansible-playbook -i hosts playbook.yml --key-file "~/.ssh/mykey.pem"
permissions 0755 for '/etc/ssh/ssh_host_ecdsa_key' are too open
755 is too large as that makes it non-writable by your own user.
600 is actually recommended as it allows owner read-write not just read
chmod 600 ~/.ssh/id_rsa
是否可以在playbook文件中指定此键的位置,而不是在命令行上使用--key-file
?
因为我想将这个键的位置写入var.yaml
文件,该文件将由vars_files:
playbook用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
我试过在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
您要查找的变量名是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
:# hosts_vars/myHost.yml ansible_ssh_private_key_file: ~/.ssh/mykey1.pem # hosts_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
您可以使用ansible.cfg文件,它应该如下所示(您可能希望包含其他参数):
[defaults]
inventory = <PATH TO INVENTORY FILE>
remote_user = <YOUR USER>
private_key_file = <PATH TO KEY_FILE>
希望这可以为您节省一些打字
如果您使用ansible-playbook -vvv
运行您的playbook,您将看到正在运行的实际命令,因此您可以检查该键是否实际包含在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 }}"
ansible配置private key来免密码登录
[defaults]
inventory = ./conf/hosts
pull_interval = 15
sudo_user = root
transport = paramiko
module_lang = C
host_key_checking = False
sudo_exe = sudo
# SSH timeout
timeout = 30
#remote_user = root
#remote_port = 22
remote_tmp = $HOME/.ansible/tmp
[ssh_connection]
# if True, make ansible use scp if the connection type is ssh, default is sftp
scp_if_ssh = True
#sftp_batch_mode= False
control_path = ./ssh_keys
去服务器B上生成密钥对,
#ssh root@服务器B
#ssh-keygen 如果要重命名可以自己指定, 回车后生成密钥对
# echo ~/.ssh/id_rsa.pub >> known_hosts 这里把生成的公钥放入known_hosts
如果自己配置了ssh_config, 关闭了known_hosts, 可能就需要写进~/.authorized_keys里面去了.
把服务器B上的私钥回传到ansible执行的服务器A的ssh_keys里, 命名为
服务器B_ip_.key
给ssh_keys文件夹授权为700, 一定要700, 其它的都会报错.
建立hosts文件里面指定server, 每个server一行.这里我测试就写一行.
[test_server]
10.0.1.5 ansible_ssh_private_key_file=ssh_keys/10.0.1.5.key ansible_ssh_user=root
现在回到服务器A的这个ansible文件夹下测试:
ansible test_server -m shell -a ‘ls -la /etc/hostname’
10.0.1.5 | success | rc=0 >>
-rw-r--r-- 1 root root 15 Jun 4 2012 /etc/hostname
无密码成功.