Site icon UnixArena

How to update the Ansible Tower Inventory using API?

Tower API - Ansible - UnixArena

Tower API - Ansible - UnixArena

Ansible Tower offers various REST API to integrate with other tools. I had come across the scenario where the playbook needs to update tower inventory after provisioning cloud instance. tower-cli is one of the methods to update/import inventory on ansible tower but at the time of writing this article, tower-cli is not supported by Red Hat. After a few research, I found the way to update inventory using API and also adding the host in multiple groups.

Check out the various Tower API here

Add a new ansible client on Tower using API:

1. Login to Ansible tower and navigate to inventory to find out the group id. Just hover the mouse on the inventory group to know the group id.

2. Let’s assume that group id is 1. Here is the ansible task block which will add the passing variables in tower inventory. Return code is going to be 201 for adding the host for the first time on the tower. Create the playbook with the following content. (add_ansible_tower.yaml). Replace tower host URL with yours.

---
- 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: son
       validate_certs: no

3. Execute the playbook with required variables.

# ansible-playbook add_ansible_tower.yaml -e FQDN=test4.example.com -e GROUP1=1

How to add the host into multiple ansible tower groups?

1. Here is the block of code which adds the hosts in to multiple group. URI status code should be 204 for invoking api for second time to add the host in to other groups.

---
- hosts: localhost

  tasks:
   - name: Update Ansible Tower inventory to the first group
     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: no
   
   - name: Update Ansible Tower inventory to other groups
     uri:
      url: https://unixarena.tower.com/api/v2/groups/{{item}}/hosts/
      user: admin
      password: test@123
      method: POST
      body: '{ "name" : "{{FQDN}}" }'
      force_basic_auth: yes
      status_code: 204
      body_format: json
      validate_certs: no
     with_items:
            - "{{GROUP2}}"
            - "{{GROUP3}}"

2. Execute the playbook to add the new ansible client in to multiple groups.

$ ansible-playbook add_hosts_multiple_towergroup.yaml -e  FQDN=test1.example.com -e GROUP1=1 -e GROUP2=4 -e GROUP3=6

Struggling to find group id’s ?

Option: 1

Navigate to API URL for groups to find the group id. https://tower.example.com/api/v2/groups/

Option: 2

Login to tower and hover the mouse on top the group to know the group id.

Tower inventory – Find Group ID
Exit mobile version