0
votes
Ansible variable scope clash - Stack Overflow
Asked
Viewed 971 times
0

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, the value of foo from inventories/group_vars/sandbox1 is not overriding the "global/all" version.

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?

3
  • the group_vars folder should contain the names of groups, not inventories for adding variables. Can you share a sample of your sandbox1 inventory for further clarification?
    – Theo
    Jun 15 2016 at 17:03
  • your comment provided the solution, I was not including a group called sandbox1 in my inventory file. Thanks!
    – Zack
    Jun 15 2016 at 17:08
  • For posterity, I added my comment as an answer. :-)
    – Theo
    Jun 15 2016 at 17:23
1

The group_vars folder should contain the names of groups, not inventories for adding variables. Add a group inside your sandbox1 inventory file called "sandbox1" and it will then use those variables.

0

    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.

     
    1
    the group_vars folder should contain the names of groups, not inventories for adding variables. Can you share a sample of your sandbox1 inventory for further clarification?Theo
    your comment provided the solution, I was not including a group called sandbox1 in my inventory file. Thanks!Zack
    For posterity, I added my comment as an answer. :-)Theo

    1 Answers

    1
    votes

    The group_vars folder should contain the names of groups, not inventories for adding variables. Add a group inside your sandbox1 inventory file called "sandbox1" and it will then use those variables.