2

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.

techraf
  • 64,883
  • 27
  • 193
  • 198
Jan Cichy
  • 21
  • 1
  • 4
  • I had the same problem and finally ended up writing a python-script using the ansible-tower-cli module where you can use group -> associate() to add hosts to groups. – Stefan Wegener Jul 23 '18 at 07:22
  • What are you talking about? What is "ansible-tower-cli module"? There is no tower-cli module among Ansible modules https://docs.ansible.com/ansible/latest/modules/list_of_all_modules.html. There is tower-cli https://docs.ansible.com/ansible-tower/latest/html/towerapi/tower_cli.html – Vladimir Botka Jul 24 '18 at 01:48
  • I am talking about tower-cli which is a "Command line tool and client library for the Ansible Tower and AWX Project's REST API" (https://github.com/ansible/tower-cli). I was using the name ansible-tower-cli as it is its name on PyPi (http://tower-cli.readthedocs.io/en/latest/install.html#installation). – Stefan Wegener Jul 24 '18 at 07:18
  • PyPi is "Python Package Index" and ansible-tower-cli is a package not a "module". – Vladimir Botka Jul 24 '18 at 07:37

2 Answers2

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}}"
Kredns
  • 36,461
  • 52
  • 152
  • 203
1

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

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).

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

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

Add the awx.awx collection to your playbook

collections:
  - awx.awx

and then use the hosts: option to tower_group:.

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

You can see a demo playbook here.

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
Josef
  • 2,869
  • 2
  • 22
  • 23
jursetto
  • 41
  • 3