1

I'm in the process of migrating Ansible playbooks into Ansible AWX projects.

Previously I'd checkout the Ansible playbook from git, then run it from the command line.

In this specific case I have a Ansible Playbook that creates VMware virtual machines. I use the following tasks to gather information about the git repo and current git commit hash, and use this info in the VM annotations, so that it can be later used to identify the exact instructions used to create the VM.

  - name: return git commit hash
    command: git rev-parse HEAD
    register: gitCommitHash
    delegate_to: localhost
    become: false
    become_user: "{{ lookup('env','USER') }}"


  - name: get remote git repo
    command: git config --get remote.origin.url
    register: gitRemote
    delegate_to: localhost
    become: false
    become_user: "{{ lookup('env','USER') }}"

I realize that playbooks run in AWX run as the awx user.

Is there anyway, in a playbook, I can get the AWX user that is running the AWX template, and can I get the url for the Ansible AWX project?

Update

I found I can get the AWX that is running the template by using the {{awx_user_name}}, but have not yet found out how to get the git remote url of the project/playbook.

Magick
  • 4,603
  • 22
  • 66
  • 103

1 Answers1

1

I ran a job template with a simple playbook:

---
- name: Debug AWX
  hosts: all
  tasks:
    - debug:
        var: vars

And in the output, I could see these variables:

    "awx_inventory_id": 1,
    "awx_inventory_name": "Demo Inventory",
    "awx_job_id": 23,
    "awx_job_launch_type": "manual",
    "awx_job_template_id": 10,
    "awx_job_template_name": "Debug AWX",
    "awx_project_revision": "3214f37f271ad589f7a63d12c3d1ef9fa0972d91",
    "awx_user_email": "admin@example.com",
    "awx_user_first_name": "",
    "awx_user_id": 1,
    "awx_user_last_name": "",
    "awx_user_name": "admin",

So, no you don't get the AWX project url from the job. But you get the project ID!.

So you could query the API to get the job_template details, then query the project details to get the url. I would suggest to use the CLI for these steps.

Sergio
  • 61
  • 4