如何在 Ansible 中设置默认 inventory 文件路径

AnsibleBeginner
立即练习

介绍

Ansible 是一个被广泛使用的 IT 自动化工具,它简化了基础设施和应用程序的管理。在本教程中,我们将探讨如何在 Ansible 中设置默认的 inventory 文件路径,从而确保高效且一致的基础设施管理。Ansible 中的 inventory 文件包含 Ansible 管理的主机列表。通过配置自定义的 inventory 文件位置,你可以更好地组织你的自动化工作流程。

理解 Ansible Inventory

在配置自定义 inventory 文件路径之前,让我们先了解一下 Ansible inventory 是什么,以及它默认是如何工作的。

什么是 Ansible Inventory?

Ansible inventory 是一个包含 Ansible 将要管理的主机信息的 文件。默认情况下,Ansible 在 /etc/ansible/hosts 查找 inventory 文件。inventory 可以采用多种格式,但最常见的是 INI 风格的文件或 YAML 文件。

让我们检查一下 Ansible 是否已正确安装在我们的系统上:

ansible --version

你应该看到类似这样的输出:

ansible [core 2.12.x]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/labex/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.10.x (default, Ubuntu, etc.)

现在,让我们看看默认的 inventory 文件位置:

cat /etc/ansible/hosts

这个文件可能为空,或者包含示例主机配置。默认的 inventory 文件通常看起来像这样:

## Example inventory file

[webservers]
web1.example.com
web2.example.com

[dbservers]
db1.example.com
db2.example.com

在这种格式中:

  • [webservers][dbservers] 是组名
  • 在每个组下列出的主机名或 IP 地址属于该组

对于我们的实验,我们将在不同的位置创建我们自己的自定义 inventory 文件。

创建自定义 Inventory 文件

现在我们了解了 inventory 文件是什么,让我们在我们的项目目录中创建我们自己的自定义 inventory 文件。

为 Ansible 文件创建目录

首先,让我们导航到我们的项目目录,并为我们的 Ansible 文件创建一个专用文件夹:

cd ~/project
mkdir -p ansible/inventory

创建自定义 Inventory 文件

现在,让我们在新目录中创建一个简单的 inventory 文件:

cat > ansible/inventory/hosts.ini << 'EOF'
## Custom inventory file

[webservers]
web1.example.com
web2.example.com

[dbservers]
db1.example.com
db2.example.com

[local]
localhost ansible_connection=local

[all:vars]
ansible_user=ubuntu
EOF

此 inventory 文件定义了三个组:

  • webservers:包含两个 Web 服务器
  • dbservers:包含两个数据库服务器
  • local:包含 localhost,具有特殊的连接设置

让我们检查一下我们新创建的 inventory 文件:

cat ansible/inventory/hosts.ini

你应该看到我们刚刚创建的内容。

此文件现在已准备好用作我们的自定义 inventory 文件。在下一步中,我们将配置 Ansible 默认使用此文件,而不是系统范围的 /etc/ansible/hosts 文件。

设置默认 Inventory 文件路径

现在我们已经创建了自定义 inventory 文件,让我们配置 Ansible 使用它作为默认的 inventory 文件。有几种方法可以做到这一点,但我们将重点关注两种最常见的方法。

方法 1:使用 Ansible 配置文件

Ansible 按照以下顺序查找配置文件:

  1. ANSIBLE_CONFIG 环境变量
  2. 当前目录中的 ansible.cfg
  3. ~/.ansible.cfg(用户主目录)
  4. /etc/ansible/ansible.cfg(系统范围)

让我们在我们的项目目录中创建一个 ansible.cfg 文件:

cat > ~/project/ansible/ansible.cfg << 'EOF'
[defaults]
inventory = ~/project/ansible/inventory/hosts.ini
host_key_checking = False
EOF

在此配置文件中,我们设置了:

  • inventory:指向我们自定义 inventory 文件的路径
  • host_key_checking:禁用以防止 SSH 主机密钥验证提示

方法 2:使用环境变量

另一种指定 inventory 文件的方法是使用 ANSIBLE_INVENTORY 环境变量。当你希望临时使用不同的 inventory 文件而无需更改配置时,这很有用:

export ANSIBLE_INVENTORY=~/project/ansible/inventory/hosts.ini

让我们通过运行一个简单的 Ansible 命令来确认我们的配置是否有效:

cd ~/project/ansible
ansible --list-hosts all

此命令应该列出我们自定义 inventory 文件中的所有主机:

  hosts (5):
    web1.example.com
    web2.example.com
    db1.example.com
    db2.example.com
    localhost

现在,当我们从 ~/project/ansible 目录运行 Ansible 命令时,Ansible 将默认使用我们的自定义 inventory 文件。

验证 Inventory 配置

现在我们已经配置了自定义 inventory 文件,让我们验证 Ansible 是否正确使用它,并探索一些用于处理 inventory 的命令。

使用 ansible-inventory 检查 Inventory

ansible-inventory 命令允许我们查看和验证我们的 inventory 配置:

cd ~/project/ansible
ansible-inventory --list

这将以 JSON 格式显示 inventory:

{
  "_meta": {
    "hostvars": {
      "localhost": {
        "ansible_connection": "local"
      }
    }
  },
  "all": {
    "children": ["dbservers", "local", "ungrouped", "webservers"]
  },
  "dbservers": {
    "hosts": ["db1.example.com", "db2.example.com"]
  },
  "local": {
    "hosts": ["localhost"]
  },
  "webservers": {
    "hosts": ["web1.example.com", "web2.example.com"]
  }
}

为了获得更易读的格式,我们可以使用 --graph 选项:

ansible-inventory --graph

这将以类似树状结构显示 inventory:

@all:
  |--@dbservers:
  |  |--db1.example.com
  |  |--db2.example.com
  |--@local:
  |  |--localhost
  |--@ungrouped:
  |--@webservers:
  |  |--web1.example.com
  |  |--web2.example.com

测试与本地主机的连接

让我们运行一个简单的 ping 命令来验证我们是否可以与本地主机通信:

ansible local -m ping

你应该看到类似于以下的输出:

localhost | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}

在主机上运行一个简单的命令

我们也可以使用 ansible 命令在我们的主机上运行一个命令。由于我们的示例主机实际上并不存在,让我们仅针对本地主机:

ansible local -a "uname -a"

这将在本地主机上执行 uname -a 命令,并显示类似于以下的输出:

localhost | CHANGED | rc=0 >>
Linux ubuntu 5.15.0-1033-aws #37-Ubuntu SMP Wed Aug 16 07:38:46 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

这确认了 Ansible 正在正确使用我们的自定义 inventory 文件,并且可以在其中定义的主机上执行命令。

总结

在本教程中,你学习了如何在 Ansible 中设置和配置自定义 inventory 文件路径。你现在了解了:

  • 什么是 Ansible inventory 文件及其默认位置
  • 如何使用主机组创建自定义 inventory 文件
  • 如何使用配置文件方法和环境变量配置 Ansible 默认使用你的自定义 inventory 文件
  • 如何使用各种 Ansible 命令验证你的 inventory 配置

这些技能将帮助你更有效地组织你的 Ansible 自动化工作流程。通过自定义 inventory 文件位置,你可以根据项目的结构和需求更好地管理你的基础设施。

在处理大型项目时,你可能需要探索动态 inventory 或使用 Ansible Vault 来保护 inventory 文件中的敏感信息。