# \[HOSTVARS\]\[MERGED\] Unexpected behavior with merge variables of different host which are in several groups

**URL:** <https://forum.ansible.com/t/hostvars-merged-unexpected-behavior-with-merge-variables-of-different-host-which-are-in-several-groups/32675>\
**Category:** Ansible Project\
**Created:** [April 14, 2020, 9:39am UTC](https://forum.ansible.com/t/hostvars-merged-unexpected-behavior-with-merge-variables-of-different-host-which-are-in-several-groups/32675 "2020-04-14T09:39:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Baptiste\_Doublet](https://avatars.discourse-cdn.com/v4/letter/b/b77776/32.png) [@Baptiste\_Doublet](https://forum.ansible.com/u/Baptiste_Doublet)\
**Post date:** [April 14, 2020, 9:39am UTC](https://forum.ansible.com/t/hostvars-merged-unexpected-behavior-with-merge-variables-of-different-host-which-are-in-several-groups/32675/1 "2020-04-14T09:39:36Z")

</div>

Hi Everybody,

I have a problem that I can’t resolve on my own.  
I want to deploy an etcd cluster with postgresql on the same servers  
I have three servers and this is my inventory file:

```auto

all:
children:
etcd_cluster: # recommendation: 3 or 5-7 nodes
hosts:
demo_postgres1:
vars:
etcd_ip: '***'
demo_postgres2:
vars:
etcd_ip: '***'
demo_postgres3:
vars:
etcd_ip: '***'
master:
hosts:
demo_postgres1:
vars:
postgres_ip: '***'
postgresql_exists: 'false'
replica:
hosts:
demo_postgres2:
vars:
postgres_ip: '***'
postgresql_exists: 'false'
demo_postgres3:
vars:
postgres_ip: '***'
postgresql_exists: 'false'
balancers:
hosts:
demo_haproxy1:
vars:
haproxy_ip: '***'
demo_haproxy2:
vars:
haproxy_ip: '***'
demo:
vars:
ansible_ssh_common_args: "-F ../../ssh_config/demo_config"
children:
balancers:
etcd_cluster:
postgres_cluster:
children:
master:
replica:

```

I have the demo\_postgres[0-3] servers that run etcd and postgres. Just to prepare an external instance for etcd cluster, I have separated its in two distinct groups: etcd\_cluster and postgres\_cluster.  
I have etcd\_ip vars and postgres\_ip vars respectively on these groups.

My problem is when I run my playbook with hosts: etcd\_cluster and I print hostvars, I get the hostvars of postgres\_cluster:

```auto
PLAY [etcd_cluster] ***************************************************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************************************
ok: [demo_postgres1]
ok: [demo_postgres3]
ok: [demo_postgres2]

TASK [Ansible | List all known variables and facts] *******************************************************************************************************************************************
ok: [demo_postgres1] => {
"hostvars[inventory_hostname]['vars']": {
"postgres_ip": "***",
"postgresql_exists": "false"
}
}
ok: [demo_postgres2] => {
"hostvars[inventory_hostname]['vars']": {
"postgres_ip": "***",
"postgresql_exists": "false"
}
}
ok: [demo_postgres3] => {
"hostvars[inventory_hostname]['vars']": {
"postgres_ip": "***",
"postgresql_exists": "false"
}
}

```

I didn’t find any information about the merging of hostvars when the host is in multiple groups and when I have tested to remove the postgres\_cluster group, I get the correct hostvars:

```auto
PLAY [etcd_cluster] ***************************************************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************************************
ok: [demo_postgres1]
ok: [demo_postgres3]
ok: [demo_postgres2]

TASK [Ansible | List all known variables and facts] *******************************************************************************************************************************************
ok: [demo_postgres1] => {
"hostvars[inventory_hostname]['vars']": {
"etcd_ip": "***"
}
}
ok: [demo_postgres2] => {
"hostvars[inventory_hostname]['vars']": {
"etcd_ip": "***"
}
}
ok: [demo_postgres3] => {
"hostvars[inventory_hostname]['vars']": {
"etcd_ip": "***"
}
}

```

I would expect that my etcd\_ip was merged with postgres\_ip ? I can resolve this by adding the etcd\_ip in postgres\_cluster hostvars but I want to understand why merge is not working.

If you need more information, feel free to ask.

Best regards,

BDO

---

<div class="post-metadata">

**Author:** ![jean-yves](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jean-yves](https://forum.ansible.com/u/jean-yves)\
**Post date:** [April 14, 2020, 10:14am UTC](https://forum.ansible.com/t/hostvars-merged-unexpected-behavior-with-merge-variables-of-different-host-which-are-in-several-groups/32675/2 "2020-04-14T10:14:08Z")

</div>

Hi,

As I understand etc\_ip and postgresql\_ip are variable specific for each host, and so they should be in hosts parts of your inventory file (Here you are doing a mix).

postgresql\_exists can be setup in a your postgresql group, but I’m not sure if it is really necessary to have it

See there for example : [https://docs.ansible.com/ansible/latest/plugins/inventory/yaml.html](https://docs.ansible.com/ansible/latest/plugins/inventory/yaml.html)

If you want further help, please provide your playbooks used for your outputs.

And for the record, no needs to use a playbook to list variables, command “ansible-inventory” can be used successfully for this

Regards,

---

<div class="post-metadata">

**Author:** ![Baptiste\_Doublet](https://avatars.discourse-cdn.com/v4/letter/b/b77776/32.png) [@Baptiste\_Doublet](https://forum.ansible.com/u/Baptiste_Doublet)\
**Post date:** [April 15, 2020, 6:41pm UTC](https://forum.ansible.com/t/hostvars-merged-unexpected-behavior-with-merge-variables-of-different-host-which-are-in-several-groups/32675/3 "2020-04-15T18:41:51Z")

</div>

Hi,

thanks for your reply.

Yeah that’s it. etcd\_ip and postgresql\_ip are in separate host. But I don’t understand, there are already on the host part, aren’t it ?  
what is the syntax you think of ?

This is the playbook which should get the etcd\_ip

```auto

- hosts: etcd_cluster
become: true
become_method: sudo
any_errors_fatal: true
gather_facts: true
vars_files:
- vars/main.yml
- vars/system.yml
- "vars/{{ ansible_os_family }}.yml"

tasks:
- meta: flush_handlers

- import_tasks: tasks/sysctl.yml
tags: [sysctl, kernel]

- import_tasks: tasks/timezone.yml
when: timezone is defined and timezone | length > 0
ignore_errors: yes
tags: [timezone, configure]

- import_tasks: tasks/ntp.yml
when: ntp_enabled is defined and ntp_enabled == "true"
ignore_errors: yes
tags: [ntp, configure]

- import_tasks: tasks/etcd.yml
when: dcs_exists == "false" and dcs_type == "etcd"
tags: [etcd, etcd_cluster]

```

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [April 15, 2020, 6:54pm UTC](https://forum.ansible.com/t/hostvars-merged-unexpected-behavior-with-merge-variables-of-different-host-which-are-in-several-groups/32675/4 "2020-04-15T18:54:16Z")

</div>

> Hi,
> 
> thanks for your reply.
> 
> Yeah that's it. etcd\_ip and postgresql\_ip are in separate host. But I don't understand, there are already on the host  
> part, aren't it ?  
> what is the syntax you think of ?

Try to remove vars: from your inventory when you specify host variables, e.g.

etcd\_cluster: # recommendation: 3 or 5-7 nodes  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hosts:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;demo\_postgres1:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;etcd\_ip: '\*\*\*'

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Baptiste\_Doublet](https://avatars.discourse-cdn.com/v4/letter/b/b77776/32.png) [@Baptiste\_Doublet](https://forum.ansible.com/u/Baptiste_Doublet)\
**Post date:** [April 17, 2020, 2:54pm UTC](https://forum.ansible.com/t/hostvars-merged-unexpected-behavior-with-merge-variables-of-different-host-which-are-in-several-groups/32675/5 "2020-04-17T14:54:36Z")

</div>

Hi,

That did the job:  
etcd\_cluster: # recommendation: 3 or 5-7 nodes  
hosts:  
demo\_postgres1:  
etcd\_ip: ‘\*\*\*’

I don’t know why I used vars, maybe because I read a lot of ansible documentation like How to build your inventory and that cursed my brain ^^

```auto
atlanta:
  hosts:
    host1:
    host2:
  vars:
    ntp_server: ntp.atlanta.example.com
    proxy: proxy.atlanta.example.com

```

Thanks again Racke !

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [April 17, 2020, 3:00pm UTC](https://forum.ansible.com/t/hostvars-merged-unexpected-behavior-with-merge-variables-of-different-host-which-are-in-several-groups/32675/6 "2020-04-17T15:00:30Z")

</div>

> Hi,
> 
> That did the job:  
> etcd\_cluster: # recommendation: 3 or 5-7 nodes  
> hosts:  
> demo\_postgres1:  
> etcd\_ip: '\*\*\*'
> 
> I don't know why I used vars, maybe because I read a lot of ansible documentation like How to build your inventory and  
> that cursed my brain ^^

While the documentation covers a lot of good thing, it doesn't note the differences between group variables (with vars:  
) and host vars (without vars:).

In order to avoid confusion, you can use separate files for the host variables.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke
