Ansible推送ssh秘钥
一、系统环境和安装软件版本 1),系统安装# cat /etc/issue
CentOS> # uname -r
2.6.32-431.el6.x86_64
2)4台主机Ansible(192.168.0.22)另外3台主机IP分别是(192.168.0.24,192.168.0.156和192.168.0.157)
3)安装YUM源
# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm
# yuminstall ansible -y
# ansible --version
ansible 2.4.2.0
config file = /etc/ansible/ansible.cfg
configured module search path =
ansible python module location = /usr/lib/python2.6/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.6.6 (r266:84292, Aug 18 2016, 15:13:37)
二、在Ansible主机上配置
1)在推送主机上做ssh秘钥
# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your> Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
a3:08:ab:02:bf:7b:12:7d:d9:8f:9c:a9:67:38:53:a1 root@Ansible.localdomain
The key's randomart image is:
+--[ RSA 2048]----+
... +S.. .o..E.o........+ =..o . + B .o o=.* +-----------------+
2)查看生成的秘钥。
# ls -a
... > 3)在Ansible上配置hosts
#vim hosts
在内容最后加上如下内容:
192.168.0.24ansible_user=root ansible_ssh_pass="hwg123"
192.168.0.156 ansible_user=root ansible_ssh_pass="hwg123"
192.168.0.157 ansible_user=root ansible_ssh_pass="hwg123"
4)接着创建push.ssh.ymal脚本
# cat push.ssh.ymal
#Using>
[*]hosts: test remote_user: root
tasks:
[*]name: copy ssh key authorized_key:
user: root
key: "{{ lookup('file', '/root/.ssh/id_rsa.pub') }}"
5)需要修改ansible.cfg的#host_key_checking= False取消注释
# vim ansible.cfg
host_key_checking = False
三、最后运行push.ssh.ymal
# ansible-playbook push.ssh.ymal
PLAY ****
TASK *****
ok:
ok:
ok:
TASK ****
changed:
changed:
changed:
PLAY RECAP *****
192.168.0.156 : ok=2 changed=1 unreachable=0 failed=0
192.168.0.157 : ok=2 changed=1 unreachable=0 failed=0
192.168.0.24 : ok=2 changed=1 unreachable=0 failed=0
好了,Ansible主机端使用ssh root@192.168.0.24 看看需要密码不,不需要密码证明配置正确;或者使用ansible测试一下。
# ansible test -m command -a "date"
192.168.0.157 | SUCCESS | rc=0 >>
Mon Apr2 15:26:58 CST 2018
192.168.0.156 | SUCCESS | rc=0 >>
Mon Apr2 15:26:58 CST 2018
192.168.0.24 | SUCCESS | rc=0 >>
Mon Apr2 15:27:01 CST 2018
页:
[1]