Leverage host variables in ansible Tower/awx

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)

Missing the three dashes??

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] => {
“VirtualMachine_Storage_Name”: “VARIABLE IS NOT DEFINED!: ‘VirtualMachine_Storage_Name’ is undefined”
}
ok: [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] => {
“inventory_hostname”: “bigip2-test1.example1.com
}
ok: [bigip1-test1.example1.com] => {
“inventory_hostname”: “bigip1-test1.example1.com
}

(attachments)

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

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 (57.3 KB)


What happens when you remove the connection: local

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> 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’, ‘/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 ?

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.

@arun hotra Did you get it figured out?

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

Thanks for the help

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]: 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”
}

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
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
But I don't know why you want to do that.

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”