「Ansible」- 使用 主机清单 管理主机 @20210306

主机清单,Host Inventory,是配置文件,用于记录 Ansbile 管理主机,可对主机分组。

配置文件位置

默认为 /etc/ansible/hosts 文件,可以在 /etc/ansible/ansible.cfg 中设置 inventory 参数来修改 主机清单 的位置。

可以在命令行中使用选项来指定“主机清单”的位置:

ansible-playbook -i hosts site.yml

ansible-playbook --inventory-file hosts site.yml

# Excluding a host from a playbook run
# https://coderwall.com/p/mnnjkg/excluding-a-host-from-a-playbook-run
ansible-playbook --limit 'all:!bad_host' playbook.yml

编写主机清单文件

第一步、注释

使用 # 注释行,添加清单文件说明

第二步、定义目标主机

可以直接定义:

mail.example.com
ntpd.example.com

可以将主机分组:

[dbservers]
one.example.com
two.example.com
three.example.com

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

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

方法三、分组嵌套:

# 定义第一个主机分组
[group-atlanta]
hostl.example.com
host2.example.com

# 定义第二个主机分组
[group-raleigh]
host2.example.com
host3.example.com

# 第一个嵌套分组
[sourceeast:children]
atlanta
raleigh

# 第二个嵌套分组
[usa:children]
southeast
northeast
southwest
northwest

第三步、指定连接方法

所有可用参数参考 Connecting to hosts: behavioral inventory parameters 手册

在主机后面指定参数:

[targets]
localhost ansible_connection=local

other0.example.com ansible_connection=ssh ansible_user=mpdehaan
otherl.example.com ansible_connection=ssh ansible_user=mpdehaan

或者为所有主机指定连接方法:

[targets]
other0.example.com ansible_connection=ssh ansible_user=mpdehaan
otherl.example.com ansible_connection=ssh ansible_user=mpdehaan

[atlgroup:vars]
ansible_connection=ssh
ansible_user=root
ansible_ssh_private_key_file=/path/to/id_ras
ansible_ssh_common_args="-oStrictHostKeyChecking=no"

为主机定义变量(在主机清单中)

为单个主机指定变量:

[atlanta)
hostl http_port=80 maxRequestsPerChild=808
host2 http_port=303 maxRequestsPerChild=909

为分组指定变量:

[atlgroup]
hostl.example.com
host2.example.com

[atlgroup:vars]
ntp_sarvar=ntp.atlanta.example.com
proxy=proxy.atlanta.exampla.com

为所有主机及分组定义变量:

[atlgroup]
hostl.example.com
host2.example.com

[all:vars]
ntp_sarvar=ntp.atlanta.example.com
proxy=proxy.atlanta.exampla.com

为主机定义变量(在目录中)

假设主机清单为 /etc/ansible/hosts 文件,那么:

	主机变量 可以保存在''/etc/ansible/host_vars/''中, 文件名 与主机名相同。
	分组变量 可以保存在''/etc/ansible/group_vars/''中,文件名 与分组名相同。

文件名可以是以.yml.yaml结尾的YAML文件,或者是以.json结尾的JSON文件,或者不使用后缀。

如下目录结构示例,文件名称主机名或者分组名

/etc/ansible/group_vars/raleigh
/etc/ansible/group_vars/webservers
/etc/ansible/host_vars/foosball

文件 /etc/ansible/group_vars/raleigh 的内容为变量定义:

---
ntp_server: acme.example.erg
database_server : storage.example.org

为了分组或者可维护等原因,raleigh也可以是目录,然后分别存储不同的配置信息。此时文件名称为变量名,文件内容为变量值:

/etc/ansible/group_vars/raleigh/db_settiηgs
/atc/ansible/group_vars/ralaigh/cluster_settings

Ansible 会读取这个目录下面所有文件的内容。

主机变量目录(group_vars/和host_vars/)可放在主机清单文件同级目录下,或是Playbook文件同级目录下。如果两个目录下都存在变量文件,那么Playbook目录下的值会覆盖主机清单目录下变量的值。

相关链接

Docs » User Guide » Working with Inventory

相关文章

「Ansible」- yum / yum_repository
「Ansible」- 使用 Playbook 脚本
「Ansible」- 使用变量

参考文献

How to set host_key_checking=false in ansible inventory file?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值