Querying AWX / automation controller

I am posting this because I usually have to play with this several times whenever this problem comes up. I am not sure how often this will come up for others… but hopefully this will save someone sometime in the future :slight_smile:

There is an awesome Ansible content collection for AWX itself Awx.Awx — Ansible Documentation

One of the Lookup Plugins there is very powerful: awx.awx.controller_api lookup – Search the API for objects — Ansible Documentation

This will allow you to basically do any lookup in the API (which you can get to via https://your_awx_ip/api/v2)

Some of the documentation examples from the lookup plugin are great but they don’t contain a playbook so I thought I would show a very simple Ansible Playbook here that could run locally (on the AWX node itself).

---
- hosts: localhost
  vars: 
    desired_job_template: 'Network-Banner'
  tasks:
    - name: retrieve Network-Banner job template
      set_fact: 
        network_banner_exist: "{{ query('awx.awx.controller_api', 'job_templates', query_params={ 'name': desired_job_template }, host='localhost', username='admin', password='ansible123!', verify_ssl=False) }}"

    - name: print out network_banner_exist var
      debug: 
        msg: "{{ network_banner_exist }}"
      failed_when: network_banner_exist == []

    - name: fail when survey is not enabled
      debug: 
        msg: "Is a survey enabled for {{ desired_job_template }}? {{ network_banner_exist[0].survey_enabled }}"
      failed_when: not network_banner_exist[0].survey_enabled

This Ansible Playbook is (numbers corresponding to the tasks)

  1. Queries the job_templates, specifically looking for a job template called desired_job_template, which in my case is a job called Network-Banner
  2. Will fail if this job is empty (which is []) which means it does not exist.
  3. Will fail if this job has no survey enabled.

This is a quick way to audit AWX / automation controller to make sure jobs exist, or are configured the way you want them.

I am showing usernames and passwords here, obviously you would want to encrypt those with something like ansible-vault.

2 Likes

is it worth adding the playbook to the docs? We have some community-writer volunteers that could probably just pop it in the docs if that seems helpful…

3 Likes

Would be curious what others think, would certainly be cool to have more full playbook examples in the docs.

2 Likes