概念
An inventory is a list of managed nodes, or hosts, that Ansible deploys and configures.
清单是受控的节点、主机。
但通常我们不会去修改这个配置项,如果在其它地方定义了inventory文件,可以直接在ansible的命令行中使用-i选项去指定我们自定义的inventory文件。
$ ansible -i /tmp/myinv.ini ...
$ ansible-playbook -i /tmp/myinv.ini ...
配置文件中的清单
ansible 的配置文件为/etc/ansible/ansible.cfg,ansible 有许多参数,下面我们列出一些常见的参数:
inventory = /etc/ansible/hosts #这个参数表示资源清单inventory文件的位置
library = /usr/share/ansible #指向存放Ansible模块的目录,支持多个目录方式,只要用冒号(:)隔开就可以
forks = 5 #并发连接数,默认为5
sudo_user = root #设置默认执行命令的用户
remote_port = 22 #指定连接被管节点的管理端口,默认为22端口,建议修改,能够更加安全
host_key_checking = False #设置是否检查SSH主机的密钥,值为True/False。关闭后第一次连接不会提示配置实例
timeout = 60 #设置SSH连接的超时时间,单位为秒
log_path = /var/log/ansible.log #指定一个存储ansible日志的文件(默认不记录日志)
创建清单
1、 直接指明主机地址或主机名:
## green.example.com#
# blue.example.com#
# 192.168.100.1
# 192.168.100.10
2、 定义一个主机组[组名]把地址或主机名加进去
[mysql_test]
192.168.253.159
192.168.253.160
192.168.253.153
清单的格式
You can create your inventory file in one of many formats, depending on the inventory plugins you have. The most common formats are INI and YAML.
清单的格式可以是INI和YAML
A basic INI /etc/ansible/hosts might look like this:
mail.example.com
[webservers]
foo.example.com
bar.example.com
[dbservers]
one.example.com
two.example.com
three.example.com
Here’s that same basic inventory file in YAML format:
ungrouped:
hosts:
mail.example.com:
webservers:
hosts:
foo.example.com:
bar.example.com:
dbservers:
hosts:
one.example.com:
two.example.com:
three.example.com:
默认的组
Even if you do not define any groups in your inventory file, Ansible creates two default groups: all and ungrouped. The all group contains every host. The ungrouped group contains all hosts that don’t have another group aside from all. Every host will always belong to at least 2 groups (all and ungrouped or all and some other group). For example, in the basic inventory above, the host mail.example.com belongs to the all group and the ungrouped group; the host two.example.com belongs to the all group and the dbservers group. Though all and ungrouped are always present, they can be implicit and not appear in group listings like group_names。
即使你没定义任何组在清单文件中,ansible 也会创建2个默认的组all、ungrouped。
Ansible默认预定义了两个主机组:all分组和ungrouped分组。
(1).all分组中包含所有分组内的节点
(2).ungrouped分组包含所有不在分组内的节点
(3).这两个分组都不包含localhost这个特殊的节点
ALL 组包含所有的主机。未定组ungrouped 包含除all 组中已经定义的主机,每台主机至少属于2个组(all和ungrouped) .例如上述的mail.example.com 属于all组和ungrouped 组。two.example.com 属于all组和dhservers组。
一台主机属于多个组
ungrouped:
hosts:
mail.example.com:
webservers:
hosts:
foo.example.com:
bar.example.com:
dbservers:
hosts:
one.example.com:
two.example.com:
three.example.com:
east:
hosts:
foo.example.com:
one.example.com:
two.example.com:
west:
hosts:
bar.example.com:
three.example.com:
prod:
hosts:
foo.example.com:
one.example.com:
two.example.com:
test:
hosts:
bar.example.com:
three.example.com:
组的父子关系
ungrouped:
hosts:
mail.example.com:
webservers:
hosts:
foo.example.com:
bar.example.com:
dbservers:
hosts:
one.example.com:
two.example.com:
three.example.com:
east:
hosts:
foo.example.com:
one.example.com:
two.example.com:
west:
hosts:
bar.example.com:
three.example.com:
prod:
children:
east:
test:
children:
west:
定义主机组变量
有了主机组之后,可以直接为主机组定义变量,这样组内的所有主机都具有该变量。
[nginx]
192.168.200.27
192.168.200.28 ansible_password=123456
192.168.200.29
[nginx:vars]
ansible_password='123456'
[all:vars]
ansible_port=22
[ungrouped:vars]
ansible_port=22
上面[nginx:vars]表示为nginx组内所有主机定义变量ansible_password='123456'。而[all:vars]和[ungrouped:vars]分别表示为all和ungrouped这两个特殊的主机组内的所有主机定义变量。
组的嵌套
Inventory还支持主机组的分组嵌套,可以通过[GROUP:children]的方式定义一个主机组,并在其中包含子组。
例如:
[nginx]
192.168.200.27
192.168.200.28
192.168.200.29
[apache]
192.168.200.3[0:3]
[webservers:children]
nginx
apache
其中webservers主机组中包含了nginx组合apache组内的所有主机。
清单中主机名称的解析
例如,在默认的inventory文件/etc/ansible/hosts添加几个目标主机:
node1
node2 ansible_host=192.168.200.28
192.168.200.31
192.168.200.32:22
192.168.200.3[2:3] ansible_port=22
上面的inventory配置中:
(1).第一行通过主机名定义,在ansible连接该节点时会进行主机名DNS解析
(2).第二行也是通过主机名定义,但是使用了一个主机变量ansible_host=IP,此时Ansible去连接该主机时将直接通过IP地址进行连接,而不会进行DNS解析,所以此时的node2相当于是主机别名,它可以命名为任何其它名称,如node_2
(3).第三行通过IP地址定义主机节点
(4).第四行定义时还指定了端口号
(5).最后一行通过范围的方式展开成了两个主机节点192.168.200.32和192.168.200.33,同时还定义了这两个节点的主机变量ansible_port=22表示连接这两个节点时的端口号为22
范围表示 展开结果
-----------------------------
a[1:3] --> a1,a2,a3
[08:12] --> 08,09,10,11,12
a[a:c] --> aa,ab,ac
ansible 连接时的行为控制变量
例如上面的 ansible_port=22 ansible_port 就是控制变量,在每个版本中都不尽相同。
具体参考链接:https://docs.ansible.com/ansible/latest/inventory_guide/intro_inventory.html#connecting-to-hosts-behavioral-inventory-parameters
查看清单中的所有主机
ansible-inventory –graph all
要清单中添加变量
You can easily assign a variable to a single host and then use it later in playbooks. You can do this directly in your inventory file.
ini 文件格式
[atlanta]
host1 http_port=80 maxRequestsPerChild=808
host2 http_port=303 maxRequestsPerChild=909
yaml 文件格式
atlanta:
hosts:
host1:
http_port: 80
maxRequestsPerChild: 808
host2:
http_port: 303
maxRequestsPerChild: 909
多个清单文件
You can also combine multiple inventory source types in an inventory directory. This can be useful for combining static and dynamic hosts and managing them as one inventory. The following inventory directory combines an inventory plugin source, a dynamic inventory script, and a file with static hosts:
inventory/
openstack.yml # configure inventory plugin to get hosts from OpenStack cloud
dynamic-inventory.py # add additional hosts with dynamic inventory script
on-prem # add static hosts and groups
parent-groups # add static hosts and groups
You can target this inventory directory as follows:
ansible-playbook example.yml -i inventory
例如,Ansible配置文件将inventory指令设置为对应的目录:
inventory = /etc/ansible/inventorys
或者,ansible或ansible-playbook命令使用-i INVENTORY选项指定的路径应当为目录。
$ ansible-inventory -i /etc/ansible/inventorys --graph all
执行下面的命令将列出所有主机:
$ ansible-inventory -i /etc/ansible/inventorys --graph all