简介
Inventory是ansible管理主机信息的配置文件,相当于我们系统的hosts文化的功能,默认存放在/etc/ansible/hosts。为了批量管理主机,便捷使用其中的主机分组,ansible通过Inventory来定义其主机和组,在使用时通过-i 或--inventoty-file指定读取,与ansible命令结合使用如下:
ansible -i /etc/ansible/hosts webs -m ping
如果只有一个Inventory时可以不指定路径,默认读取/etc/ansible/hosts。
注意:
前面我们说过在后期的ansible使用的过程中,我们建议针对每个项目创建一个ansible执行目录,同时针对项目使用ansible.cfg配置文件和hosts管理文件;因此我们只需要在对应项目的ansible.cfg文件中指定相应的hosts即可。
Inventory定义规则
1. 定义主机和组
Inventory配置文件遵循INI文件风格,中括号中的字符串为组名。其支持将同一个主机同时归并到多个不同的组中。此外若目标主机使用了非默认的ssh端口,还可以在主机名称之后使用冒号加端口号来标明,以行为单位分隔配置。示例如下:
# ‘#’开头表示该行为注释行,即当行不生效。
(1)Inventory可以直接为ip
10.10.10.134
(2)Inventory同样支持Hostname的方式,后面跟冒号加数字表示端口号,默认为22
client.xhz.com:2222
(3)中括号内的内容表示一个分组的开始,紧随其后的主机均属于该组成员,空行后的主机也属于改组。如:
[webservers]
web1.xhz.com
web[10:20].xhz.com #[10:20]表示10~20之间的数字(包括10和20),即web10.xhz.com,web11.xhz.com ....web20.xhz.com的所有主机。
web2.xhz.com
[dbservers]
db-a.xhz.com
db-[b:f].xhz.com #[b:f]表示b~f之间的字母,包括b和f
2. 定义主机变量
在日常工作中,通常会遇到非标准化的需求配置,由于考虑到安全性问题,通常将企业内部的web服务器80端口修改为其他端口,而该功能可以直接通过修改Inventory配置来实现,在定义主机的时候为其添加主机变量,以便在playbook中使用。
[webserver]
web1.xhz.com http_port=808 maxRequestPerChild=801 #定义http_port的端口为808,配置maxRequestPerChild为801
Ansible其实支持多种方式修改或自定义变量,Inventory是其中的一种方式,后面我们还会介绍其他方式定义变量。
3. 定义组变量
Ansible支持定义组变量,主要针对大量机器的变量定义需求,赋予指定组内所有主机在Playbook中可用的变量,等同于逐一给改组下的所有主机赋予同一变量,如:
[groupservers]
web1.xhz.com
web2.xhz.com
[groupservers:vars]
ntp_server=ntp.xhz.com ##定义groupservers组中所有主机ntp_server值为ntp.xhz.com
nfs_server=nfs.xhz.com ##定义groupservers组中所有主机nfs_server值为nfs.xhz.com
4. 定义组嵌套
Inventory中,组还可以包含其他的组(嵌套),并且可用向指定组中的主机指定变量,不过,这些变量只能在ansible-playbook中使用,按ansible ad-hoc不支持,组与组间可用相互调用,并且可用向组中的主机指定变量。
[apache]
httpd1.xhz.com
http2.xhz.com
[nginx]
nginx1.xhz.com
nginx2.xhz.com
[webservers:children]
apache
nginx
[webserver:vars]
ntp_server=ntp.xhz.com
5. 多重变量定义
变量除了可以在Inventory中一并定义,也可以独立于Inventory文件之外单独存储到YAML格式的配置文件中,这些文件通常以.yaml,.yml,.json为后缀或者无后缀,变量通常在如下4个位置进行检索(遵循从事到下的顺序)。
· Inventory配置文件(默认为/etc/ansible/hosts,在ansible.cfg中可定义)
· playbook中的var区域
· Roles中的var目录下的文件
` Roles同级目录下group_vars和hosts_vars目录下的文件
假如foosball主机属于raleigh和webservers组,那么变量在如下文件中设置均有效:
/etc/ansible/group_vars/raleigh
/etc/ansible/group_vars/webservers
/etc/ansible/hosts_vars/foosball
6. Inventory其他参数列表
除了支持如上的功能外,Ansible基于ssh连接Inventory中指定的远程主机时,还内置了很多其他参数,用于指定其交互方式,如:
ansible_ssh_host: 指定连接主机
ansible_ssh_port: 指定ssh连接端口
ansible_ssh_user: 指定ssh连接用户
ansible_ssh_pass: 指定ssh连接密码
ansible_sudo_pass: 指定ssh连接时sudo密码
ansible_ssh_private_key_file: 指定特有私钥文件
这些参数可以直接卸载命令行或者playbook文件中,以覆盖配置文件中的定义。
Ansible主机匹配规则
正则表达式是各类高级语言的必定支持方法之一,ansible也不例外。其正则功能等同于正则表达式,语法使用也一样。ansible ad-hoc使用正则的语法如下:
ansible <正则匹配主机> -m <module_name> -a <argument>
该功能主要针对Inventory的主机列表使用,在以下案例中主要针对webservers进行正则匹配:
# 重启webservers组中的所有主机的httpd服务
ansible webserver -m service -a "name=httpd state=restarted"
1. ALL(全量)匹配
匹配所有的主机,all 或者 * 号功能相同。
## all和*功能相同,但是*好需要引起来
[root@clinet test1]# ansible all -m ping
[root@clinet test1]# ansible '*' -m ping
## 检查hosts中10.10.10.0/24网段的所有主机
[root@clinet test1]# ansible 10.10.10.* -m ping
2. 逻辑或(or)匹配
同时对多台或多个组同时指向,相互之间用‘:’(冒号)分隔即可。
[root@clinet test1]# cat hosts
[web]
10.10.10.[134:136] ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='123'
[db]
10.10.10.[137:138] ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='123'
[root@clinet test1]#
[root@clinet test1]# ansible 'web:db' -m ping
3. 逻辑非(!)匹配
逻辑非用感叹号(!)表示,主要针对多重条件的匹配规则,使用如下:
[root@clinet test1]# cat hosts
[web]
10.10.10.[134:136] ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='123'
#10.10.10.135 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='123'
[db]
10.10.10.[136:138] ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='123'
[root@clinet test1]#
## 在web组中,但是不在db组的多有主机,因此要过滤掉136
[root@clinet test1]# ansible 'web:!db' -m ping
10.10.10.134 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
10.10.10.135 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
[root@clinet test1]#
4. 逻辑与(&)匹配
逻辑与用&表示,主要针对多重条件的匹配规则,示例如下:
[root@clinet test1]# cat hosts
[web]
10.10.10.[134:136] ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='123'
#10.10.10.135 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='123'
[db]
10.10.10.[136:138] ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='123'
[root@clinet test1]#
## 在web组中同时也在db组中的所有主机,因此只匹配了136
[root@clinet test1]# ansible 'web:&db' -m ping
10.10.10.136 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
5. 多条件匹配
ansible支持多条件匹配,就是将上述几种匹配方式联合起来,了解即可。
## web组合db组中的所有主机,然后再和clinet组取都有的主机,然后再和lvs组过滤掉都有的主机。
##例如:
[web]
10.10.10.133
10.10.10.134
[db]
10.10.10.135
10.10.10.136
[clinet]
10.10.10.133
10.10.10.136
10.10.10.137
[lvs]
10.10.10.133
10.10.10.138
匹配:
web:db ==> 10.10.10.{133,134,135,136}
web:db:&clinet ==>10.10.10.{133,136}
web:db:&clinet:!lvs ==> 10.10.10.{136}
[root@clinet test1]# ansible 'web:db:&clinet:!lvs' -m ping
10.10.10.136 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
6. 模糊匹配
*通配符在ansible表示0个或多个任意字符,主要应用于模糊匹配。
匹配所有10.10.10.0段的主机
10.10.10.*
[root@clinet test1]# ansible 10.10.10.* -m ping
10.10.10.135 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
10.10.10.136 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
10.10.10.137 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
1*.136
[root@clinet test1]# ansible 1*.136 -m ping
10.10.10.136 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
[root@clinet test1]#
7. 域切割
ansible底层基于Python,因此支持切割,示例如下:
[root@clinet test1]# cat hosts
[web]
10.10.10.133
10.10.10.134
[root@clinet test1]#
##
web[0]=10.10.10.133
web[1]=10.10.1.134
[root@clinet test1]# ansible web[1] -m ping
10.10.10.134 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
[root@clinet test1]#
8. 正则匹配
ansible同样完整支持正则匹配功能,‘~’开始表示正则匹配。
##检测beta.example.com,web.example.com,green..example.com,beta..example.org,web..example.org,green..example.org的存活。
ansible "~(beta|web|green)\.example\.(cpm|oorg)" -m ping
注意:
--list-host 可以查看到匹配的主机。例:
anisble web --list-host 查看web组中匹配到的主机。