1. 程式人生 > >ansible hosts檔案寫法

ansible hosts檔案寫法

1、正常寫法,name1為別名:
[test1]
name1 ansible_ssh_host=192.168.1.111 ansible_ssh_user="root" ansible_ssh_pass="1234" ansible_ssh_port=22
name2 ansible_ssh_host=192.168.1.222  ansible_ssh_user="root" ansible_ssh_pass="1234" ansible_ssh_port=22

2、連續的IP寫法,表示192.168.1.20到192.168.1.50,共31臺主機:
[test1]
name1 ansible_ssh_host=192.168.1.[20:50] ansible_ssh_user="root" ansible_ssh_pass="1234" ansible_ssh_port=22

3、帶引數的群組,vars底下為群組共同便變數,包括已定義變數和自定義變數:
[test1]
name1 ansible_ssh_host=192.168.1.[20:50]

[test1:vars]
ansible_ssh_user=root
ansible_ssh_pass="1234"
testvar="test"

4、群組整合,children底下為父群組test的子群組,呼叫方式為ansible test -m ping:
[dbtest]
name1 ansible_ssh_host=192.168.1.[20:50] ansible_ssh_user="root" ansible_ssh_pass="1234" ansible_ssh_port=22
[webtest]
name2 ansible_ssh_host=192.168.2.[20:50] ansible_ssh_user="root" ansible_ssh_pass="1234" ansible_ssh_port=22
[test:children]
dbtest
webtest

5、呼叫兩個主機組的寫法,以下webservers和dbservers都會被呼叫:
ansible webservers:dbservers -m ping

6、在webservers組中但不在dbsersers中的呼叫:
ansible webservers:!dbservers -m win_ping

7、在webservers組中並且在dbservers組中的才會呼叫:
ansible webservers:&dbservers -m ping

8、在呼叫前加~,代表正則表示式:
ansible ~(web|db).*.91it.org -m win_ping

9、組合的例子:
webserver:dbservers:&nginx:!ntp

10、hosts檔案中沒有定義的IP或別名,在進行呼叫中,會提示錯誤。ansible對單臺伺服器的呼叫,伺服器IP或域名必須有寫在hosts裡。