• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

UnixArena

  • Home
  • kubernetes
  • DevOps
    • Terraform
    • Jenkins
    • Docker
    • Openshift
      • OKD
    • Ansible engine
    • Ansible Tower
      • AWX
    • Puppet
  • Cloud
    • Azure
    • AWS
    • Openstack
    • Docker
  • VMware
    • vCloud Director
    • VMware-Guests
    • Vcenter Appliance 5.5
    • vC OPS
    • VMware SDDC
    • VMware vSphere 5.x
      • vSphere Network
      • vSphere DS
      • vShield Suite
    • VMware vSphere 6.0
    • VSAN
    • VMware Free Tools
  • Backup
    • Vembu BDR
    • Veeam
    • Nakivo
    • Azure Backup
    • Altaro VMBackup
    • Spinbackup
  • Tutorials
    • Openstack Tutorial
    • Openstack Beginner’s Guide
    • VXVM-Training
    • ZFS-Tutorials
    • NetApp cDot
    • LVM
    • Cisco UCS
    • LDOM
    • Oracle VM for x86
  • Linux
    • How to Articles
    • Q&A
    • Networking
    • RHEL7
  • DevOps Instructor-led Training
  • Contact

How to update the Ansible Tower Inventory using API?

June 6, 2020 By Cloud_Devops 1 Comment

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
Tower inventory – Find Group ID

Filed Under: Ansible engine, Ansible Tower, Automation Tagged With: Ansible, Ansible Tower

Reader Interactions

Comments

  1. Russell Cecala says

    May 3, 2023 at 6:50 am

    Nice! How can I remove a host from a group?

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Follow UnixArena

  • Facebook
  • LinkedIn
  • Twitter

Copyright © 2025 · UnixArena ·

Go to mobile version