环境192.168.1.100 部署ansible 192.168.1.101 、192.168.1.02 测试使用
安装
yum install ansible
测试
echo "127.0.0.1">>/etc/ansible/hosts
ansible all -m shell -a "uname -a "
输出
127.0.0.1 | CHANGED | rc=0 >>
Linux localhost.localdomain 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
生成密钥
ssh-keygen -t rsa
将生成的密钥放入测试服务器上(192.168.1.101/192.168.1.102),将/etc/ansible/hosts 中的127.0.0.1 清掉,加入192.168.1.101/102机器
# 1、vim 将公钥放在这个文件中
~/.ssh/authorized_keys
#2、使用 ssh-copy
ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.1.101
cat >>/etc/ansible/hosts<<EOF
> [test]
> 192.168.1.101
> 192.168.1.102
EOF
copy到测试服务器
ansible test -m copy -a "src=~/.ssh/id_rsa.pub dest=/tmp/id_rsa.pub"
输出
SSH password:
192.168.1.102 | CHANGED => {
"changed": true,
"checksum": "a5b73d42a98b91fea3f34985ee70c8a3b951902c",
"dest": "/tmp/id_rsa.pub",
"gid": 0,
"group": "root",
"md5sum": "cd5775138e4f1027f853939d96cbfb01",
"mode": "0644",
"owner": "root",
"secontext": "unconfined_u:object_r:admin_home_t:s0",
"size": 408,
"src": "/root/.ansible/tmp/ansible-tmp-1556036868.31-196408653419037/source",
"state": "file",
"uid": 0
}
192.168.1.101 | CHANGED => {
"changed": true,
"checksum": "a5b73d42a98b91fea3f34985ee70c8a3b951902c",
"dest": "/tmp/id_rsa.pub",
"gid": 0,
"group": "root",
"md5sum": "cd5775138e4f1027f853939d96cbfb01",
"mode": "0644",
"owner": "root",
"secontext": "unconfined_u:object_r:admin_home_t:s0",
"size": 408,
"src": "/root/.ansible/tmp/ansible-tmp-1556036868.3-192517345649257/source",
"state": "file",
"uid": 0
}
将密钥放入root目录下
ansible test -m shell -a "cat /tmp/id_rsa.pub >> /root/.ssh/authorized_keys" --ask-pass -c paramiko -u root
如果root目录下没有.ssh目录先创建一个
ansible test -m shell -a "mkdir /root/.ssh" --ask-pass -c paramiko -u root
测试是否还需要密码
ansible test -m shell -a "uname -a"
输出
192.168.1.102 | CHANGED | rc=0 >>
Linux localhost.localdomain 3.10.0-957.10.1.el7.x86_64 #1 SMP Mon Mar 18 15:06:45 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
192.168.1.101 | CHANGED | rc=0 >>
Linux localhost.localdomain 3.10.0-957.10.1.el7.x86_64 #1 SMP Mon Mar 18 15:06:45 UTC