# Using dynamic variables with Ansible pull

**URL:** <https://forum.ansible.com/t/using-dynamic-variables-with-ansible-pull/22265>\
**Category:** Ansible Project\
**Created:** [August 10, 2016, 2:28pm UTC](https://forum.ansible.com/t/using-dynamic-variables-with-ansible-pull/22265 "2016-08-10T14:28:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sam\_S](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@Sam\_S](https://forum.ansible.com/u/Sam_S)\
**Post date:** [August 10, 2016, 2:28pm UTC](https://forum.ansible.com/t/using-dynamic-variables-with-ansible-pull/22265/1 "2016-08-10T14:28:29Z")

</div>

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

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

```auto
- hosts: localhost

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

```

`# output`

`

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

```

`

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 10, 2016, 3:26pm UTC](https://forum.ansible.com/t/using-dynamic-variables-with-ansible-pull/22265/2 "2016-08-10T15:26:28Z")

</div>

> 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:  
> &nbsp;&nbsp;- 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.
