CaptRisky
CaptRisky

Reputation: 801

Ansible: merging of server vars from 2 groups not working

Im using ansible 2.9.6 and I have a single server for now but it is within 2 groups in the host file. I have configured ufw for each group which has different firewall rules to apply, and when i run the playbook for 'db_servers' group its grabing the vars (ufw_rules) from the 'web_servers' group. Considering the server has the same name I would expect the vars to merge at least. but if i give the same servers a different name 'node_w' and 'node_d' in their groups, it will work with the corresponding vars for that group. I have no host_vars set to cause issues.

[web_servers]
node1 ansible_host=123.123.123.1

[db_servers]
node1 ansible_host=123.123.123.1

web_servers.yml
ufw_rules:
    - { service: http, port: 80, interface: '{{ ufw_default_public_interface }}', direction: '{{ ufw_service_default_direction }}', proto: tcp }
    - { service: https, port: 443, interface: '{{ ufw_default_public_interface }}', direction: '{{ ufw_service_default_direction }}', proto: tcp }

db_servers.yml
ufw_rules:
    - { service: mysql, port: 3306, interface: '{{ ufw_default_private_interface }}', direction: '{{ ufw_service_default_direction }}' }

Any ideas as to why its not getting the corresponding group vars, or at least not merging them both with the same name for server?

Upvotes: 0

Views: 161

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 68074

Q: "Expect the vars to merge."

A: See DEFAULT_HASH_BEHAVIOUR. For example

shell> ANSIBLE_HASH_BEHAVIOUR=merge ansible-playbook playbook.yml

will merge dictionaries. Quoting:

"it does not affect variables whose values are scalars (integers, strings) or arrays"

Upvotes: 1

Related Questions