Leverage host variables in ansible Tower/awx

268 views
Skip to first unread message

arun hotra

unread,
Nov 26, 2020, 1:48:38 PM11/26/20
to AWX Project
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


tower.png

mike.charchuk

unread,
Nov 26, 2020, 2:14:10 PM11/26/20
to arun hotra, AWX Project
Missing the three dashes??

---


I have no problem using mine but I do keep tje three dashes in there.



Sent from my Galaxy
--
You received this message because you are subscribed to the Google Groups "AWX Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to awx-project...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/awx-project/16612b39-25b9-4c95-9fe0-b70fd9680ae7n%40googlegroups.com.

arun hotra

unread,
Nov 26, 2020, 2:47:12 PM11/26/20
to mike.charchuk, AWX Project
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"
}
tower-2.png

Gerald Dykeman

unread,
Nov 26, 2020, 3:03:29 PM11/26/20
to arun hotra, mike.charchuk, AWX Project
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



--
Gerald Dykeman, CCIE #36354

Sr. Solution Architect

Red Hat North America Public Sector


M: (703)-304-2036    

arun hotra

unread,
Nov 26, 2020, 4:05:55 PM11/26/20
to Gerald Dykeman, mike.charchuk, AWX Project
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]




bigip1_vars.json
inventory_ansible.png
directory_structure_ansible.png

mike.charchuk

unread,
Nov 26, 2020, 4:38:52 PM11/26/20
to arun hotra, Gerald Dykeman, AWX Project
What happens when you remove the connection: local

arun hotra

unread,
Nov 27, 2020, 9:14:40 AM11/27/20
to mike.charchuk, Gerald Dykeman, AWX Project
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 ?

Gerald Dykeman

unread,
Nov 27, 2020, 11:53:33 AM11/27/20
to arun hotra, mike.charchuk, AWX Project
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.

Gerald Dykeman

unread,
Nov 28, 2020, 1:11:29 PM11/28/20
to arun hotra, mike.charchuk, AWX Project
@arun hotra Did you get it figured out?

arun hotra

unread,
Nov 28, 2020, 6:45:40 PM11/28/20
to Gerald Dykeman, mike.charchuk, AWX Project
I was able to see the variables when I tested today, not sure what changed.

Thanks for the help


arun hotra

unread,
Nov 29, 2020, 4:14:10 PM11/29/20
to Gerald Dykeman, mike.charchuk, AWX Project
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"
}

Chiku

unread,
Nov 29, 2020, 4:49:31 PM11/29/20
to awx-p...@googlegroups.com
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.


