ansible-playbook 配置 hosts 後能夠指定變量,經過-k 能夠交互輸入密碼,也能夠將密碼寫在 hosts 文件中。web
入口 yaml 文件中經過 {{ ** }} 獲取變量,命令行經過 -i 指定 hosts 文件, -e 傳入參數,若是同時傳入多個 host 參數可以使用逗號分隔,同時也能夠使用 hosts 文件中的變量 ,其中 remote_user: root 能夠在 ansiplay-book 命令行中使用 -u root 替代。shell
[root@10_1_162_39 host_vars]# ll total 16 -rw-r--r-- 1 root root 236 May 5 09:25 hosts
-rw-r--r-- 1 root root 152 May 5 09:21 test1.yaml -rw-r--r-- 1 root root 146 May 5 09:20 test.playbook [root@10_1_162_39 host_vars]# cat hosts [web] 10.1.167.36 [web:vars] ansible_ssh_port=32200 [web1] 10.1.162.18 [web1:vars] ansible_ssh_port=322 [root@10_1_162_39 host_vars]# cat test1.yaml --- - hosts: "{{ host }}" gather_facts: false remote_user: root tasks: - shell: uptime register: output - debug: var=output.stdout
[root@10_1_162_39 host_vars]# ansible-playbook test1.yaml -i hosts -e host=10.1.162.18 -k SSH password: PLAY [10.1.162.18] ************************************************************* TASK [command] ***************************************************************** changed: [10.1.162.18] TASK [debug] ******************************************************************* ok: [10.1.162.18] => { "output.stdout": " 09:26:36 up 18:05, 7 users, load average: 0.05, 0.10, 0.08" } PLAY RECAP ********************************************************************* 10.1.162.18 : ok=2 changed=1 unreachable=0 failed=0 [root@10_1_162_39 host_vars]#
配置密鑰後還須要指定 host 端口,因爲只配了一個 ip 的密鑰,則另外 ip 報錯,能夠經過 -k 輸入另外 ip 的密碼便可。(這裏的原理應該是 兩個 ip 都會驗證這個密碼,其中一個 ip 密碼驗證經過,另一個 ip 驗證不經過則會判斷是否已經打通公鑰認證。固然順序可能相反,沒有看源碼。並且 ansible 自己會緩存密碼,緩存時間比較短,應該是幾分鐘內,這段時間內即便輸錯密碼也能夠登錄,這裏應該是相似 session 原理)api
[root@10_1_162_39 host_vars]# cat hosts [web] 10.1.162.18:322 10.1.167.36:32200 [root@10_1_162_39 host_vars]# ssh-copy-id -i ~/.ssh/id_rsa.pub "-p 322 root@10.1.162.18 " root@10.1.162.18's password: Now try logging into the machine, with "ssh '-p 322 root@10.1.162.18 '", and check in: .ssh/authorized_keys to make sure we haven't added extra keys that you weren't expecting. [root@10_1_162_39 host_vars]# ansible web -m shell -a ifconfig -i hosts 10.1.162.18 | SUCCESS | rc=0 >> eth0 Link encap:Ethernet HWaddr 00:0C:29:21:BD:17 inet addr:192.168.238.129 Bcast:192.168.238.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe21:bd17/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:301306 errors:0 dropped:0 overruns:0 frame:0 TX packets:121768 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:364185541 (347.3 MiB) TX bytes:6774878 (6.4 MiB) Interrupt:19 Base address:0x2000 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:8 errors:0 dropped:0 overruns:0 frame:0 TX packets:8 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:480 (480.0 b) TX bytes:480 (480.0 b) 10.1.167.36 | UNREACHABLE! => { "changed": false, "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).\r\n", "unreachable": true }