1

I've got an ansible setup that looks like this

inventories/
    sandbox1
    group_vars/
       all
       sandbox1
pb/
    pb1/
        run_pb1.yml

I've got roles and other stuff too, but I think this is enough to capture the gist of what I need to do.

I've got a variable foo that I am defining inside inventories/group_vars/all AND inventories/group_vars/sandbox1

Now my issue is that whenever I call

ansible-playbook -i inventories/sandbox1 pb/pb1/run_pb1.yml

The playbook is run with the value of foo that is contained in inventories/group_vars/all... put another way, foo is not being picked up from the inventories/group_vars/sandbox1

Is this the correct way to implement an inventory specific variable? I want to avoid creating mulitple playbooks for something as simple as a variable change... ie, I do not want to do something like this inside the "all" file

default_foo: bar
sandbox1_foo: baz

along with multiple playbooks, which would be the same except for the variable they reference.

Is there a way to do what I am asking?

0

Host variables overriding group variables (see docs) but you should define them in /host_vars directory. So eg using following setup:

.
├── group_vars
│   └── all.yml # foo: var_from_all
├── hosts
├── host_vars
│   └── host1.yml # foo: var_from_host1
└── test.yml

You will get:

$ ansible-playbook -i hosts test.yml 

PLAY [host1,host2] *************************************************************

TASK [debug] *******************************************************************
ok: [host1] => {
    "foo": "var_from_host1"
}
ok: [host2] => {
    "foo": "var_from_all"
}

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.