Ansible 组中基于 ip 范围的动态组 [英] Dynamic groups based on ip range in Ansible groups

查看:68
本文介绍了Ansible 组中基于 ip 范围的动态组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何根据 ansible 清单组中的 ip 地址范围创建变量组?我在不同的位置有两组服务器.我想在每次剧本时创建组,以便剧本在更新的服务器列表上运行.我已经根据主机库存中的分布、生产、开发、测试、qa、dr 进行了分组.

How to create variable group based on ip address-range in ansible inventory groups ? I have two groups of servers in different location. I want to create the groups every time the playbook so that the playbook is run on a updated list of servers. I already groups based on distribution, prod, dev, test, qa ,dr in my host inventory.

提前致谢

推荐答案

您可以在开始时扫描所有主机并根据需要对它们进行分组,例如通过网络:

You can scan all hosts at the beginning and group them how you want, e.g. by network:

- hosts: all
  tasks:
    - group_by: key=network_{{ ansible_default_ipv4.network }}

这将创建与您拥有的不同网络一样多的名称为network_ 的组.但请记住,它需要从所有主机收集事实,因此根据主机数量和连接速度,它可能需要大量时间.事实缓存会加快速度,但无论如何...

This will create as many groups with names network_<network> as many different networks you have. But keep in mind that it need to gather facts from all hosts, so depending on count of hosts and speed of connections it can require significant time. Facts caching will speed up things, but anyway...

如果您手动管理库存文件,也可以考虑手动指定区域组.

If you manage your inventory file by hand, consider manually specifying regional groups as well.

这篇关于Ansible 组中基于 ip 范围的动态组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