库存
了解 Ansible inventory 文件及其不同类型。
Ansible 中的库存(inventory)指的是我们可以用自动化方式目标的主机或节点集合。它可以通过分组或模式进行组织,使我们能够选择特定的主机或组来执行 Ansible 任务。库存对于任务自动化至关重要。
all 关键字
关键字 all 包含了所使用的库存中的所有主机。唯一的例外是 localhost,需要显式指定。
all 关键字非常重要,在我们的 Ansible 使用中是必需的。
ansible-inventory 工具
ansible-inventory 命令行工具包含在每个 Ansible 安装中,其用途是显示当前 Ansible 库存信息。验证当前 Ansible 库存状态非常有帮助。
它支持 Ansible Vault 以及 INI、YAML 和 JSON 格式的 Ansible 库存。
我们可以使用 --help 标志获取完整的参数列表。
一个实用的功能是使用 --list 参数显示主机列表,或使用 --graph 参数以树形视图显示。
Ansible 列表视图
ansible-inventory 命令的列表视图有助于显示 Ansible 当前使用的库存的 JSON 内容。随着库存变得越复杂,列表就越长。
假设我们有一个类似于以下内容的简单 INI 库存文件:
1localhost ansible_connection=local
我们可以使用以下命令获取列表视图:
1ansible-inventory -i inventory --list
其中:
ansible-inventory命令用于处理 Ansible 库存。-i参数指定了当前目录中的源inventory文件。--list参数请求以列表视图格式输出。
执行结果如下:
ansible-inventory --list 命令的输出
1{ 2 "_meta": { 3 "hostvars": { 4 "localhost": { 5 "ansible_connection": "local" 6 } 7 } 8 }, 9 "all": { 10 "children": [ 11 "ungrouped" 12 ] 13 }, 14 "ungrouped": { 15 "hosts": [ 16 "localhost" 17 ] 18 } 19}
Ansible 图形视图
ansible-inventory 命令的图形视图有助于以树形结构展示 Ansible 当前使用的库存。
假设我们有一个类似于以下内容的简单 INI 库存文件:
1localhost ansible_connection=local
我们可以使用以下命令查看树形视图:
1ansible-inventory -i inventory --graph
其中:
ansible-inventory命令用于处理 Ansible 库存。-i参数指定了当前目录中的源inventory文件。--graph参数请求以图形视图格式输出。
执行结果如下:
ansible-inventory --graph 命令的输出
1@all: 2 |--@ungrouped: 3 | |--localhost
INI 库存文件
INI 库存是一种简单且流行的库存类型,允许列出主机或 IP 地址。其默认位置为 /etc/ansible/hosts,但可以使用 -i 参数自定义。INI 格式在 IT 行业中广泛用于配置文件。它是一个包含 name=value 字段的文件。该文件还可以使用方括号定义部分:[section]。
让我们通过下面的 ini_simple_inventory 文件引入一个简单示例:
1one.example.com 2 3[web] 4two.example.com 5three.example.com
Ansible 库存文件列出了三个位于 example.com 域名下的主机。
具体来说,one.example.com 主机未分组。
two.example.com 和 three.example.com 主机被分组为 web 组。
YAML 库存文件
YAML 格式是一种简单且易于阅读的格式,常用于编写 Ansible 库存。请务必注意代码的缩进。
所有主机的完整列表必须位于 all 关键字下。
每个主机必须位于 hosts 字段下。
我们可以像 INI 格式一样,在 children 关键字下指定组。
上一节的简单库存可以用以下代码表示:
1--- 2all: 3 hosts: 4 one.example.com: 5 children: 6 web: 7 hosts: 8 two.example.com: 9 three.example.com:
one.example.com 未分组,但 two.example.com 和 three.example.com 主机被分组为 web。
主机范围
组成员也可以使用数字或字母的范围来定义。在数字字段中,我们可以指定步长作为数字序列之间的增量。
1[web] 2www[01:19].example.com
在 ini_range_inventory 文件中,web 组包含从 www01.example.com 到 www19.example.com 的所有主机。
我们可以使用列表视图命令测试 Ansible 库存:
1ansible-inventory -i ini_range_inventory --list
该命令产生以下输出:
ini_range_inventory 库存的输出
1{ 2 "_meta": { 3 "hostvars": {} 4 }, 5 "all": { 6 "children": [ 7 "ungrouped", 8 "web" 9 ] 10 }, 11 "web": { 12 "hosts": [ 13 "www01.example.com", 14 "www02.example.com", 15 "www03.example.com", 16 "www04.example.com", 17 "www05.example.com", 18 "www06.example.com", 19 "www07.example.com", 20 "www08.example.com", 21 "www09.example.com", 22 "www10.example.com", 23 "www11.example.com", 24 "www12.example.com", 25 "www13.example.com", 26 "www14.example.com", 27 "www15.example.com", 28 "www16.example.com", 29 "www17.example.com", 30 "www18.example.com", 31 "www19.example.com" 32 ] 33 } 34}
多组中的主机
主机可以在库存中属于多个组。 此功能在生产环境中非常有用,因为我们可以从不同角度对系统进行分类。
1one.example.com 2 3[web] 4two.example.com 5three.example.com 6 7[prod] 8two.example.com 9 10[dev] 11three.example.com
在此 INI 库存示例中,two.example.com 和 three.example.com 主机属于 web 组;two.example.com 同时存在于 web 和 prod 组中,而 three.example.com 存在于 web 和 dev 组中。
我们可以使用列表视图命令验证这一点:
1ansible-inventory -i ini_groupsmultiple_inventory --list
这会产生以下输出:
ini_groupsmultiple_inventory 库存的输出
1{ 2 "_meta": { 3 "hostvars": {} 4 }, 5 "all": { 6 "children": [ 7 "dev", 8 "prod", 9 "ungrouped", 10 "webservers" 11 ] 12 }, 13 "dev": { 14 "hosts": [ 15 "three.example.com" 16 ] 17 }, 18 "prod": { 19 "hosts": [ 20 "two.example.com" 21 ] 22 }, 23 "ungrouped": { 24 "hosts": [ 25 "one.example.com" 26 ] 27 }, 28 "webservers": { 29 "hosts": [ 30 "three.example.com", 31 "two.example.com" 32 ] 33 } 34}
如我们所见,每个组名都有其内部主机的列表。
主机变量
在库存中,我们可能希望存储与特定主机或组相关的变量值。
这种情况很常见,因为它定义了与不同主机的不同连接方式。例如,对 localhost 使用 local 连接,对所有其他主机使用默认的 ssh 连接。对于每台主机,我们可以自定义登录用户。例如,one.example.com 使用 devops,two.example.com 使用 ansible。
1[webservers] 2localhost ansible_connection=local 3one.example.com ansible_connection=ssh ansible_user=devops 4two.example.com ansible_connection=ssh ansible_user=ansible
我们可以使用以下 Ansible 命令验证列表视图:
1ansible-inventory -i ini_hostinventory --list
该命令产生以下输出:
ini_hostinventory 库存的输出
1{ 2 "_meta": { 3 "hostvars": { 4 "localhost": { 5 "ansible_connection": "local" 6 }, 7 "one.example.com": { 8 "ansible_connection": "ssh", 9 "ansible_user": "devops" 10 }, 11 "two.example.com": { 12 "ansible_connection": "ssh", 13 "ansible_user": "ansible" 14 } 15 } 16 }, 17 "all": { 18 "children": [ 19 "ungrouped", 20 "webservers" 21 ] 22 }, 23 "webservers": { 24 "hosts": [ 25 "localhost", 26 "one.example.com", 27 "two.example.com" 28 ] 29 } 30}
组变量
组变量允许我们设置适用于特定组内所有主机的特定变量。
考虑以下场景:我们必须将 ntp_server 变量的值设为 europe.pool.ntp.org,应用于该组的所有主机。
以下是带有组变量 ntp_server 的 INI 库存,为 webservers 组内的所有主机提供共享的 NTP 服务器:
1[webservers] 2one.example.com 3two.example.com 4 5[webservers:vars] 6ntp_server=europe.pool.ntp.org
我们可以使用以下命令获得列表视图结果:
1ansible-inventory -i ini_groupsvariables_inventory --list
该命令产生以下输出:
ini_groupsvariables_inventory 库存的输出
1{ 2 "_meta": { 3 "hostvars": { 4 "one.example.com": { 5 "ntp_server": "europe.pool.ntp.org" 6 }, 7 "two.example.com": { 8 "ntp_server": "europe.pool.ntp.org" 9 } 10 } 11 }, 12 "all": { 13 "children": [ 14 "ungrouped", 15 "webservers" 16 ] 17 }, 18 "webservers": { 19 "hosts": [ 20 "one.example.com", 21 "two.example.com" 22 ] 23 } 24}
我们也可以使用 YAML 库存获得相同的结果。 以下是之前示例的 YAML 格式版本:
1--- 2webservers: 3 hosts: 4 two.example.com: 5 three.example.com: 6 vars: 7 ntp_server: europe.pool.ntp.org
我们可以使用以下命令获得列表视图:
1ansible-inventory -i groupsvariables_inventory.yml --list
以下是列表视图的输出:
groupsvariables_inventory.yml 库存的输出
1{ 2 "_meta": { 3 "hostvars": { 4 "three.example.com": { 5 "ntp_server": "europe.pool.ntp.org" 6 }, 7 "two.example.com": { 8 "ntp_server": "europe.pool.ntp.org" 9 } 10 } 11 }, 12 "all": { 13 "children": [ 14 "ungrouped", 15 "webservers" 16 ] 17 }, 18 "webservers": { 19 "hosts": [ 20 "three.example.com", 21 "two.example.com" 22 ] 23 } 24}
继承变量值
主机和组也可以组合使用。在以下示例中,web 组有两个成员:asia 和 europe。这两个组分别定义了单个主机:host1.example.com 和 host2.example.com,但也可以包含更多主机。ntp_server 变量在 web 级别定义。因此,最终 ntp_server 变量对 web、asia 和 europe 组都可用。ntp_server 变量也对 host1.example.com 和 host2.example.com 主机可用。
以下是包含继承变量的最终库存:
1[asia] 2host1.example.com 3 4[europe] 5host2.example.com 6 7[web:children] 8asia 9europe 10 11[web:vars] 12ntp_server=europe.pool.ntp.org
我们可以使用以下命令获取库存的列表视图:
1ansible-inventory -i ini_variableinheriting_inventory --list
该命令产生以下输出:
ini_variableinheriting_inventory 库存的输出
1{ 2 "_meta": { 3 "hostvars": { 4 "host1.example.com": { 5 "ntp_server": "europe.pool.ntp.org" 6 }, 7 "host2.example.com": { 8 "ntp_server": "europe.pool.ntp.org" 9 } 10 } 11 }, 12 "all": { 13 "children": [ 14 "ungrouped", 15 "web" 16 ] 17 }, 18 "asia": { 19 "hosts": [ 20 "host1.example.com" 21 ] 22 }, 23 "europe": { 24 "hosts": [ 25 "host2.example.com" 26 ] 27 }, 28 "web": { 29 "children": [ 30 "asia", 31 "europe" 32 ] 33 } 34}
我们也可以使用 YAML 格式表达相同的 Ansible 库存。
1--- 2children: 3 hosts: 4 web: 5 asia: 6 hosts: 7 host1.example.com: 8 europe: 9 hosts: 10 host2.example.com: 11 vars: 12 ntp_server: europe.pool.ntp.org
我们可以使用以下命令获取列表视图:
1ansible-inventory -i variableinheriting_inventory.yml --list
输出虽然较长,但并不复杂。它按预期报告了配置:
variableinheriting_inventory.yml 库存的输出
1{ 2 "_meta": { 3 "hostvars": { 4 "web": { 5 "asia": { 6 "hosts": { 7 "host1.example.com": null 8 } 9 }, 10 "europe": { 11 "hosts": { 12 "host2.example.com": null 13 } 14 }, 15 "vars": { 16 "ntp_server": "europe.pool.ntp.org" 17 } 18 } 19 } 20 }, 21 "all": { 22 "children": [ 23 "children", 24 "ungrouped" 25 ] 26 }, 27 "children": { 28 "hosts": [ 29 "web" 30 ] 31 } 32}
localhost 库存文件
库存中的一个特殊情况是 localhost。我们需要将连接类型指定为 local,否则 Ansible 将使用默认的 ssh 连接。
以下是 INI 格式中的经典 Ansible 示例:
ini_local_inventory 文件
1localhost ansible_connection="local"
我们可以使用以下命令获取此库存的列表视图:
1ansible-inventory -i ini_local_inventory --list
这会产生以下输出:
ini_local_inventory 库存的输出
1{ 2 "_meta": { 3 "hostvars": { 4 "localhost": { 5 "ansible_connection": "local" 6 } 7 } 8 }, 9 "all": { 10 "children": [ 11 "ungrouped" 12 ] 13 }, 14 "ungrouped": { 15 "hosts": [ 16 "localhost" 17 ] 18 } 19}
使用多个库存源
每次执行时可以使用多个库存文件。
例如,我们可以指定 ini_simple_inventory 和 ini_local_inventory 库存,两次设置 -i 参数。
1ansible-inventory -i ini_local_inventory -i ini_simple_inventory --list
结果是两个 Ansible 库存 ini_local_inventory 和 ini_simple_inventory 的合并输出:
ini_local_inventory 和 ini_simple_inventory 库存的输出
1{ 2 "_meta": { 3 "hostvars": { 4 "localhost": { 5 "ansible_connection": "local" 6 } 7 } 8 }, 9 "all": { 10 "children": [ 11 "ungrouped", 12 "webservers" 13 ] 14 }, 15 "ungrouped": { 16 "hosts": [ 17 "localhost", 18 "one.example.com" 19 ] 20 }, 21 "webservers": { 22 "hosts": [ 23 "three.example.com", 24 "two.example.com" 25 ] 26 } 27}
实验场
让我们在下面的实验场中尝试本课介绍的命令。
-
显示列表视图。
1ansible-inventory -i inventory --list -
显示图形列表。
1ansible-inventory -i inventory --graph -
显示简单的 INI 库存。
1ansible-inventory -i ini_range_inventory --list -
显示简单的 YAML 库存。
1ansible-inventory -i simple_yaml_inventory.yml --list -
显示多组中的主机。
1ansible-inventory -i ini_groupsmultiple_inventory --list -
显示主机变量。
1ansible-inventory -i ini_hostinventory --list -
显示 INI 库存中的组变量。
1ansible-inventory -i ini_groupsvariables_inventory --list -
显示 YAML 库存中的组变量。
1ansible-inventory -i groupsvariables_inventory.yml --list -
显示继承的变量值。
1ansible-inventory -i ini_variableinheriting_inventory --list -
显示
localhost库存。1ansible-inventory -i ini_local_inventory --list -
显示多个库存源。
1ansible-inventory -i ini_local_inventory -i ini_simple_inventory --list
-
显示列表视图。
1ansible-inventory -i inventory --list -
显示图形列表。
1ansible-inventory -i inventory --graph -
显示简单的 INI 库存。
1ansible-inventory -i ini_range_inventory --list -
显示简单的 YAML 库存。
1ansible-inventory -i simple_yaml_inventory.yml --list -
显示多组中的主机。
1ansible-inventory -i ini_groupsmultiple_inventory --list -
显示主机变量。
1ansible-inventory -i ini_hostinventory --list -
显示 INI 库存中的组变量。
1ansible-inventory -i ini_groupsvariables_inventory --list -
显示 YAML 库存中的组变量。
1ansible-inventory -i groupsvariables_inventory.yml --list -
显示继承的变量值。
1ansible-inventory -i ini_variableinheriting_inventory --list -
显示
localhost库存。1ansible-inventory -i ini_local_inventory --list -
显示多个库存源。
1ansible-inventory -i ini_local_inventory -i ini_simple_inventory --list
No configuration found for ID: 0675fcebccf8216aadfc09fdcefdbcc8197b4e67276c6e1e49ac53a976f37fa7