# Need to extrapolate variable listed in host\_vars that is pulled form another variable file.

**URL:** <https://forum.ansible.com/t/need-to-extrapolate-variable-listed-in-host-vars-that-is-pulled-form-another-variable-file/29267>\
**Category:** Ansible Project\
**Created:** [December 14, 2018, 7:00pm UTC](https://forum.ansible.com/t/need-to-extrapolate-variable-listed-in-host-vars-that-is-pulled-form-another-variable-file/29267 "2018-12-14T19:00:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric\_Brewer](https://avatars.discourse-cdn.com/v4/letter/e/a5b964/32.png) [@Eric\_Brewer](https://forum.ansible.com/u/Eric_Brewer)\
**Post date:** [December 14, 2018, 7:00pm UTC](https://forum.ansible.com/t/need-to-extrapolate-variable-listed-in-host-vars-that-is-pulled-form-another-variable-file/29267/1 "2018-12-14T19:00:21Z")

</div>

I am working on a project where we need to be able to hand out a yml file that will be filled in, and that will be the input for the server specific and custom information. In this configuration, we need to configure pieces of hardware and vms, and all of that information needs to come off of that yml file. When trying to do this, I hit a weird scenario where the variable listed in the host file is just printed literally. I need to be able to reference the variable file in each host\_var, seeing as the number of pieces will vary from deployment to deployment, and the number of vms on each piece of hardware can differ from one another, as well as from deployment to deployment. Its only an issue when I need to use the same variable from all of the host\_vars (in this instance to create a /etc/hosts file) and feeding in what hosts are available to do this from, from the hosts.ini. It just prints the variable from the host\_var file literally, where as every other way of referencing it translates the variable from the file into its intended value. Any one have any ideas? In my example below I don’t intend to use that command to edit the hosts file, but to show the different ways the system is correctly interpreting the variable, and the one way it is not. (I will probably use a template or a lineinfile module, not sure yet…)

`ansible 2.7.4 config file = /etc/ansible/ansible.cfg configured module search path = [u'/home/ebrewer/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python2.7/dist-packages/ansible executable location = /usr/bin/ansible python version = 2.7.12 (default, Nov 12 2018, 14:36:49) [GCC 5.4.0 20160609]`

Here is my playbook:

**test.yml**

`

- name: Configure VMs  
hosts:
- vm\_group1  
gather\_facts: yes  
vars\_files:
- vars/csm\_vars.yml  
roles:
- vm\_config  
become\_user: root  
become: true  
become\_method: sudo

`

**vars/csm\_vars.yml**

`hardware1: vm1: hostname: "vm1" connection_ip: "1.1.1.1" vm2: hostname: "vm2" connection_ip: "2.2.2.2"`

**inventory/hosts.ini**

`  
[vm\_group1]  
vm1 ansible\_host=“{{ hardware1[‘vm1’][‘connection\_ip’]}}”

[vm\_group1:vars]  
ansible\_ssh\_common\_args=‘-o StrictHostKeyChecking=no’  
ansible\_user=user  
ansible\_ssh\_pass=“xxxx”  
ansible\_become\_pass=“xxxx”

[vm\_group2]  
#vm2 ansible\_host=“{{ hardware1[‘vm1’][‘connection\_ip’]}}”

[vm\_group2:vars]  
ansible\_ssh\_common\_args=‘-o StrictHostKeyChecking=no’  
ansible\_user=user  
ansible\_ssh\_pass=“xxxx”  
ansible\_become\_pass=“xxxx”

`

**host\_vars/vm1.yml**

`  
hostname: “{{ hardware1[‘vm1’][‘hostname’] }}”  
connection\_ip: “{{ hardware1[‘vm1’][‘connection\_ip’]}}”

`

**host\_vars/vm2.yml**

`hostname: "{{ hardware1['vm2']['hostname'] }}" connection_ip: "{{ hardware1['vm2']['connection_ip']}}"`

**roles/vm\_config/main.yml**

`

- shell: “echo {{ item }} {{ hardware1[‘vm1’][‘connection\_ip’] }} ‘{{ hostvars[item].connection\_ip }}’ {{ hostvars[item].inventory\_hostname }}”  
with\_items: “{{ groups.all }}”  
`

**Actual output:**

`  
root@laptop:~# ansible-playbook test.yml -i inventory/Customer/hosts.ini -v

PLAY [Move common image files] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*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: “0:00:00.002270”, “end”: “2018-12-14 17:13:58.261286”, “item”: “vm1”, “rc”: 0, “start”: “2018-12-14 17:13:58.259016”, “stderr”: “”, “stderr\_lines”: , “stdout”: “vm1 1.1.1.1 {{ hardware1[vm1][connection\_ip]}} vm1”, “stdout\_lines”: [“vm1 1.1.1.1 {{ hardware1[vm1][connection\_ip]}} vm1”]}  
changed: [vm1] =\> (item=vm2) =\> {“changed”: true, “cmd”: “echo vm2 1.1.1.1 ‘{{ hardware1[‘vm2’][‘connection\_ip’]}}’ vm2”, “delta”: “0:00:00.002268”, “end”: “2018-12-14 17:13:59.006689”, “item”: “vm2”, “rc”: 0, “start”: “2018-12-14 17:13:59.004421”, “stderr”: “”, “stderr\_lines”: , “stdout”: “vm2 1.1.1.1 {{ hardware1[vm2][connection\_ip]}} vm2”, “stdout\_lines”: [“vm2 1.1.1.1 {{ hardware1[vm2][connection\_ip]}} vm2”]}

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
vm1 : ok=2 changed=1 unreachable=0 failed=0  
`

**Intended output:**

`  
root@laptop:~# ansible-playbook test.yml -i inventory/Customer/hosts.ini -v

PLAY [Move common image files] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*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*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: [vm1] =\> (item=vm1) =\> {“changed”: true, “cmd”: “echo vm1 192.168.34.65 1.1.1.1 vm1”, “delta”: “0:00:00.002270”, “end”: “2018-12-14 17:13:58.261286”, “item”: “vm1”, “rc”: 0, “start”: “2018-12-14 17:13:58.259016”, “stderr”: “”, “stderr\_lines”: , “stdout”: “vm1 1.1.1.1 1.1.1.1 vm1”, “stdout\_lines”: [“vm1 1.1.1.1 {{ hardware1[vm1][connection\_ip]}} vm1”]}  
changed: [vm1] =\> (item=vm2) =\> {“changed”: true, “cmd”: “echo vm2 192.168.34.65 2.2.2.2 vm2”, “delta”: “0:00:00.002268”, “end”: “2018-12-14 17:13:59.006689”, “item”: “vm2”, “rc”: 0, “start”: “2018-12-14 17:13:59.004421”, “stderr”: “”, “stderr\_lines”: , “stdout”: “vm2 1.1.1.1 2.2.2.2 vm2”, “stdout\_lines”: [“vm2 1.1.1.1 {{ hardware1[vm2][connection\_ip]}} vm2”]}

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
vm1 : ok=2 changed=1 unreachable=0 failed=0  
`

So I am not getting any errors, it’s just interpreting the one thing I need to reference from the host\_vars from input provided by the with\_items literally instead of interpolating it. Any ideas on how to force that last step?

---

<div class="post-metadata">

**Author:** ![Eric\_Brewer](https://avatars.discourse-cdn.com/v4/letter/e/a5b964/32.png) [@Eric\_Brewer](https://forum.ansible.com/u/Eric_Brewer)\
**Post date:** [December 14, 2018, 7:22pm UTC](https://forum.ansible.com/t/need-to-extrapolate-variable-listed-in-host-vars-that-is-pulled-form-another-variable-file/29267/2 "2018-12-14T19:22:56Z")

</div>

And please forgive that I forgot to scrub one of the 192 addresses out of my desired output, those 192.168.34.65 addresses should read as 1.1.1.1 in my desired output. Long day…
