Using dynamic variables with Ansible pull

21 views
Skip to first unread message

Sam Sen

unread,
Aug 10, 2016, 10:28:30 AM8/10/16
to Ansible Project
I have a role that pulls in the correct variable depending on the type of server (eg web vs perl). If I set the variable within an inventory file, I can't get my task to access the variable in question. I think my issue is the FQDN isn't being used.

# Inventory file

[web_servers]
myweb001.phl.domain.local
myweb004.phl.domain.local
myweb005.phl.domain.local

[perl_servers]
myperl011.phl.domain.local
myperl001.phl.domain.local
myperl010.phl.domain.local

[web_servers:vars]
server_type=web
some_random_variable=20

[perl_servers:vars]
server_type=perl
some_random_variable=40

# task

- hosts: localhost

tasks:
  - debug: var={{ server_type }}

# output

PLAY [localhost]    ***************************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [debug] *******************************************************************
fatal: [localhost]: FAILED! => {"failed": true, "msg": "'server_type' is undefined"}

NO MORE HOSTS LEFT *************************************************************
to retry, use: --limit @test.retry

PLAY RECAP *********************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1

Kai Stian Olstad

unread,
Aug 10, 2016, 11:27:04 AM8/10/16
to ansible...@googlegroups.com
On 10. aug. 2016 16:28, Sam Sen wrote:
> I have a role that pulls in the correct variable depending on the type of
> server (eg web vs perl). If I set the variable within an inventory file, I
> can't get my task to access the variable in question. I think my issue is
> the FQDN isn't being used.
>
> # Inventory file
>
> [web_servers]
> myweb001.phl.domain.local
> myweb004.phl.domain.local
> myweb005.phl.domain.local
>
> [perl_servers]
> myperl011.phl.domain.local
> myperl001.phl.domain.local
> myperl010.phl.domain.local
>
> [web_servers:vars]
> server_type=web
> some_random_variable=20
>
> [perl_servers:vars]
> server_type=perl
> some_random_variable=40
>
>
> # task
>
> - hosts: localhost

Your playbook is working against localhost.

>
> tasks:
> - debug: var={{ server_type }}
>
>
> # output
>
> PLAY [localhost] ***************************************************************
>
> TASK [setup] *******************************************************************
> ok: [localhost]
>
> TASK [debug] *******************************************************************
> fatal: [localhost]: FAILED! => {"failed": true, "msg": "'server_type' is undefined"}

Since you inventory file does not define server_type for localhost this
is as aspected.

--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages