Ansible Tower REST API with Python (tower-cli / python)

  • 1
    Share

Introduction

Ansible Tower (license’s product) is a web-based solution by RedHat that makes Ansible even more easy to use for IT teams of all kinds. It’s designed to be the hub for all of your automation tasks. Ansible Tower centralize and control your IT infrastructure with a visual dashboard, role-based access control, job scheduling, integrated notifications and graphical inventory management… and more. Find out more about Tower features Ansible Tower webpage

Ansible Tower’s REST API and CLI make it easy to embed Ansible Tower into existing tools and processes.  In this tutorial we will discuss invoke or calls Ansible Tower’s REST API with Python.

We have find out two ways to invoke/calls Ansible Tower’s REST API with Python
1. tower-cli – The tower-cli is an open source and command line tool for Ansible Tower.
2. Custom Code – Invoke/calls Ansible Tower’s REST API with Python urllib2 library.

 

1. tower-cli

tower-cli is a command line tool for Ansible Tower. It allows Tower commands to be easily run from the UNIX command line. It can also be used as a client library for other python apps, or as a reference for others developing API interactions with Tower’s REST API.

tower-cli sends commands to the Tower API. It is capable of retrieving, creating, modifying, and deleting most objects within Tower.

A few potential uses include:

  • Launching playbook runs (for instance, from Jenkins, TeamCity, Bamboo, etc)
  • Checking on job statuses
  • Rapidly creating objects like organizations, users, teams, and more

1.1 tower-cli Installation

tower-cli is available as a package on PyPI.

The preferred way to install is through pip:

[ansible@localhost ~]$ pip install ansible-tower-cli

For more information on tower-cli, refer to the project page at: https://github.com/ansible/tower-cli/

1.2 tower-cli Configuration

tower-cli can edit its own configuration using its py client/binary script or users can directly edit the configuration file.

Set configuration with tower-cli config

The preferred way to set configuration is with the tower-cli config command.

[ansible@localhost ~]$ tower-cli config key value

# Generally need to set at least following three configuration options – host, username, and password
[ansible@localhost ~]$ tower-cli config host tower.localhost.com
[ansible@localhost ~]$ tower-cli config username admin
[ansible@localhost ~]$ tower-cli config password tower123

Write to the config files directly.

Edit configuration file directly. A configuration file is a simple file with keys and values, separated by : or =:

host: tower.localhost.com
username: admin
password: tower123

tower-cli File Locations
The order of precedence for configuration file locations is as follows, from least to greatest:

  • internal defaults
  • /etc/awx/tower_cli.cfg (written using tower-cli config –global)
  • ~/.tower_cli.cfg (written using tower-cli config)
  • run-time parameters

1.3 tower-cli Usage

tower-cli invocation generally follows this format:

$ tower-cli {resource} {action} ...

The resource is a type of object within Tower (a noun), such as userorganizationjob_template, etc.;

# List all users.
[ansible@localhost ~]$ tower-cli user list

# Launch a job.
[ansible@localhost ~]$ tower-cli job launch --job-template=33

# Monitor a job.
[ansible@localhost ~]$ tower-cli job monitor 33

For more usage examples please visit official website

 

2. Custom Code / Wrapper

We have implemented custom python script to connect Ansible Tower as we need to integrate Ansible Tower API data with ELK for data analysis. For more information on it, refer to the project page at: https://github.com/avinash6784/ansible-tower-elk/blob/master/ansible_tower/ansible_tower.py

 

References:
http://docs.ansible.com/ansible-tower/latest/html/towerapi/tower_cli.html
http://docs.ansible.com/ansible-tower/latest/html/towerapi/index.html
https://github.com/ansible/tower-cli

Avinash Pawar

DevOps Practitioner interested in learning new technologies and interested in sharing the knowledge with others.

More Posts - Website

Follow Me:
TwitterFacebookLinkedInGoogle Plus

Avinash Pawar

DevOps Practitioner interested in learning new technologies and interested in sharing the knowledge with others.

Leave a Reply