How to add host to group in Ansible Tower inventor

2019-04-13 17:55发布

问题:

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"

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

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

回答1:

I solved it using Ansible shell module and tower-cli. I Know that create a ansible module is better than it, but to a fast solution...

  • hosts: awx
    vars:
    tasks:
    • name: Create Inventory
      tower_inventory:
      name: "Foo Inventory"
      description: "Our Foo Cloud Servers"
      organization: "Default"
      state: present
    • name: Create Group
      tower_group:
      inventory: "Foo Inventory"
      name: Testes
      register: fs_group
    • name: Create Host
      tower_host:
      inventory: "Foo Inventory"
      name: "host"
      register: fs_host
    • name: Associate host group
      shell: tower-cli host associate --host "{{fs_host.id}}" --group "> {{fs_group.id}}"