# Host\_vars not working as expected

**URL:** <https://forum.ansible.com/t/host-vars-not-working-as-expected/3199>\
**Category:** Get Help\
**Tags:** playbook, getting-started, ee\
**Created:** [January 8, 2024, 12:27pm UTC](https://forum.ansible.com/t/host-vars-not-working-as-expected/3199 "2024-01-08T12:27:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![msmith](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/msmith/32/1934_2.png) [@msmith](https://forum.ansible.com/u/msmith)\
**Post date:** [January 8, 2024, 12:27pm UTC](https://forum.ansible.com/t/host-vars-not-working-as-expected/3199/1 "2024-01-08T12:27:59Z")

</div>

Hi, I’m very new to Ansible and I’m sure this is me being stupid, but I’m struggling to get host\_vars and group\_vars working.

I’m trying to configure a few Aruba switches, and obviously some settings are group or host specific, so I want to define them in group\_vars and host\_vars.

As an example, this works perfectly in my playbook -

- file = configswitches.yml

```auto
- hosts: core
  collections:
    - arubanetworks.aoscx

  tasks:
    - name: VSX configuration 
      aoscx_vsx:
        isl_port: lag99
        keepalive_peer_ip: 10.0.0.2
        keepalive_src_ip: 10.0.0.1
        system_mac: 00:FF:11:EE:22:DD

```

But if I move the IPs and MAC to the host\_vars file then they are ignored -

- file = host\_vars/core01.yml

```auto
---
      aoscx_vsx:
        keepalive_peer_ip: 10.0.0.2
        keepalive_src_ip: 10.0.0.1
        system_mac: 00:FF:11:EE:22:DD

```

- file = hosts.yml

```auto
---
core:
 hosts:
  core01:
   ansible_host: 10.1.1.227
  core02:
   ansible_host: 10.1.1.228

```

Can anyone please point me in the right direction?

Thanks!

Mike

---

<div class="post-metadata">

**Author:** ![shertel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/shertel/32/1241_2.png) [@shertel](https://forum.ansible.com/u/shertel)\
**Post date:** [January 8, 2024, 12:53pm UTC](https://forum.ansible.com/t/host-vars-not-working-as-expected/3199/2 "2024-01-08T12:53:01Z")

</div>

Once the variables are in host\_vars/group\_vars, they need to still be included for the specific tasks.

```auto
  tasks:
    - name: VSX configuration 
      aoscx_vsx:
        keepalive_peer_ip: "{{ aoscx_vsx.keepalive_peer_ip }}"
        ...

```

If you’re trying to reduce some repetitive option passing, you might be interested in using [`module_defaults`](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_module_defaults.html), which can be set on a play or block.

```auto
- hosts: core
  module_defaults:
    aoscx_vsx: "{{ aoscx_vsx }}" # variable can be defined in host_vars
  tasks:
    aoscx_vsx:
      # Add individual arguments here to override or add to the defaults

```

---

<div class="post-metadata">

**Author:** ![msmith](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/msmith/32/1934_2.png) [@msmith](https://forum.ansible.com/u/msmith)\
**Post date:** [January 8, 2024, 1:18pm UTC](https://forum.ansible.com/t/host-vars-not-working-as-expected/3199/3 "2024-01-08T13:18:45Z")

</div>

Thanks Shertel, that’s awesome!

I needed the fully qualified name for the module\_defaults in the playbook -

```auto
  module_defaults:
    arubanetworks.aoscx.aoscx_vsx: "{{ aoscx_vsx }}" # variable can be defined in host_vars

```

My only issue now is that I have tried to move the isl\_port to group\_vars and leave the others in host\_vars and it appears to be ignoring the group\_vars/core.yml file.

This works -

```auto
cat host_vars/core01.yml
---
      aoscx_vsx:
        isl_port: lag99
        device_role: primary
        keepalive_peer_ip: 10.0.0.2
        keepalive_src_ip: 10.0.0.1
        system_mac: 00:FF:11:EE:22:DD

```

but this doesn’t -

```auto
cat host_vars/core01.yml
---
      aoscx_vsx:
        device_role: primary
        keepalive_peer_ip: 10.0.0.2
        keepalive_src_ip: 10.0.0.1
        system_mac: 00:FF:11:EE:22:DD

cat group_vars/core.yml
---
      aoscx_vsx:
        isl_port: lag99

```

---

<div class="post-metadata">

**Author:** ![akira6592](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/akira6592/32/222_2.png) [@akira6592](https://forum.ansible.com/u/akira6592)\
**Post date:** [January 8, 2024, 1:39pm UTC](https://forum.ansible.com/t/host-vars-not-working-as-expected/3199/4 "2024-01-08T13:39:22Z")

</div>

Hello @msmith

The `host_vars` has higher precedence than the `group_vars`.

[https://docs.ansible.com/ansible/latest/playbook\_guide/playbooks\_variables.html#understanding-variable-precedence](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_variables.html#understanding-variable-precedence)

So, the variable `aoscx_vsx` in group\_vars is ignored.

How about using flatten variable definition as follows?

cat host\_vars/core01.yml

```yaml
---
device_role: primary
keepalive_peer_ip: 10.0.0.2
keepalive_src_ip: 10.0.0.1
system_mac: 00:FF:11:EE:22:DD

```

cat group\_vars/core.yml

```yaml
---
isl_port: lag99

```

---

<div class="post-metadata">

**Author:** ![kurokobo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kurokobo/32/182_2.png) [@kurokobo](https://forum.ansible.com/u/kurokobo)\
**Post date:** [January 8, 2024, 2:32pm UTC](https://forum.ansible.com/t/host-vars-not-working-as-expected/3199/5 "2024-01-08T14:32:42Z")

</div>

Hi, let me show you an alternative approach; using [ansible.builtin.combine](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/combine_filter.html) filter.

Define both group\_vars and host\_vars with different names, and define new combined variable in host\_vars:

```yaml
# group_vars/core.yml
---
_group_aoscx_vsx:
  isl_port: lag99

```

```yaml
# host_vars/core01.yml
---
_host_aoscx_vsx:
  device_role: primary
  keepalive_peer_ip: 10.0.0.2
  keepalive_src_ip: 10.0.0.1
  system_mac: 00:FF:11:EE:22:DD

aoscx_vsx: "{{ _group_aoscx_vsx | ansible.builtin.combine(_host_aoscx_vsx, recursive=true) }}"

```

Now we can use `aoscx_vsx` that have complete values from group\_vars and host\_vars:

```yaml
  module_defaults:
    arubanetworks.aoscx.aoscx_vsx: "{{ aoscx_vsx }}"

```

---

<div class="post-metadata">

**Author:** ![msmith](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/msmith/32/1934_2.png) [@msmith](https://forum.ansible.com/u/msmith)\
**Post date:** [January 8, 2024, 2:57pm UTC](https://forum.ansible.com/t/host-vars-not-working-as-expected/3199/6 "2024-01-08T14:57:06Z")

</div>

Thanks everyone, you’ve really helped me out! 😃

---

<div class="post-metadata">

**Author:** ![msmith](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/msmith/32/1934_2.png) [@msmith](https://forum.ansible.com/u/msmith)\
**Post date:** [February 23, 2024, 2:14pm UTC](https://forum.ansible.com/t/host-vars-not-working-as-expected/3199/7 "2024-02-23T14:14:25Z")

</div>

Hi all, I figured out another way to do this by just using different variable names and then passing them back through, eg for ‘aoscx\_vsx.keepalive\_peer\_ip’ I just create a new var at the most specific level in the group\_vars or host\_vars file and then map it in the more general vars file. I think this is what akira suggested with flattened variables, but I was too much of a noob to understand that a few weeks ago!

```auto
host_vars/core01.yml
        ka_peer_ip: 10.0.0.2
        ka_src_ip: 10.0.0.1

host_vars/core02.yml
        ka_peer_ip: 10.0.0.1
        ka_src_ip: 10.0.0.2

group_vars/switches.yml
   aoscx_vsx:
        isl_port: lag99
        keepalive_peer_ip: "{{ ka_peer_ip }}"
        keepalive_src_ip: "{{ ka_src_ip }}"
        system_mac: 00:FF:11:EE:22:DD

```

Thanks again everyone for your help, I’m learning 🙂

I do, however, have a new question on the same subject.

This time it’s for Dell OS10 switches rather than Aruba and I’m configuring VLANs and ports as shown below. I define ‘nicprefix’ so I can change this on different switches, in this case the servers all have their OCP NICs connected to one switch and their PCI NICs connected to another, so I set ‘nicprefix: PCI’ on the second switch. So the snippet below works perfectly to create a couple of VLANs and set the appropriate interfaces to be tagged -

```auto
host_vars/labsw1.yml
	nicprefix: OCP
	portgroup01range: 1/1/1-1/1/8
	portgroup01desc: "{{ nicprefix }}1-VM-Mgmt-vMotion"
	#
	os10_vlan:
	  # VM Mgmt
	  vlan 50:
		vlan-name: V0050-ESXi
		state: present
		tagged_members:
		  - port: range ethernet {{ portgroup01range }},{{ portgroup09range }}
			desc: V0050-ESXi
			state: present
			access_vlan: "false"
	  # VM vMotion
	  vlan "{{vlan-vMotion}}" :
		state: present
		tagged_members:
		  - port: range ethernet {{ portgroup01range }}
			desc: V0203-vMotion
			state: present
			access_vlan: "false"

# Dell S4148 switch -
labsw1# show vlan
Codes: * - Default VLAN, M - Management VLAN, R - Remote Port Mirroring VLANs,
       @ - Attached to Virtual Network, P - Primary, C - Community, I - Isolated,
       S - VLAN-Stack VLAN
Q: A - Access (Untagged), T - Tagged
    NUM Status Description Q Ports
    50 Inactive T Eth1/1/1-1/1/10
    203 Inactive T Eth1/1/1-1/1/8

```

But when I come to set the interface descriptions the yml below does not work and I get the error shown below -

```auto
host_vars/labsw1.yml
	#
	os10_interface:
	  range ethernet "{{ portgroup01range }}":
		desc: "{{ portgroup01desc }}"

	TASK [dellemc.os10.os10_interface : Provisioning interface configuration for os10] ********************************************************************************************
	fatal: [labsw1]: FAILED! => {
		"changed": false,
		"command": "interface range ethernet\"{{",
		"rc": -32603
	}

	MSG:

	interface range "ethernet{{
	% Error: Illegal parameter.
	labsw1(conf-range-eth1/1/28-1/1/30)#

```

but this works fine -

```auto
	os10_interface:
	  range ethernet 1/1/1-1/1/8:
		desc: "{{ portgroup01desc }}"

```

I get the same if I try to replace VLAN numbers with vars -

```auto
	vlan_vMotion: 203
	os10_vlan:
	  # VM vMotion
	  vlan "{{vlan_vMotion}}" :
		state: present
		tagged_members:
		  - port: range ethernet {{ portgroup01range }}
			desc: V0203-vMotion
			state: present
			access_vlan: "false"

	TASK [dellemc.os10.os10_vlan : Provisioning VLAN configuration for os10] ******************************************************************************************************
	fatal: [labsw1]: FAILED! => {
		"changed": false,
		"command": "interface vlan\"{{",
		"rc": -32603
	}

	MSG:

	interface vlan"{{
	% Error: Illegal parameter.
	labsw1(conf-if-po-47)#

```

Does anyone have any ideas?

Thanks,

Mike

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [February 23, 2024, 7:24pm UTC](https://forum.ansible.com/t/host-vars-not-working-as-expected/3199/8 "2024-02-23T19:24:12Z")

</div>

See this rather [long discussion](https://github.com/ansible/ansible/pull/19664).
