retrieving the inventory name from the job template

Hi Ansible/AWX Community!

I’m working on a project that builds an on-premesis virtual machine then adds it to a tower inventory.

I have a job template that runs against “Inventory X”. How can I retrieve the name of the tower inventory this job was run against (Inventory X), so that I can add this new host to that inventory?

Hello,

Isn’t the tower_inventory_name or tower_inventory_id available?

https://docs.ansible.com/ansible-tower/latest/html/userguide/job_templates.html

Regards,

Nuno Jordão

Hmm -
If I use
‘{{ lookup(“env”, “tower_inventory_name”) }}’
I get

TASK [debug] *******************************************************************
18:59:49

11
ok: [localhost] => {

12
“msg”: “”

13
}

14

If I use just {{ tower_inventory_name }}
I get

The task includes an option with an undefined variable. The error was: ‘tower_inventory_name’ is undefined

Apologies if I’m doing something stupid I’m only about a month into ansible

It should work without the lookup, it’s intended here:

https://github.com/ansible/awx/blob/devel/awx/main/models/unified_jobs.py#L1442

and I can easily find it in the Ansible Tower docs. We even have a unit test for it as awx_inventory_name.

Alan
github: AlanCoding

my playbook: —

  • hosts: localhost
    connection: local
    gather_facts: yes

tasks:

  • debug:
    var: extra_vars, tower_inventory_name
    verbosity: 1
    -truncated-

The output:

TASK [debug] *******************************************************************
20:27:21

11
ok: [localhost] => {

12
“extra_vars, tower_inventory_name”: “(AnsibleUndefined, AnsibleUndefined)”

13
}

14

I cannot reproduce. Here is a playbook I wrote for testing this:

https://github.com/AlanCoding/utility-playbooks/blob/master/meta_vars.yml

I see the inventory name in its output.

It is expected that the workflow name is missing (because it was not run in a workflow). Everything else seems to be functioning correctly.

This functionality was added in 6.1.0, so maybe you’re running an old version?