W
W
wisebit2019-11-08 14:08:43
Ansible
wisebit, 2019-11-08 14:08:43

Strange behavior of Ansible, a bug or I didn't get it right?

Good afternoon. I started using Ansible actively and ran into a problem. For some reason, vars from one group are distributed to others, ignoring inheritance.

There is such an inventory file

---
all:
  children:
    location1:
      children:
        debian:
          hosts:
            server1:
              ansible_host: 192.168.0.1
              ansible_user: root
      vars:
        ansible_ssh_common_args: '-o ProxyCommand="ssh -W %h:%p -q [email protected]"'

    location2:
      children:
        debian:
          hosts:
            server2:
              ansible_host: 192.168.0.2
              ansible_user: root


the output of ansible-inventory -i test.yml --vars --list is

{
    "_meta": {
        "hostvars": {
            "server1": {
                "ansible_host": "192.168.0.1", 
                "ansible_ssh_common_args": "-o ProxyCommand=\"ssh -W %h:%p -q [email protected]\"", 
                "ansible_user": "root"
            }, 
            "server2": {
                "ansible_host": "192.168.0.2", 
                "ansible_ssh_common_args": "-o ProxyCommand=\"ssh -W %h:%p -q [email protected]\"", 
                "ansible_user": "root"
            }
        }
    }, 
    "all": {
        "children": [
            "location1", 
            "location2", 
            "ungrouped"
        ]
    }, 
    "debian": {
        "hosts": [
            "server1", 
            "server2"
        ]
    }, 
    "location1": {
        "children": [
            "debian"
        ]
    }, 
    "location2": {
        "children": [
            "debian"
        ]
    }
}


It's all so bad with Ansible inheritance, as I understand it? And are there ways to overcome this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Shitskov, 2019-11-08
@Zarom

Why this happens - Inventory has a flat structure. Essentially you only have hosts and groups. When expanding the hierarchy you described, ansible finds a group twice with the same name - debian. And this group is a child of both location1 and location2, so the variables for the hosts of those groups are merged.
How to get around this situation - think for yourself how it will be easier for you. For example, I would make different inventory files for each location. Or, changed the hierarchy to all-> debian->location1->hosts

W
wisebit, 2019-11-11
@wisebit

decided for myself

---
all:
  children:
    location1:
      children:
        debian1:
          hosts:
            server1:
              ansible_host: 192.168.0.1
              ansible_user: root
      vars:
        ansible_ssh_common_args: '-o ProxyCommand="ssh -W %h:%p -q [email protected]"'

    location2:
      children:
        debian2:
          hosts:
            server2:
              ansible_host: 192.168.0.2
              ansible_user: root

and in playbooks indicate if necessary like this debian *
I hope they will finish the code with normal inheritance

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question