Awx.inventory adding variables issue

Hi,
I am struggling with a playbook to interact with my inventory using the awx.awx collection:

When I specify a variable that relates to a previous one it fails as the variable is not defined (i.e. in the context of the play).

- name: Create Inventory
  awx.awx.inventory:
    name: MyInv
    variables:
      Variable1: "SomeValue"
      Variable2: "SomeOtherValue/{{ Variable1}}"

i.e. the play doesn’t need to know the value of Variable1, but when I run a jobtemplate in that inventory Variable2 should get populated correctly. .

the error reported being:
“The task includes an option with an undefined variable. . . Variable1 is undefined”

Is it possible to escape the content of the variables: section without impacting the resultant variables in the inventory.

ansible 2.15.12, target awx 23.6.0

I think you want to do something like this:

- ansible.builtin.set_fact:
    Variable1: SomeValue
- name: Create Inventory
  awx.awx.inventory:
    name: MyInv
    variables:
      Variable1: "{{ Variable1 }}"
      Variable2: "SomeOtherValue/{{ Variable1}}"

I think @Klaas is on the right track. Even though the dict you are defining is called variables, these aren’t “Ansible variables” in the sense that they participate in playbook lazy evaluation, can be referenced in/by other variables, etc.

But I don’t think you need to go to the extreme of using set_fact either. You might consider this:

- name: Create Inventory
  awx.awx.inventory:
    name: MyInv
    variables: "{{ foo }}"
  vars:
    v1: "SomeValue"
    v2: "SomeOtherValue/{{ v1 }}"
    foo:
      Variable1: "{{ v1 }}"
      Variable2: "{{ v2 }}"

Note: nothing in foo can reference anything else defined in foo or you’ll hit infinite recursion. But as long as everything in foo is some combination of literal and/or references to vars defined outside of foo you should be okay.

thanks, I’d looked that, but I don’t want the variables to be evaluated by the playbook, I want the resultant var in awx to be “{{ SomeOtherValue/{{ variable1 }}”

so when a job is run against any override at the time requires only variable1 to be included.

I am not sure that works, but you can just do something like this in inventory:

    v1: "SomeValue"
    v2: "SomeOtherValue

and then when you use it
"{{ v2 }}/{{ v1 }}"

indeed, but the issue is that I want the entry in the inventory variables to be “{{ v1 }}/{{ v2 }}” in that instance, but not “SomeValue/SomeOtherValue”, but the playbook is evaluating “{{ v1 }}/{{ v2 }}” when what i want is to have the text “{{ v1 }}/{{ v2 }}”, so that they get evaluated when a job is run using that inventory.

if I edit the inventory in AWX manually I can enter that, and I have some terraform code that does push the entries in that way, but I want now to use ansible in order to populate the inventory instead.

so if you want to write the literal "{{ v1 }}" into inventory I think you can use something like
!unsafe "{{ v1 }}"

but I am not sure if that is by design if that works within the inventory :wink:

2 Likes

https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_advanced_syntax.html#unsafe-or-raw-strings

1 Like

Just what I was after and unable to find the search terms to find! thanks!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.