【ansible】playbook 主機變量2


ansible-playbook 配置 hosts 后可以指定變量,通過-k 可以交互輸入密碼,也可以將密碼寫在 hosts 文件中。

入口 yaml 文件中通過 {{ ** }} 獲取變量,命令行通過 -i 指定 hosts 文件, -e 傳入參數,如果同時傳入多個 host 參數可使用逗號分隔,同時也可以使用 hosts 文件中的變量 ,其中 remote_user: root 可以在 ansiplay-book 命令行中使用 -u root 替代。

[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 原理)

[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
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM