在本章中,我将为您介绍 Ansible 的主机清单(Inventory),解释它是什么、为什么需要它、它的不同类型,以及如何在日常工作中编辑和使用它。

什么是 Ansible 主机清单

  1. 主机清单的定义 Ansible 的主机清单是 Ansible 可以操作的主机集合。它本质上是您基础设施中节点或主机的列表,通过一个清单文件或一组清单文件来定义。您可以使用“组”或“模式”来组织清单,以便选择您希望 Ansible 操作的主机或组。
  2. “all” 关键字 特殊关键字“all”包含清单中除 localhost 外的所有主机。它在后续的课程中会非常有用,但需要注意的是,localhost 需要单独指定。

简单的 INI 格式清单

文件名:ini_simple_inventory

one.example.com

[webservers]
two.example.com
three.example.com
  • one.example.com 是未分组的主机。
  • two.example.com 和 three.example.com 被归为“webservers”组。

最简单的清单类型是 INI 格式的清单。默认位置是 /etc/ansible/hosts,但您也可以通过 -i 参数指定自定义的清单文件。在本例中,one.example.com 是未分组的主机,而 two.example.com 和 three.example.com 被归为“webservers”组。

简单的 YAML 格式清单

文件名:inventory.yml

---
all:
  hosts:
    one.example.com:
  children:
    webservers:
      hosts:
        two.example.com:
        three.example.com:
  • one.example.com 是未分组的主机。
  • two.example.com 和 three.example.com 被归为“webservers”组。

您也可以使用 YAML 语法来表示相同的清单。在本例中,one.example.com 是未分组的主机,而 two.example.com 和 three.example.com 被归为“webservers”组。

添加主机范围

文件名:ini_range_inventory

[webservers]
www[01:99].example.com

[databases]
db-[a-f].example.com
  • “webservers” 组包含从 www01.example.com 到 www99.example.com 的所有主机。
  • “databases” 组包含从 db-a.example.com 到 db-f.example.com 的所有主机。

组成员可以通过数字或字母范围来定义。在数字范围中,您还可以指定步长(即数字序列之间的增量)。在本例中,“webservers” 组包含从 www01.example.com 到 www99.example.com 的所有主机,而“databases”组包含从 db-a.example.com 到 db-f.example.com 的所有主机。

主机属于多个组

文件名:ini_groupsmultiple_inventory

one.example.com

[webservers]
two.example.com
three.example.com

[prod]
two.example.com

[dev]
three.example.com
  • two.example.com 和 three.example.com 属于多个组。
  • two.example.com 同时属于“webservers”和“prod”组。
  • three.example.com 同时属于“webservers”和“dev”组。

主机可以属于多个组。在本例中,two.example.com 和 three.example.com 被归为“webservers”组,同时 two.example.com 也属于“prod”组,three.example.com 也属于“dev”组。

主机变量

文件名:ini_hostinventory

[webservers]
localhost  ansible_connection=local
one.example.com ansible_connection=ssh ansible_user=devops
two.example.com ansible_connection=ssh ansible_user=ansible
  • 自定义了 ansible_connection 和 ansible_user 变量。

在清单中,您可以为特定主机或组存储变量值。这种场景非常常见,因为它定义了与不同主机的不同连接方式。例如,为 localhost 使用“local”连接,为其他主机使用默认的“ssh”连接。您还可以为每个主机自定义登录用户,例如为 one.example.com 使用用户“devops”,为 two.example.com 使用用户“ansible”。

组变量

文件名:ini_groupsvariables_inventory

[webservers]
one.example.com
two.example.com

[webservers:vars]
ntp_server=europe.pool.ntp.org

文件名:inventory.yml

---
webservers:
  hosts:
    two.example.com:
    three.example.com:
  vars:
    ntp_server: europe.pool.ntp.org

与单个主机类似,您也可以为组定义变量。在本例中(INI 和 YAML 格式),变量 ntp_server 被赋值为 europe.pool.ntp.org,适用于该组的所有主机。

继承变量值

文件名:ini_variableinheriting_inventory

[asia]
host1.example.com

[europe]
host2.example.com

[webservers:children]
asia
europe

[webservers:vars]
ntp_server=europe.pool.ntp.org

文件名:variableinheriting_inventory.yml

---
children:
  webservers:
    children:
      asia:
        hosts:
          host1.example.com:
      europe:
        hosts:
          host2.example.com:
    vars:
      ntp_server: europe.pool.ntp.org

主机和组可以组合在一起。在本例中,“webservers”组有两个成员:“asia”和“europe”。这两个元素分别定义为单个主机 host1.example.com 和 host2.example.com,但也可以包含更多主机。变量 ntp_server 在“webservers”组级别定义,因此最终该变量适用于“webservers”、“asia”和“europe”组,同时也适用于 host1.example.com 和 host2.example.com 主机。

使用多个清单源

$ ansible-playbook playbook.yml -i production -i development
  • 执行名为 playbook.yml 的 Ansible Playbook,目标为“production”和“development”清单。

您可以为每次执行使用多个清单文件。在本例中,将执行名为 playbook.yml 的 Playbook,目标为“production”和“development”清单。

本地主机清单

文件名:inventory

localhost ansible_connection="local"

默认路径:/etc/ansible/hosts

在清单中,localhost 是一个特殊情况。您需要指定连接类型为“local”,否则 Ansible 会默认使用 SSH 连接。

总结

现在您已经了解了 Ansible 的 INI 和 YAML 格式清单文件,以及主机和组变量的相关知识。