关于Inventory文件的写法,共有两种方式:
1. ini方式
2. yaml方式
一、 一个简单的Inventory文件
(ansible) [root@Master initNodes]# cat /opt/ansible/hosts
[master]
main.plus7s.com
[node]
n1.plus7s.com
n2.plus7s.com
n3.plus7s.com
二、 Inventory文件中多个连续主机的缩写方法:
如之前的Inventory文件,可以写成如下格式:
(ansible) [root@Master initNodes]# cat /tmp/hosts
[master]
main.plus7s.com
[node]
n[1:3].plus7s.com
(ansible) [root@Master ~]# ansible -i /tmp/hosts node -m ping
n1.plus7s.com | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
n2.plus7s.com | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
n3.plus7s.com | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
(ansible) [root@Master ~]#
三、此外,Inventory文件也可以通过yaml的形式来表达:
几个要点:
1. 最上面的”all:“不可省
2. 每一个分组,都需要用"children:"标签进行标示,并在下一锁进使用hosts: 标示列出所有主机名或IP
3. 每一个主机名或IP后必须要有“:”
4. 主机名或IP的下一级缩进中可以增加该主机的主机属性,比如端口、地址等……
(ansible) [root@Master ansible]# cat hosts.yaml
all:
hosts:
children:
master:
hosts:
10.1.1.1:
nodes:
hosts:
n1.plus7s.com:
n2.plus7s.com:
n3.plus7s.com:
db:
hosts:
dbmix1.plus7s.com:
ansible_hosts: 1.1.1.1
ansible_port: 5307
(ansible) [root@Master ansible]#