ansible 批量添加互信

1.生成秘钥

[root@pmm-monitor ~]# ssh-keygen 
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 identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:tWwLhxlyj4HNHWzGPtPKxK+iCILEsDvOanfC3+i0Hk0 root@118slave
The key's randomart image is:
+---[RSA 2048]----+
|         o.      |
|       + .=.     |
|.     o *=o.     |
|o.     o X*..    |
|.o     ESo*=     |
|o.    o  +o..    |
|= o  o .  ..     |
|o+ =.o= . .      |
|+o. B*.o .       |sqlserver SERIALIZABLE
+----[SHA256]-----+

一路直接回车

2、指定远程机器及密码

关掉key验证

vi /etc/ansible/ansible.cnf
host_key_checking = False

3、指定机器和密码

vi /etc/ansible/hosts

[mysql]
56.56.56.82
56.56.56.78

[mysql:vars]
ansible_ssh_pass="123456"

4、发送公钥到目标机器

ansible mysql -m copy -a "src=/root/.ssh/id_rsa.pub dest=/tmp/authorized_keys mode=600"

5、验证

[root@pmm-monitor ~]# ansible mysql -m shell -a "du -sh /data/mysql/"
56.56.56.82 | CHANGED | rc=0 >>
128G	/data/mysql/


56.56.56.78 | CHANGED | rc=0 >>
365G	/data/mysql/

你可能感兴趣的:(自动化运维)