Need to extrapolate variable listed in host_vars that is pulled form another variable file.

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] *************************************************************************************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************************************************************************************************
ok: [vm1]

TASK [vm_config : shell] *******************************************************************************************************************************************************************************************
changed: [vm1] => (item=vm1) => {“changed”: true, “cmd”: “echo vm1 1.1.1.1 ‘{{ hardware1[‘vm1’][‘connection_ip’]}}’ 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 {{ 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] *************************************************************************************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************************************************************************************************
ok: [vm1]

TASK [vm_config : shell] *******************************************************************************************************************************************************************************************
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?

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…