【问题标题】:In Ansible dynamic inventory, how do I filter host by a tag?在 Ansible 动态清单中,如何按标签过滤主机?
【发布时间】:2021-03-04 01:37:51
【问题描述】:

堆栈:

  1. Git 存储库
  2. Ansible AWX,具体来说,AzureRM Inventory Plugin

问题:只有当虚拟机具有特定标签并且该标签具有特定价值时,我才需要在库存中导入虚拟机。

背景: Azure Inventory 插件有一个过滤器“exclude_host_filters”,下面的文档中提供了示例:

# excludes a host from the inventory when any of these expressions is true, can refer to any vars defined on the host
exclude_host_filters:
# excludes hosts in the eastus region
- location in ['eastus']
# excludes hosts that are powered off
- powerstate != 'running'

问题:我正在尝试在排除主机过滤器中使用标签。逻辑上我需要:

  1. 过滤所有带有“所有者”标签的主机。
  2. 然后过滤所有标签为“owner”且值为“testing_ansible”的主机。

这是我尝试做的:

exclude_host_filters:
- if tags.owner is defined
- tags.owner != 'testing_ansible'

我也尝试了其他过滤器,但没有奏效。文档需要 jinja2 格式。 如何检查标签是否存在以及是否具有一定的价值导入主机?

【问题讨论】:

    标签: ansible ansible-inventory ansible-awx


    【解决方案1】:

    根据the inventory plugin source codeexclude_host_filtersdefault_host_filters 中定义的过滤器使用此表达式进行评估:

    "{{% if filter %}} True {{% else %}} False {{% endif %}}"
    

    您的第一个过滤器中的 if 不允许评估此表达式。 检查标签是否存在并具有一定的价值:

    exclude_host_filters:
    - tags.owner is defined
    - tags.owner != 'testing_ansible'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-08
      相关资源
      最近更新 更多