【Ansible 学习之旅】Inventory文件介绍

系列文章

Ansible 介绍和架构
Ansible 安装和入门
配置控制机器和受控机器


目录


什么是 Ansible Inventory文件?

Ansible inventory file是 Ansible 用来了解要操作哪些主机及其相关信息的一个文件。库存文件通常位于 /etc/ansible/hosts,但也可以通过 --inventory-file 或环境变量 ANSIBLE_INVENTORY 指定其他位置。


库存文件的基本结构

库存文件的格式类似于 INI 文件,但具有自己的特性。以下是库存文件中的一些基本元素:

定义主机和组

  • 组:使用方括号 [ ] 来定义组名。
  • 主机:每行一个主机名或 IP 地址或域名,直接写在组定义下方。

示例:

复制代码
[web_servers]
host1.example.com
host2.example.com

[databases]
db1.example.com
db2.example.com

定义主机变量

  • 在主机名后面添加空格或制表符,然后列出变量名和值。

示例:

复制代码
[web_servers]
host1.example.com http_port=80 maxRequestsPerChild=808
host2.example.com http_port=303 maxRequestsPerChild=909

定义组变量

  • 使用 [组名:vars] 来定义组的变量。
  • 在 [组名:vars] 下定义的变量适用于该组内的所有主机。

示例:

复制代码
[atlanta]
host1
host2

[atlanta:vars]
ntp_server=ntp.atlanta.example.com
proxy=proxy.atlanta.example.com

定义复合组

  • 复合组由其他组构成,使用 [组名:children] 来定义子组。
  • 子组可以是其他复合组或简单组。

示例:

复制代码
[southeast:children]
atlanta
raleigh

[southeast:vars]
some_server=foo.southeast.example.com
halon_system_timeout=30
self_destruct_countdown=60
escape_pods=2
  • [southeast:children] 定义了一个复合组 southeast,它包含了 atlanta 和 raleigh 两个子组
  • [southeast:vars] 定义了 southeast 组的变量。这些变量可以被 southeast 组内的所有主机(包括 atlanta 和 raleigh 组中的主机)使用

定义主机连接属性

  • 可以为每个主机定义连接属性,如 SSH 端口、用户名等。

示例:

复制代码
[targets]
localhost ansible_connection=local
other1.example.com ansible_connection=ssh ansible_ssh_user=mpdehaan
other2.example.com ansible_connection=ssh ansible_ssh_user=mdehaan

简写模式

  • 可以使用简写模式来定义一系列相似的主机名。

示例:

复制代码
[webservers]
www[01:50].example.com

[databases]
db-[a:f].example.com

Inventory文件的高级特性

分文件定义变量

  • 可以使用单独的 YAML 文件来定义主机或组的变量。

  • 文件路径通常为:

    /etc/ansible/host_vars/hostname
    /etc/ansible/group_vars/groupname

  • 如果在 playbook 和 inventory 目录下都有 host_vars 和 group_vars 目录,则 playbook 目录下的配置会覆盖 inventory 目录下的配置。

示例:

复制代码
/etc/ansible/host_vars/foosball
/etc/ansible/group_vars/raleigh/db_settings
/etc/ansible/group_vars/raleigh/cluster_settings

动态库存

  • 动态库存允许从外部数据源(如云提供商 API)获取库存信息。
  • 动态库存脚本必须输出 JSON 或 YAML 数据。

示例:

bash 复制代码
#!/usr/bin/env python
import json

inventory = {
    "_meta": {"hostvars": {}},
    "webservers": {"hosts": ["192.168.1.10", "192.168.1.11"], "vars": {"http_port": 80}},
    "databases": {"hosts": ["192.168.1.12"]}
}

print(json.dumps(inventory))

库存参数

  • 库存文件中的参数可以控制 Ansible 与远程主机的交互方式。
    例如,定义 SSH 端口、用户名、连接类型等。

示例:

复制代码
some_host ansible_ssh_port=2222 ansible_ssh_user=manager
aws_host ansible_ssh_private_key_file=/home/example/.ssh/aws.pem
freebsd_host ansible_python_interpreter=/usr/local/bin/python
ruby_module_host ansible_ruby_interpreter=/usr/bin/ruby.1.9.3

参考

https://ansible-tran.readthedocs.io/en/latest/docs/intro_inventory.html#host-group

相关推荐
重启就好1 天前
【Ansible】模块详解
linux·服务器·ansible
是垚不是土1 天前
Kolla-Ansible搭建与扩容OpenStack私有云平台
linux·运维·服务器·云计算·ansible·openstack
xmweisi023 天前
Ansible内置模块之package
linux·ansible·rhce·rhca·红帽认证·it培训
xmweisi023 天前
Ansible内置模块之service
linux·ansible·rhce·rhca·红帽认证
企鹅侠客4 天前
Ansible 流程控制
ansible·ansible流程控制
小叶子来了啊6 天前
千锋教育Ansible自动化运维实战教程从入门到精通
运维·自动化·ansible
Johny_Zhao7 天前
阿里云Ansible自动化运维平台部署
linux·人工智能·ai·信息安全·云计算·ansible·shell·yum源·系统运维·itsm
xbd_zc9 天前
【Ansible自动化运维实战:从Playbook到负载均衡指南】
运维·自动化·ansible·负载均衡
#暖瓶11 天前
基于openEuler系统利用 Kolla-Ansible 工具多节点安装OpenStack平台
ansible·openstack
杨凯凡11 天前
Linux批量管理:Ansible自动化运维指南
linux·运维·服务器·自动化·ansible