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/
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}}/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#/
I still cannot figure out how to “undo” the POST I executed to add the host to the group.
Please help
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) 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.
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 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.
-The AWX Team
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:
- 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
- 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”
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
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”