我正在编写一个 Ansible 模板,需要生成主机组中的 IP 列表,排除当前主机IP。我在网上和文档中进行了搜索,但找不到任何允许您删除列表中项目的过滤器。我在下面创建了(hacky)for循环来执行此操作,但想知道是否有人知道这样的过滤“最佳实践”方式。
{% set filtered_list = [] %}
{% for host in groups['my_group'] if host != ansible_host %}
{{ filtered_list.append(host)}}
{% endfor %}
假设 groups['my_group'] 有 3 个 IP(192.168.1.1、192.168.1.2 和 192.168.1.3)。当为 192.168.1.1 生成模板时,它应该只打印 IP 的 192.168.1.2 和 192.168.1.3。
有difference
filter http://docs.ansible.com/ansible/playbooks_filters.html#list-filters为了那个原因:
- debug: var=item
with_items: "{{ groups['my_group'] | difference([inventory_hostname]) }}"
这将为您提供来自以下主机的所有项目my_group
没有当前主机。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)