1. 程式人生 > >ansible免手工輸入yes和快速部署公鑰

ansible免手工輸入yes和快速部署公鑰

usr 測試 AR 驗證 需要 自己的 check str 密碼

新搭的機器,達到百以上級別的機器,怎麽實現批量化管理呢?第一步當然快速部署公鑰,實現免密碼登陸

演示一下比較煩的情況:
ssh 127.0.0.1得輸入yes,然後再輸入密碼才能登錄
cat .ssh/known_hosts

自動化的部分:
Are you sure you want to continue connecting (yes/no)? yes
[email protected]‘s password:

免手工輸入yes

/etc/ansible/hosts,ansible配置文件:

128.127.0.0.1
172.16.0.3
192.168.1.106

/etc/ssh/ssh_config配置文件添加:

StrictHostKeyChecking no

運行:

ansible all -m ping

配置文件修改回原來的:

StrictHostKeyChecking ask

驗證:

cat .ssh/known_hosts
ssh 172.16.0.3

使用密碼批量操作機器(一開始機器無公鑰)

$ cat host

[nginx]
nginx_127 ansible_ssh_port=22 ansible_ssh_host=127.0.0.1 ansible_ssh_pass=123456 host_key_checking=false ansible_sudo_pass=‘123456‘

[mysql]
mysql_172 ansible_ssh_port=22 ansible_ssh_host=172.16.0.3 ansible_ssh_pass=123456 host_key_checking=false ansible_sudo_pass=‘123456‘

測試:

ansible -i host all -m shell -a "pwd" --user user1

批量添加公鑰

ansible all -m script -a "/usr/local/src/script"

chmod +x /usr/local/src/script
/usr/local/src/script(可寫)
#!/bin/sh
mkdir /root/.ssh
chmod 700 /root/.ssh
echo ‘公鑰‘ >>/root/.ssh/authorized_keys    #這一行的話需要改,改成自己的公鑰(就是.pub文件)
chmod 600 /root/.ssh/authorized_keys

補充:如果是用普通用戶來管理的,需要批量創建用戶和添加sudo 權限

配置文件去除密碼

[nginx]
nginx_127 ansible_ssh_port=22 ansible_ssh_host=127.0.0.1 ansible_sudo_pass=‘123456‘

[mysql]
mysql_172 ansible_ssh_port=22 ansible_ssh_host=172.16.0.3 ansible_sudo_pass=‘123456‘

驗證:

ansible -i hosts  all -m shell  -a ‘pwd‘ --user djidba --private-key=/home/user1/.ssh/id_rsa

ansible免手工輸入yes和快速部署公鑰