本文介绍了如何将主机添加到 Ansible Tower 清单中的组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 tower_grouptower_host 模块将主机添加到组中?

How can I add a host to a group using tower_group or tower_host modules?

以下代码创建了一个主机和一个组,但它们彼此无关:

The following code creates a host and a group, but they are unrelated to each other:

---
- hosts: localhost
  connection: local
  gather_facts: false

  tasks:
    - tower_inventory:
        name: My Inventory
        organization: Default
        state: present
        tower_config_file: "~/tower_cli.cfg"

    - tower_host:
        name: myhost
        inventory: My Inventory
        state: present
        tower_config_file: "~/tower_cli.cfg"

    - tower_group:
        name: mygroup
        inventory: My Inventory
        state: present
        tower_config_file: "~/tower_cli.cfg"

文档提到了 instance_filters 参数(匹配主机的过滤器表达式的逗号分隔列表."),但是没有提供任何使用示例.

Docs mention instance_filters parameter ("Comma-separated list of filter expressions for matching hosts."), however do not provide any usage example.

instance_filters: myhost 添加到 tower_group 任务没有效果.

Adding instance_filters: myhost to the tower_group task has no effect.

推荐答案

这在 Tower 包含的模块中本身不可用,这些模块较旧并且使用已弃用的 tower-cli 包.

This isn't natively available in the modules included with Tower, which are older and use the deprecated tower-cli package.

但它在较新的 AWX 集合中可用,它使用 awx CLI,只要你有足够新的 Ansible(2.9 应该没问题).

But it is available in the newer AWX collection, which uses the awx CLI, as long as you have a recent enough Ansible (2.9 should be fine).

本质上,安装awx集合需求文件,或者直接像

In essence, install the awx collection through a requirements file, or directly like

ansible-galaxy collection install awx.awx -p ./collections

awx.awx 集合添加到您的剧本

Add the awx.awx collection to your playbook

collections:
  - awx.awx

然后使用 hosts: 选项到 tower_group:.

and then use the hosts: option to tower_group:.

- tower_group:
    name: mygroup
    inventory: My Inventory
    hosts:
      - myhost
    state: present

您可以在此处查看演示手册.

请注意,您可能需要 preserve_existing_hosts:如果您的组已经包含其他主机,则为真.不幸的是,似乎没有一种简单的方法可以从组中删除单个主机.

Be aware though that you may need preserve_existing_hosts: True if your group already contains other hosts. Unfortunately there does not seem to be an easy way to remove a single host from a group.

就您的示例而言,这可能会起作用:

In terms of your example this would probably work:

---
- hosts: localhost
  connection: local
  gather_facts: false
  collections:
    - awx.awx

  tasks:
    - tower_inventory:
        name: My Inventory
        organization: Default
        state: present
        tower_config_file: "~/tower_cli.cfg"

    - tower_host:
        name: myhost
        inventory: My Inventory
        state: present
        tower_config_file: "~/tower_cli.cfg"

    - tower_group:
        name: mygroup
        inventory: My Inventory
        state: present
        tower_config_file: "~/tower_cli.cfg"
        hosts:
          - myhost

这篇关于如何将主机添加到 Ansible Tower 清单中的组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-13 23:14