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 win_ping

6、在webservers组中但不在dbsersers中的调用:
ansible webservers:!dbservers -m win_ping

7、在webservers组中并且在dbservers组中的才会调用:
ansible webservers:&dbservers -m win_ping

8、在调用前加~,代表正则表达式:
ansible ~(web|db).*.91it.org -m win_ping

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

10、hosts文件中没有定义的IP或别名,在进行调用中,会提示错误。ansible对单台服务器的调用,服务器IP或域名必须有写在hosts里。

相关内容