# Programmatically add and remove host to an inventory group?

**URL:** <https://forum.ansible.com/t/programmatically-add-and-remove-host-to-an-inventory-group/37956>\
**Category:** AWX Project\
**Created:** [May 3, 2023, 1:26am UTC](https://forum.ansible.com/t/programmatically-add-and-remove-host-to-an-inventory-group/37956 "2023-05-03T01:26:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![russell.cecala](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/russell.cecala/32/1028_2.png) [@russell.cecala](https://forum.ansible.com/u/russell.cecala)\
**Post date:** [May 3, 2023, 1:26am UTC](https://forum.ansible.com/t/programmatically-add-and-remove-host-to-an-inventory-group/37956/1 "2023-05-03T01:26:27Z")

</div>

I figured out how to add an existing host to an existing group from this we page:  
[https://www.unixarena.com/2020/06/how-to-update-the-ansible-tower-inventory-using-api.html/](https://www.unixarena.com/2020/06/how-to-update-the-ansible-tower-inventory-using-api.html/)

I used a version of the playbook that was posted.

— - hosts: localhost tasks: - name: Update Ansible Tower inventory uri: url: [https://tower.example.com/api/v2/groups/{{GROUP1}](https://tower.example.com/api/v2/groups/%7B%7BGROUP1%7D)}/hosts/ user: admin password: test@123 method: POST body: ‘{ “name” : “{{FQDN}}” }’ force\_basic\_auth: yes status\_code: 201 body\_format: json validate\_certs: noIt works great for adding a host to a group. But how would I remove the host from the group? I can’t just change POST to DELETE. And if I look at the endpoint docs here: [https://docs.ansible.com/ansible-tower/latest/html/towerapi/api\_ref.html#/](https://docs.ansible.com/ansible-tower/latest/html/towerapi/api_ref.html#/)

I still cannot figure out how to “undo” the POST I executed to add the host to the group.

Please help 🙂

---

<div class="post-metadata">

**Author:** ![russell.cecala](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/russell.cecala/32/1028_2.png) [@russell.cecala](https://forum.ansible.com/u/russell.cecala)\
**Post date:** [May 3, 2023, 2:10am UTC](https://forum.ansible.com/t/programmatically-add-and-remove-host-to-an-inventory-group/37956/2 "2023-05-03T02:10:13Z")

</div>

I forgot to mention that the swagger doc ([https://docs.ansible.com/ansible-tower/latest/html/towerapi/api\_ref.html#/Groups/Groups\_groups\_hosts\_create](https://docs.ansible.com/ansible-tower/latest/html/towerapi/api_ref.html#/Groups/Groups_groups_hosts_create)) does say this:

Add Hosts for a Group:

Make a POST request to this resource with only an id field to associate an  
existing host with this group.

Remove Hosts from this Group:

Make a POST request to this resource with id and disassociate fields to  
remove the host from this group  
without deleting the host.

But I still don’t understand what I need to do to remove a host from a group. 😕

---

<div class="post-metadata">

**Author:** ![AWX\_Project](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@AWX\_Project](https://forum.ansible.com/u/AWX_Project)\
**Post date:** [May 3, 2023, 6:22pm UTC](https://forum.ansible.com/t/programmatically-add-and-remove-host-to-an-inventory-group/37956/3 "2023-05-03T18:22:25Z")

</div>

You may also want to check out the awx.awx collection.  
This is a maintained ansible collection alongside the AWX project with modules for doing tasks like this.  
Foe example you could use the awx.awx.host module to add/remove a host from AWX. See [https://docs.ansible.com/ansible/latest/collections/awx/awx/host\_module.html](https://docs.ansible.com/ansible/latest/collections/awx/awx/host_module.html) after that you could use the awx.awx.group module to add the host to a group [https://docs.ansible.com/ansible/latest/collections/awx/awx/group\_module.html](https://docs.ansible.com/ansible/latest/collections/awx/awx/group_module.html).

-The AWX Team

---

<div class="post-metadata">

**Author:** ![russell.cecala](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/russell.cecala/32/1028_2.png) [@russell.cecala](https://forum.ansible.com/u/russell.cecala)\
**Post date:** [May 3, 2023, 7:22pm UTC](https://forum.ansible.com/t/programmatically-add-and-remove-host-to-an-inventory-group/37956/4 "2023-05-03T19:22:33Z")

</div>

Yes awx.awx was what I tried first. I forgot why I didn’t go down that route. I think maybe my client is using an older version of python on centos7. Anyways I was able to cobble together a Job Template/Playbook and Role that works.

## Here is the playbook:—

- name: Update Inventory  
hosts: localhost  
gather\_facts: False roles:
- role: roles/update\_inventory

#### Use that playbook in an AWX Job Template

- The job template needs to prompt the user for “ **Inventory** ” on launch.

> Respond with the inventory the hosts you wish to update are in. This will provide the value to the special AWX variable **tower\_inventory\_id**.

- The job template needs to prompt the user for “ **Variables** ” on launch.

> Example:
> 
> * * *
> 
> update\_ati\_fqdns:
> 
> - [host1.example.com](http://host1.example.com)
> - [host2.example.com](http://host2.example.com)  
> ansible\_connection: ‘local’

- You will need a survey for your Job Template
- Admin Username: admin
- Admin User Password: xxxxxxx
- URL for AWX REST API GROUP endpoint: [http://awx.example.com/api/v2/groups](http://awx.example.com/api/v2/groups)
- update\_ati\_group: integer
- Action: pulldown [REMOVE\_FROM\_GROUP, ADD\_TO\_GROUP]  
Here is the role:

bash-5.1$ cat defaults/main.yml — awx\_base\_url: “[https://awx.example.com](https://awx.example.com)”

bash-5.1$ cat tasks/main.yml — - name: ansible.builtin.include\_tasks: test\_block\_loop.yml loop: “{{ update\_ati\_fqdns }}” bash-5.1$ cat tasks/test\_block\_loop.yml — - name: Get Host ID uri: url: “{{ awx\_base\_url }}/api/v2/inventories/{{ tower\_inventory\_id }}/hosts/?name={{ item }}” method: GET user: “{{ admin\_username }}” password: “{{ admin\_password }}” force\_basic\_auth: yes validate\_certs: no headers: Content-Type: “application/json” register: host\_result failed\_when: host\_result.status != 200 or host\_result.json.count != 1 - name: “Set host id for {{ item }}” set\_fact: body: “{ "id" : {{ host\_result.json.results[0].id | int }} }” - name: “Set host id for {{ item }}” set\_fact: body: “{ "disassociate" : {{ host\_result.json.results[0].id | int }}, "id" : {{ host\_result.json.results[0].id | int }} }” when: action == “REMOVE\_FROM\_GROUP” - name: Update Ansible Tower inventory uri: url: “{{ update\_ati\_groups\_api }}/{{ update\_ati\_group }}/hosts/” user: “{{ admin\_username }}” password: “{{ admin\_password }}” method: “POST” body: “{{ body }}” force\_basic\_auth: yes status\_code: 204 body\_format: json validate\_certs: no

---

<div class="post-metadata">

**Author:** ![area51coder](https://avatars.discourse-cdn.com/v4/letter/a/a88e57/32.png) [@area51coder](https://forum.ansible.com/u/area51coder)\
**Post date:** [May 4, 2023, 6:45am UTC](https://forum.ansible.com/t/programmatically-add-and-remove-host-to-an-inventory-group/37956/5 "2023-05-04T06:45:35Z")

</div>

i have tried to test below code, but it is not working. can you suggest more about it…

“msg”: “The conditional check ‘action == \“REMOVE\_FROM\_GROUP\”’ failed. The error was: error while evaluating conditional (action == \“REMOVE\_FROM\_GROUP\”): ‘action’ is undefined\n\nThe error appears to be in ‘/tmp/bwrap\_1748\_nsontv7t/awx\_1748\_29wci7g0/project/addhostnew.yml’: line 23, column 9, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: \“Set host id for {{ item }}\”\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n with\_items:\n - {{ foo }}\n\nShould be written as:\n\n with\_items:\n - \”{{ foo }}\“\n”
