Ansible教程

Ansible 清单

Ansible 清单

Ansible 同时针对基础架构中的多个托管主机工作,使用列表或列表组称为清单。
定义清单后,您可以使用模式选择要针对 Ansible 运行的主机或组。
清单的默认位置是一个名为 /etc/ansible/hosts。您还可以使用-i <path> 选项在命令行中指定不同的清单文件。您可以从动态或云源或不同格式(YAML、ini)中提取清单文件。 Ansible 有清单插件,使其灵活和自定义。

主机和组

格式为 /etc/ansible/ 主机是INI 之类的格式,例如:
mail.example.com
[webservers]
foo.example.com
bar.example.com
[dbservers]
one.example.com
two.example.com
three.example.com
括号中的标题是组名,用于对系统进行分类。并决定你在什么时候为了什么目的控制什么政策。您可以将系统放在多个组中。
例如,服务器可以是 dbserver 和 webserver。
如果您的主机在非标准 SSH 端口上运行,那么您可以将端口号放在带有冒号的主机名之后。 SSH 配置文件中列出的端口可用于 OpenSSH 连接,但不能用于 paramiko 连接。
为了使事情明确,如果项目未在默认端口上运行,建议您设置它们:
badwolf.example.com:5309
假设您有静态 IP 并且想要设置一些存在于您的主机文件中的别名,或者您可以通过隧道进行连接。此外,您可以像以下示例一样描述主机:
Jumper ansible_port=5555 ansible_host=192.0.2.50
在上面的示例中,尝试针对主机别名"jumper"进行 Ansible 将在端口 5555 上连接 192.0.2.50。它使用清单文件的功能来定义特殊变量。

主机变量

您可以将变量分配给将在剧本中使用的主机,例如:
[atlanta]
host1 http_port=80 maxRequestsPerChild=808
host2 http_port=303 maxRequestsPerChild=909

组变量

变量可以一次应用于整个组,例如:
[atlanta]
host1
host2
[atlanta:vars]
ntp_server=ntp.atlanta.example.com
proxy=proxy.atlanta.example.com

组的组和组变量

可以使用 :children's 后缀来创建组的组。您可以使用 :vars 应用变量。
[atlanta]
host1
host2
[raleigh]
host2
host3
[southeast: children]
Atlanta
Raleigh
[southeast:vars]
some_server=foo.southeast.example.com
halon_system_timeout=30
self_destruct_countdown=60
escape_pods=2
[usa: children]
southeast
northeast
southwest
northwest
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4