Some time has gone by since my article about Getting started with Ansible AWX (Open Source Tower version) , and I wanted to continue focusing on AWX and show how to automate the configuration of an AWX Tower server.
Before we configure AWX we should install the tower-cli. You can find more information about the Tower CLI here: https://github.com/ansible/tower-cli. I also recommend having a look at the tower-cli documentation: https://tower-cli.readthedocs.io/en/latest/
sudo pip install ansible-tower-cli
The tower-cli is very useful when you want to monitor the running jobs. The web console is not that great when it comes to large playbook and is pretty slow at showing the running job state. See below the basic configuration before you start using the tower-cli:
berndonline@lab:~$ tower-cli config host 94.130.51.22 Configuration updated successfully. berndonline@lab:~$ tower-cli login admin Password: { "id": 1, "type": "o_auth2_access_token", "url": "/api/v2/tokens/1/", "created": "2018-09-15T17:41:23.942572Z", "modified": "2018-09-15T17:41:23.955795Z", "description": "Tower CLI", "user": 1, "refresh_token": null, "application": null, "expires": "3018-01-16T17:41:23.937872Z", "scope": "write" } Configuration updated successfully. berndonline@lab:~$
But now let’s continue and show how we can use the tower-cli to configure and monitor Ansible AWX Tower.
Create a project:
tower-cli project create --name "My Project" --description "My project description" --organization "Default" --scm-type "git" --scm-url "https://github.com/ansible/ansible-tower-samples"
Create an inventory:
tower-cli inventory create --name "My Inventory" --organization "Default"
Add hosts to an inventory:
tower-cli host create --name "localhost" --inventory "My Inventory" --variables "ansible_connection: local"
Create credentials:
tower-cli credential create --name "My Credential" --credential-type "Machine" --user "admin"
Create a Project Job Template:
tower-cli job_template create --name "My Job Template" --project "My Project" --inventory "My Inventory" --job-type "run" --credential "My Credential" --playbook "hello_world.yml" --verbosity "default"
After we successfully created everything let’s now run the job template and monitor the output via the tower-cli:
tower-cli job launch --job-template "My Job Template" tower-cli job monitor <ID>
Command line output:
berndonline@lab:~$ tower-cli job launch --job-template "My Job Template" Resource changed. == ============ =========================== ======= ======= id job_template created status elapsed == ============ =========================== ======= ======= 26 15 2018-10-12T12:22:48.599748Z pending 0.0 == ============ =========================== ======= ======= berndonline@lab:~$ tower-cli job monitor 26 ------Starting Standard Out Stream------ PLAY [Hello World Sample] ****************************************************** TASK [Gathering Facts] ********************************************************* ok: [localhost] TASK [Hello Message] *********************************************************** ok: [localhost] => { "msg": "Hello World!" } PLAY RECAP ********************************************************************* localhost : ok=2 changed=0 unreachable=0 failed=0 ------End of Standard Out Stream-------- Resource changed. == ============ =========================== ========== ======= id job_template created status elapsed == ============ =========================== ========== ======= 26 15 2018-10-12T12:22:48.599748Z successful 8.861 == ============ =========================== ========== ======= berndonline@lab:~$
With the tower-cli commands we can write a simple playbook using the Ansible Shell module.
Playbook site.yml:
--- - hosts: localhost gather_facts: 'no' tasks: - name: Add tower project shell: | tower-cli project create \ --name "My Project" \ --description "My project description" \ --organization "Default" \ --scm-type "git" \ --scm-url "https://github.com/ansible/ansible-tower-samples" - name: Add tower inventory shell: | tower-cli inventory create \ --name "My Inventory" \ --organization "Default" - name: Add host to inventory shell: | tower-cli host create \ --name "localhost" \ --inventory "My Inventory" \ --variables "ansible_connection: local" - name: Add credential shell: | tower-cli credential create \ --name "My Credential" \ --credential-type "Machine" \ --user "admin" - name: wait 15 seconds to pull project SCM content wait_for: timeout=15 delegate_to: localhost - name: Add job template shell: | tower-cli job_template create \ --name "My Job Template" \ --project "My Project" \ --inventory "My Inventory" \ --job-type "run" \ --credential "My Credential" \ --playbook "hello_world.yml" \ --verbosity "default"
Let’s run the playbook:
berndonline@lab:~/awx-provision$ ansible-playbook site.yml PLAY [localhost] ************************************************************************************************************************************************** TASK [Add tower project] ****************************************************************************************************************************************** changed: [localhost] TASK [Add tower inventory] **************************************************************************************************************************************** changed: [localhost] TASK [Add host to inventory] ************************************************************************************************************************************** changed: [localhost] TASK [Add credential] ********************************************************************************************************************************************* changed: [localhost] TASK [wait 15 seconds to pull project SCM content] **************************************************************************************************************** ok: [localhost -> localhost] TASK [Add job template] ******************************************************************************************************************************************* changed: [localhost] PLAY RECAP ******************************************************************************************************************************************************** localhost : ok=6 changed=5 unreachable=0 failed=0 berndonline@lab:~/awx-provision$
If you like this article, please share your feedback and leave a comment.