7

On Ansible 2.2,

I have an Ansible hosts file:

[webserver]
aegir.dev

[hostmaster]
aegir.dev

I have two group_vars/ files:

# group_vars/webserver.yml
my_var:
  - vagrant

and

# group_vars/hostmaster.yml
my_var:
  - vagrant
  - aegir

and the playbook:

- hosts: webserver
  tasks:
    - debug: var=my_var

- hosts: hostmaster
  tasks:
    - debug: var=my_var

Output:

PLAY [webserver] ***************************************************************

TASK [setup] *******************************************************************
ok: [aegir.dev]

TASK [debug] *******************************************************************
ok: [aegir.dev] => {
    "my_var": [
        "vagrant",
        "aegir"
    ]
}

PLAY [hostmaster] **************************************************************

TASK [setup] *******************************************************************
ok: [aegir.dev]

TASK [debug] *******************************************************************
ok: [aegir.dev] => {
    "my_var": [
        "vagrant",
        "aegir"
    ]
}

Why both webserver and hostmaster use the variables from hostmaster.yml?

Probably I'm not using the group_vars correctly but how I can fix that?

EDIT

The real case scenario is that I have the same role running on two groups, one is webserver that actually contains 4 generic servers and the hostmaster group that is a webserver + extra configuration on the same role (the user aegir exists only on aegir.dev and not on the other 3 web-servers)

  • 1
    Looks broken by design to me. The main problem is, that Ansible has no proper scoping at all. It is the direct consequence of Ansible's idea to build a programming language on top of a JSON data structure. – ceving Jan 18 '17 at 13:59
4

This is expected behaviour. See documentation:

Within any section, redefining a var will overwrite the previous instance. If multiple groups have the same variable, the last one loaded wins. If you define a variable twice in a play’s vars: section, the 2nd one wins.

  • 2
    Thanks! That's true, but how I can overcome this problem? I don't want to assign them directly in the playbook (after every task) because it will make a mess... – tvl Nov 21 '16 at 15:13
  • You have to refactor your variables. But your question lacks use case details to give any advices... – Konstantin Suvorov Nov 21 '16 at 15:18
  • 1
    The real case scenario is that I have the same role running on two groups, one is webserver that actually contains 4 generic servers and the hostmaster group that is a webserver + extra configuration on the same role (the user aegir exists only on aegir.dev and not on the other 3 web-servers) – tvl Nov 21 '16 at 16:01
  • I'd split role into generic and hostmater. Or you can try to adopt something like this in your role (define what users should be created, based on group names). – Konstantin Suvorov Nov 21 '16 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.