On 29/11/2020 22:13, arun hotra wrote:
> 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>]:
> <gdyk...@redhat.com <mailto:gdyk...@redhat.com>> wrote:
>
> @arun hotra <mailto:arun...@gmail.com> Did you get it
> figured out?
>
> On Fri, Nov 27, 2020 at 11:52 AM Gerald Dykeman
> <gdyk...@redhat.com <mailto:gdyk...@redhat.com>> wrote:
>
> 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.
>
> On Fri, Nov 27, 2020 at 9:18 AM arun hotra
> <arun...@gmail.com <mailto:arun...@gmail.com>> wrote:
>
> 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 ?
>
> On Thu, Nov 26, 2020 at 4:38 PM mike.charchuk
> <mike.c...@gmail.com
> <mailto:mike.c...@gmail.com>> wrote:
>
> What happens when you remove the connection: local
>
>
>
> Sent from my Galaxy
>
>
> -------- Original message --------
> From: arun hotra <arun...@gmail.com
> <mailto:arun...@gmail.com>>
> Date: 2020-11-26 13:05 (GMT-08:00)
> To: Gerald Dykeman <gdyk...@redhat.com
> <mailto:gdyk...@redhat.com>>
> Cc: "mike.charchuk" <mike.c...@gmail.com
> <mailto:mike.c...@gmail.com>>, AWX Project
> <awx-p...@googlegroups.com
> <mailto:awx-p...@googlegroups.com>>
> Subject: Re: [awx-project] Leverage host variables
> in ansible Tower/awx
>
> 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]
>
>
>
>
>
> On Thu, Nov 26, 2020 at 3:03 PM Gerald Dykeman
> <gdyk...@redhat.com <mailto:gdyk...@redhat.com>>
> wrote:
>
> 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
>
> On Thu, Nov 26, 2020 at 2:47 PM arun hotra
> <arun...@gmail.com
> <mailto:arun...@gmail.com>> wrote:
>
> 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>"
> }
>
> On Thu, Nov 26, 2020 at 2:14 PM
> mike.charchuk <mike.c...@gmail.com
> <mailto:awx-project...@googlegroups.com>.
> <https://groups.google.com/d/msgid/awx-project/16612b39-25b9-4c95-9fe0-b70fd9680ae7n%40googlegroups.com?utm_medium=email&utm_source=footer>.
>
> --
> You received this message because you are
> subscribed to the Google Groups "AWX
> Project" group.
> To unsubscribe from this group and stop
> receiving emails from it, send an email to
> awx-project...@googlegroups.com
> <mailto:awx-project...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/awx-project/CAN1mJZHFd5GdR4%3Dv2F%2BNVazEoMQAwYMxkDo1Z6Oj3U8RUc8n6w%40mail.gmail.com
> <https://groups.google.com/d/msgid/awx-project/CAN1mJZHFd5GdR4%3Dv2F%2BNVazEoMQAwYMxkDo1Z6Oj3U8RUc8n6w%40mail.gmail.com?utm_medium=email&utm_source=footer>.
>
>
>
> --
> GeraldDykeman, CCIE #36354
>
> Sr. Solution Architect
>
> Red Hat North America Public Sector
> <https://www.redhat.com/>
>
>
> M: (703)-304-2036 <tel:(703)-304-2036>
>
> <https://www.redhat.com/>
>
> --
> You received this message because you are subscribed
> to the Google Groups "AWX Project" group.
> To unsubscribe from this group and stop receiving
> emails from it, send an email to
> awx-project...@googlegroups.com
> <mailto:awx-project...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/awx-project/CAN1mJZH3ANtKaKG3AgG7MmM-M%3Da7gxQVz68Zuyocdm-T%2BQxqOA%40mail.gmail.com
> <https://groups.google.com/d/msgid/awx-project/CAN1mJZH3ANtKaKG3AgG7MmM-M%3Da7gxQVz68Zuyocdm-T%2BQxqOA%40mail.gmail.com?utm_medium=email&utm_source=footer>.
>
>
>
> --
> GeraldDykeman, CCIE #36354
>
> Sr. Solution Architect
>
> Red Hat North America Public Sector <https://www.redhat.com/>
>
>
> M: (703)-304-2036 <tel:(703)-304-2036>
>
> <https://www.redhat.com/>
>
>
>
> --
> GeraldDykeman, CCIE #36354
>
> Sr. Solution Architect
>
> Red Hat North America Public Sector <https://www.redhat.com/>
>
>
> M: (703)-304-2036 <tel:(703)-304-2036>
>
> <https://www.redhat.com/>
>
> --
> You received this message because you are subscribed to the Google
> Groups "AWX Project" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to awx-project...@googlegroups.com
> <mailto:awx-project...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/awx-project/CAN1mJZEjpXyDkfTnQ64T3kch97tDZ2w7qqezs07JipwLfwrCRg%40mail.gmail.com
> <https://groups.google.com/d/msgid/awx-project/CAN1mJZEjpXyDkfTnQ64T3kch97tDZ2w7qqezs07JipwLfwrCRg%40mail.gmail.com?utm_medium=email&utm_source=footer>.

arun hotra

unread,
Nov 29, 2020, 5:02:06 PM11/29/20
to Chiku, AWX Project
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"

To unsubscribe from this group and stop receiving emails from it, send an email to awx-project...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/awx-project/1ff4a333-259e-5075-af9e-293c1ee88984%40tsgri13.fr.
Reply all
Reply to author
Forward
0 new messages