库存插件 "constructed" 可以基于 ansible_facts 创建组吗?
Can inventory plugin "constructed" create a groups based on ansible_facts?
我在 AWS 中有很多 EC2 服务器,我想根据从 ansible_facts 收集的事实(通过模块设置)对它们进行分类。例如有一个组 "CentOS" 其中 ansible_facts['distribution'] == 'CentOS'
等等。但是有文件 inventory/constructed.yml
:
plugin: constructed
strict: False
keyed_groups:
# this creates a group per distro (distro_CentOS, distro_Debian) and assigns the hosts that have matching values to it,
# using the default separator "_"
- prefix: distro
key: ansible_distribution
我在命令 ansible-inventory --graph
的输出中没有看到 distro_CentOS
组。
该插件已在 ansible.cfg
中启用:
[inventory]
enable_plugins = host_list, ini, aws_ec2, constructed
如何根据收集到的可靠事实对 EC2 实例进行排序,避免标记每个实例的需要?
constructed
库存插件依赖于已知事实。这意味着它们要么需要是清单文件中的变量,要么是缓存的事实。
尝试设置一个简单的本地 jsonfile 缓存。查看 https://docs.ansible.com/ansible/latest/plugins/cache.html 或将与此类似的内容添加到您的 ansible.cfg
文件:
[defaults]
fact_caching = jsonfile
gathering = smart
fact_caching_timeout = 600
fact_caching_connection = /home/<user>/facts_cache
接下来,运行 跨所有主机收集事实的临时命令:
ansible all -m setup
现在尝试重新列出您的库存。您应该会看到基于 ansible_distribution
自动生成的组
请注意,当达到事实缓存超时时(在我上面的示例配置文件中为 10 分钟),事实变量不能与 constructed
插件一起使用,因此您需要再次收集所有事实。
清单插件在设置模块运行之前运行,不能依赖其输出(缓存可以改变这一点)
group_by
模块似乎正是为这个用例设计的:
- group_by:
key: distro_{{ ansible_distribution }}
我在 AWS 中有很多 EC2 服务器,我想根据从 ansible_facts 收集的事实(通过模块设置)对它们进行分类。例如有一个组 "CentOS" 其中 ansible_facts['distribution'] == 'CentOS'
等等。但是有文件 inventory/constructed.yml
:
plugin: constructed
strict: False
keyed_groups:
# this creates a group per distro (distro_CentOS, distro_Debian) and assigns the hosts that have matching values to it,
# using the default separator "_"
- prefix: distro
key: ansible_distribution
我在命令 ansible-inventory --graph
的输出中没有看到 distro_CentOS
组。
该插件已在 ansible.cfg
中启用:
[inventory]
enable_plugins = host_list, ini, aws_ec2, constructed
如何根据收集到的可靠事实对 EC2 实例进行排序,避免标记每个实例的需要?
constructed
库存插件依赖于已知事实。这意味着它们要么需要是清单文件中的变量,要么是缓存的事实。
尝试设置一个简单的本地 jsonfile 缓存。查看 https://docs.ansible.com/ansible/latest/plugins/cache.html 或将与此类似的内容添加到您的 ansible.cfg
文件:
[defaults]
fact_caching = jsonfile
gathering = smart
fact_caching_timeout = 600
fact_caching_connection = /home/<user>/facts_cache
接下来,运行 跨所有主机收集事实的临时命令:
ansible all -m setup
现在尝试重新列出您的库存。您应该会看到基于 ansible_distribution
请注意,当达到事实缓存超时时(在我上面的示例配置文件中为 10 分钟),事实变量不能与 constructed
插件一起使用,因此您需要再次收集所有事实。
清单插件在设置模块运行之前运行,不能依赖其输出(缓存可以改变这一点)
group_by
模块似乎正是为这个用例设计的:
- group_by:
key: distro_{{ ansible_distribution }}