# Leverage host variables in ansible Tower/awx

**URL:** <https://forum.ansible.com/t/leverage-host-variables-in-ansible-tower-awx/34246>\
**Category:** AWX Project\
**Tags:** vmware, awx\
**Created:** [November 26, 2020, 6:48pm UTC](https://forum.ansible.com/t/leverage-host-variables-in-ansible-tower-awx/34246 "2020-11-26T18:48:38Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![arun\_hotra](https://avatars.discourse-cdn.com/v4/letter/a/d26b3c/32.png) [@arun\_hotra](https://forum.ansible.com/u/arun_hotra)\
**Post date:** [November 26, 2020, 6:48pm UTC](https://forum.ansible.com/t/leverage-host-variables-in-ansible-tower-awx/34246/1 "2020-11-26T18:48:38Z")

</div>

Hello,

I am new to tower, I am unable to leverage host variables populated by an inventory spun up by vmware into ansible tower. (see diagram)

I have used hostvars[inventory\_hostname] and also tried directly accessing them using curly brackets, e.g. {{property\_name}} but failed.

-Arun

> **(attachments)**
>
> ![tower.png](https://us1.discourse-cdn.com/ansible/original/2X/8/8498ab5ef78af8135b1ba61254b933cf498cef6e.png)

---

<div class="post-metadata">

**Author:** ![Mike\_Charchuk](https://avatars.discourse-cdn.com/v4/letter/m/e47774/32.png) [@Mike\_Charchuk](https://forum.ansible.com/u/Mike_Charchuk)\
**Post date:** [November 26, 2020, 7:14pm UTC](https://forum.ansible.com/t/leverage-host-variables-in-ansible-tower-awx/34246/2 "2020-11-26T19:14:04Z")

</div>

Missing the three dashes??

---

<div class="post-metadata">

**Author:** ![arun\_hotra](https://avatars.discourse-cdn.com/v4/letter/a/d26b3c/32.png) [@arun\_hotra](https://forum.ansible.com/u/arun_hotra)\
**Post date:** [November 26, 2020, 7:46pm UTC](https://forum.ansible.com/t/leverage-host-variables-in-ansible-tower-awx/34246/3 "2020-11-26T19:46:57Z")

</div>

I am not sure whether that is the issue, I added the three dashes but same result.

This is the task I am running

- name: Print a host variable  
debug:  
var: VirtualMachine\_Storage\_Name

- name: Print a host variable inventory host  
debug:  
var: inventory\_hostname

TASK [buildSOT : Print a host variable] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /root/onboard-bigip/roles/buildSOT/tasks/populateVars.yaml:3  
Thursday 26 November 2020 13:43:17 -0600 (0:00:00.074) 0:00:01.144 \*\*\*\*\*  
ok: [[bigip2-test1.example1.com](http://bigip2-test1.example1.com)] =\> {  
“VirtualMachine\_Storage\_Name”: “VARIABLE IS NOT DEFINED!: ‘VirtualMachine\_Storage\_Name’ is undefined”  
}  
ok: [[bigip1-test1.example1.com](http://bigip1-test1.example1.com)] =\> {  
“VirtualMachine\_Storage\_Name”: “VARIABLE IS NOT DEFINED!: ‘VirtualMachine\_Storage\_Name’ is undefined”  
}

TASK [buildSOT : Print a host variable inventory host] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /root/onboard-bigip/roles/buildSOT/tasks/populateVars.yaml:7  
Thursday 26 November 2020 13:43:17 -0600 (0:00:00.069) 0:00:01.213 \*\*\*\*\*  
ok: [[bigip2-test1.example1.com](http://bigip2-test1.example1.com)] =\> {  
“inventory\_hostname”: “[bigip2-test1.example1.com](http://bigip2-test1.example1.com)”  
}  
ok: [[bigip1-test1.example1.com](http://bigip1-test1.example1.com)] =\> {  
“inventory\_hostname”: “[bigip1-test1.example1.com](http://bigip1-test1.example1.com)”  
}

> **(attachments)**
>
> ![tower-2.png](https://us1.discourse-cdn.com/ansible/original/2X/4/4d36910859217472a58befe26fa306601079324e.png)

---

<div class="post-metadata">

**Author:** ![Gerald\_Dykeman](https://avatars.discourse-cdn.com/v4/letter/g/a4c791/32.png) [@Gerald\_Dykeman](https://forum.ansible.com/u/Gerald_Dykeman)\
**Post date:** [November 26, 2020, 8:02pm UTC](https://forum.ansible.com/t/leverage-host-variables-in-ansible-tower-awx/34246/4 "2020-11-26T20:02:44Z")

</div>

Can you share the project directory structure and print out hostvars[inventory\_hostname] ?  
Also, feel free to try adding another var in the variables section and see if that shows up in debug

---

<div class="post-metadata">

**Author:** ![arun\_hotra](https://avatars.discourse-cdn.com/v4/letter/a/d26b3c/32.png) [@arun\_hotra](https://forum.ansible.com/u/arun_hotra)\
**Post date:** [November 26, 2020, 9:05pm UTC](https://forum.ansible.com/t/leverage-host-variables-in-ansible-tower-awx/34246/5 "2020-11-26T21:05:39Z")

</div>

Attached the dump of hostvars - I cannot seem to find any of those variables in the previous screenshot [there are 204 of those which are showing in the gui]

> **(attachments)**
>
> [bigip1\_vars.json](https://forum.ansible.com/uploads/short-url/o3XBBL7i8fXoRGqGuc1Jb8QBwFH.json) (57.3 KB)
> 
> ![inventory_ansible.png](https://us1.discourse-cdn.com/ansible/original/2X/1/1046f567648d084263301327346def2c7fc8aab9.png)  
> ![directory_structure_ansible.png](https://us1.discourse-cdn.com/ansible/original/2X/5/5fd552c1bbc77c1fdd6db99a93a83694f8b25087.png)

---

<div class="post-metadata">

**Author:** ![Mike\_Charchuk](https://avatars.discourse-cdn.com/v4/letter/m/e47774/32.png) [@Mike\_Charchuk](https://forum.ansible.com/u/Mike_Charchuk)\
**Post date:** [November 26, 2020, 9:38pm UTC](https://forum.ansible.com/t/leverage-host-variables-in-ansible-tower-awx/34246/6 "2020-11-26T21:38:46Z")

</div>

What happens when you remove the connection: local

---

<div class="post-metadata">

**Author:** ![arun\_hotra](https://avatars.discourse-cdn.com/v4/letter/a/d26b3c/32.png) [@arun\_hotra](https://forum.ansible.com/u/arun_hotra)\
**Post date:** [November 27, 2020, 2:14pm UTC](https://forum.ansible.com/t/leverage-host-variables-in-ansible-tower-awx/34246/7 "2020-11-27T14:14:20Z")

</div>

If I remove connection: local, it attempts running on the host (we dont want that) and fails. Here is the message - it just keeps retrying this

\<[bigip2-test1.example1.com](http://bigip2-test1.example1.com)\> ssh\_retry: attempt: 7, ssh return code is 255. cmd ([‘ssh’, ‘-vvv’, ‘-C’, ‘-o’, ‘ControlMaster=auto’, ‘-o’, ‘ControlPersist=1800’, ‘-o’, ‘StrictHostKeyChecking=no’, ‘-o’, ‘KbdInteractiveAuthentication=no’, ‘-o’, ‘PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey’, ‘-o’, ‘PasswordAuthentication=no’, ‘-o’, ‘ConnectTimeout=1800’, ‘-o’, ‘ControlPath=/root/.ansible/cp/03e93432ed’, ‘[bigip2-test1.example1.com](http://bigip2-test1.example1.com)’, ‘/bin/sh -c 'echo PLATFORM; uname; echo FOUND; command -v '“'”'/usr/bin/python'“'”'; command -v '“'”'python3.7'“'”'; command -v '“'”'python3.6'“'”'; command -v '“'”'python3.5'“'”'; command -v '“'”'python2.7'“'”'; command -v '“'”'python2.6'“'”'; command -v '“'”'/usr/libexec/platform-python'“'”'; command -v '“'”'/usr/bin/python3'“'”'; command -v '“'”'python'“'”'; echo ENDFOUND && sleep 0'’]…), pausing for 30 seconds

The variables are populated already as seen in the gui, but i can’t access them, so should they be locally accessible ?

---

<div class="post-metadata">

**Author:** ![Gerald\_Dykeman](https://avatars.discourse-cdn.com/v4/letter/g/a4c791/32.png) [@Gerald\_Dykeman](https://forum.ansible.com/u/Gerald_Dykeman)\
**Post date:** [November 27, 2020, 4:52pm UTC](https://forum.ansible.com/t/leverage-host-variables-in-ansible-tower-awx/34246/8 "2020-11-27T16:52:46Z")

</div>

Just to clarify… the debug playbook is being run from Tower… correct? The variables should be accessible… and this does not make sense to me.

---

<div class="post-metadata">

**Author:** ![Gerald\_Dykeman](https://avatars.discourse-cdn.com/v4/letter/g/a4c791/32.png) [@Gerald\_Dykeman](https://forum.ansible.com/u/Gerald_Dykeman)\
**Post date:** [November 28, 2020, 6:10pm UTC](https://forum.ansible.com/t/leverage-host-variables-in-ansible-tower-awx/34246/9 "2020-11-28T18:10:41Z")

</div>

[@arun hotra](mailto:arunhotra@gmail.com) Did you get it figured out?

---

<div class="post-metadata">

**Author:** ![arun\_hotra](https://avatars.discourse-cdn.com/v4/letter/a/d26b3c/32.png) [@arun\_hotra](https://forum.ansible.com/u/arun_hotra)\
**Post date:** [November 28, 2020, 11:45pm UTC](https://forum.ansible.com/t/leverage-host-variables-in-ansible-tower-awx/34246/10 "2020-11-28T23:45:24Z")

</div>

I was able to see the variables when I tested today, not sure what changed.

Thanks for the help

---

<div class="post-metadata">

**Author:** ![arun\_hotra](https://avatars.discourse-cdn.com/v4/letter/a/d26b3c/32.png) [@arun\_hotra](https://forum.ansible.com/u/arun_hotra)\
**Post date:** [November 29, 2020, 9:13pm UTC](https://forum.ansible.com/t/leverage-host-variables-in-ansible-tower-awx/34246/11 "2020-11-29T21:13:53Z")

</div>

I have a quick follow up question.

So I am running the following task which leverages the variable.

- name: Poplulate files from template  
template:  
src: “{{ lookup(‘template’,‘{{ role\_path }}/templates/test.j2’) }}}”

# src: “{{ lookup(‘template’,‘{{ role\_path }}/templates/bigip.j2’) }}}”

dest: “{{role\_path}}/tmp/repos/virtualDatacenter/{{inventory\_hostname\_short}}.json”

test.j2

{  
“base”: {  
“mgmt”: {  
“ip”:“{{ VirtualMachine\_Network0\_Address }}”  
},  
“dataCenter”: “virtualDataCenter”  
}  
}

I get an error as shown below. The host variable “VirtualMachine\_Network0\_Address” is populated - 10.254.29.109 , but not sure why it is ending up in this error.

fatal: [[bigip1-test4.example1.com](http://bigip1-test4.example1.com)]: FAILED! =\> {  
“changed”: false,  
“msg”: “Could not find or access ‘{u’base’: {u’dataCenter’: u’virtualDataCenter’, u’mgmt’: {u’ip’: u’10.254.29.109’}}}}‘\nSearched in:\n\t/tmp/awx\_100\_hes49vmf/project/roles/buildSOT/templates/{u’base’: {u’dataCenter’: u’virtualDataCenter’, u’mgmt’: {u’ip’: u’10.254.29.109’}}}}\n\t/tmp/awx\_100\_hes49vmf/project/roles/buildSOT/{u’base’: {u’dataCenter’: u’virtualDataCenter’, u’mgmt’: {u’ip’: u’10.254.29.109’}}}}\n\t/tmp/awx\_100\_hes49vmf/project/roles/buildSOT/tasks/templates/{u’base’: {u’dataCenter’: u’virtualDataCenter’, u’mgmt’: {u’ip’: u’10.254.29.109’}}}}\n\t/tmp/awx\_100\_hes49vmf/project/roles/buildSOT/tasks/{u’base’: {u’dataCenter’: u’virtualDataCenter’, u’mgmt’: {u’ip’: u’10.254.29.109’}}}}\n\t/tmp/awx\_100\_hes49vmf/project/playbooks/templates/{u’base’: {u’dataCenter’: u’virtualDataCenter’, u’mgmt’: {u’ip’: u’10.254.29.109’}}}}\n\t/tmp/awx\_100\_hes49vmf/project/playbooks/{u’base’: {u’dataCenter’: u’virtualDataCenter’, u’mgmt’: {u’ip’: u’10.254.29.109’}}}} on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote\_src option”  
}

---

<div class="post-metadata">

**Author:** ![Chiku](https://avatars.discourse-cdn.com/v4/letter/c/3ab097/32.png) [@Chiku](https://forum.ansible.com/u/Chiku)\
**Post date:** [November 29, 2020, 9:49pm UTC](https://forum.ansible.com/t/leverage-host-variables-in-ansible-tower-awx/34246/12 "2020-11-29T21:49:08Z")

</div>

I don't know what is your purpose. Is the test.j2 not in the same role? or you don't know how to use a role?  
Usually, it's just src: test.j2

About the error.  
For template module with src parameter, it expects a path. But the lookup('template'...) expands into a content.  
[https://docs.ansible.com/ansible/latest/collections/ansible/builtin/template\_module.html](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/template_module.html)  
So for what you want to do, you can use copy module and content parameter then keep your lookup('template'...)  
[https://docs.ansible.com/ansible/latest/collections/ansible/builtin/copy\_module.html](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/copy_module.html)  
But I don't know why you want to do that.

---

<div class="post-metadata">

**Author:** ![arun\_hotra](https://avatars.discourse-cdn.com/v4/letter/a/d26b3c/32.png) [@arun\_hotra](https://forum.ansible.com/u/arun_hotra)\
**Post date:** [November 29, 2020, 10:01pm UTC](https://forum.ansible.com/t/leverage-host-variables-in-ansible-tower-awx/34246/13 "2020-11-29T22:01:50Z")

</div>

Thanks for the detailed explanation. I am relatively new to ansible/tower. I changed the source as per your suggestion and got past the error.

- name: Poplulate files from template  
template:  
src: ‘bigip.j2’

# src: “{{ lookup(‘template’,‘{{ role\_path }}/templates/bigip.j2’) }}}”

dest: “{{role\_path}}/tmp/repos/virtualDataCenter/{{inventory\_hostname\_short}}.json”
