1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| 1.基于秘钥方式管理客户端 root@ansible ~]# ssh-keygen enerating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): /root/.ssh/id_rsa already exists. Overwrite (y/n)? y Enter passphrase (empty for no passphrase): Enter same passphrase again:
2.将公钥拷贝到目标主机 [root@ansible:~]#ssh-copy-id 10.0.0.41 [root@ansible:~]#ssh-copy-id 10.0.0.42 [root@ansible:~]#ssh-copy-id 10.0.0.43 登录测试:是否成功 [root@ansible:~]#ssh 10.0.0.41
3.配置主机清单 [root@ansible:~]#cat /etc/ansible/hosts 10.0.0.41 10.0.0.42 10.0.0.43 测试 [root@ansible ~]# ansible 10.0.0.41 -m ping 10.0.0.41 | SUCCESS => { "changed": false, "ping": "pong" } [root@ansible ~]# ansible 10.0.0.42 -m ping 10.0.0.42 | SUCCESS => { "changed": false, "ping": "pong" } [root@ansible ~]# ansible 10.0.0.43 -m ping 10.0.0.43 | SUCCESS => { "changed": false, "ping": "pong" }
[root@ansible:~]#cat /etc/ansible/hosts root@ansible ~]# cat /etc/ansible/hosts backup ansible_ssh_host=10.0.0.41
[webs] web01 ansible_ssh_host=10.0.0.41 web02 ansible_ssh_host=10.0.0.42 web03 ansible_ssh_host=10.0.0.43
[root@ansible:~]#ansible web02 -m ping web01 | SUCCESS => { "changed": false, "ping": "pong" } [root@ansible:~]#ansible web03 -m ping web02 | SUCCESS => { "changed": false, "ping": "pong" } [root@ansible:~]#ansible webs -m ping web01 | SUCCESS => { "changed": false, "ping": "pong" } web02 | SUCCESS => { "changed": false, "ping": "pong" }
配置多个组属于一个组(lnmp组包含了back组和webs组) [root@ansible:~]#cat /etc/ansible/hosts root@ansible ~]# cat /etc/ansible/hosts [back] backup ansible_ssh_host=10.0.0.41
[webs] web01 ansible_ssh_host=10.0.0.41 web02 ansible_ssh_host=10.0.0.42 web03 ansible_ssh_host=10.0.0.43
[lnmp:children] back webs
|