# Awx.inventory adding variables issue

**URL:** <https://forum.ansible.com/t/awx-inventory-adding-variables-issue/7945>\
**Category:** Get Help\
**Tags:** awx, collections\
**Created:** [August 19, 2024, 1:45pm UTC](https://forum.ansible.com/t/awx-inventory-adding-variables-issue/7945 "2024-08-19T13:45:27Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![richf](https://avatars.discourse-cdn.com/v4/letter/r/74df32/32.png) [@richf](https://forum.ansible.com/u/richf)\
**Post date:** [August 19, 2024, 1:45pm UTC](https://forum.ansible.com/t/awx-inventory-adding-variables-issue/7945/1 "2024-08-19T13:45:27Z")

</div>

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).

```auto
- 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

---

<div class="post-metadata">

**Author:** ![Klaas](https://avatars.discourse-cdn.com/v4/letter/k/ecc23a/32.png) [@Klaas](https://forum.ansible.com/u/Klaas)\
**Post date:** [August 19, 2024, 4:35pm UTC](https://forum.ansible.com/t/awx-inventory-adding-variables-issue/7945/2 "2024-08-19T16:35:29Z")

</div>

I think you want to do something like this:

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

```

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [August 19, 2024, 8:55pm UTC](https://forum.ansible.com/t/awx-inventory-adding-variables-issue/7945/3 "2024-08-19T20:55:53Z")

</div>

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:

```yaml
- 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.

---

<div class="post-metadata">

**Author:** ![richf](https://avatars.discourse-cdn.com/v4/letter/r/74df32/32.png) [@richf](https://forum.ansible.com/u/richf)\
**Post date:** [August 20, 2024, 6:23am UTC](https://forum.ansible.com/t/awx-inventory-adding-variables-issue/7945/4 "2024-08-20T06:23:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Klaas](https://avatars.discourse-cdn.com/v4/letter/k/ecc23a/32.png) [@Klaas](https://forum.ansible.com/u/Klaas)\
**Post date:** [August 20, 2024, 6:59am UTC](https://forum.ansible.com/t/awx-inventory-adding-variables-issue/7945/5 "2024-08-20T06:59:39Z")

</div>

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

```auto
    v1: "SomeValue"
    v2: "SomeOtherValue

```

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

---

<div class="post-metadata">

**Author:** ![richf](https://avatars.discourse-cdn.com/v4/letter/r/74df32/32.png) [@richf](https://forum.ansible.com/u/richf)\
**Post date:** [August 20, 2024, 7:18am UTC](https://forum.ansible.com/t/awx-inventory-adding-variables-issue/7945/6 "2024-08-20T07:18:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Klaas](https://avatars.discourse-cdn.com/v4/letter/k/ecc23a/32.png) [@Klaas](https://forum.ansible.com/u/Klaas)\
**Post date:** [August 20, 2024, 7:31am UTC](https://forum.ansible.com/t/awx-inventory-adding-variables-issue/7945/7 "2024-08-20T07:31:19Z")

</div>

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 😉

---

<div class="post-metadata">

**Author:** ![Klaas](https://avatars.discourse-cdn.com/v4/letter/k/ecc23a/32.png) [@Klaas](https://forum.ansible.com/u/Klaas)\
**Post date:** [August 20, 2024, 7:32am UTC](https://forum.ansible.com/t/awx-inventory-adding-variables-issue/7945/8 "2024-08-20T07:32:28Z")

</div>

[https://docs.ansible.com/ansible/latest/playbook\_guide/playbooks\_advanced\_syntax.html#unsafe-or-raw-strings](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_advanced_syntax.html#unsafe-or-raw-strings)

---

<div class="post-metadata">

**Author:** ![richf](https://avatars.discourse-cdn.com/v4/letter/r/74df32/32.png) [@richf](https://forum.ansible.com/u/richf)\
**Post date:** [August 20, 2024, 7:47am UTC](https://forum.ansible.com/t/awx-inventory-adding-variables-issue/7945/9 "2024-08-20T07:47:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [September 19, 2024, 7:48am UTC](https://forum.ansible.com/t/awx-inventory-adding-variables-issue/7945/10 "2024-09-19T07:48:08Z")

</div>

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