vars in inventory overwrite each other

I’m fairly new to Ansible, so maybe what I’m trying to do is bad anyway.

I use ansible to deploy all of our web applications. I use a special deploy playbook for every app. Now every app can be deployed to staging or production. For each of those environments I use a separate inventory such as `envs/staging’. Inside the inventory I use one group for every app and set certain environment specific variables in this way"

`
[app1]
app.example.com
[app1:vars]
app-port=3000
app_domain=sub1.example.com

[app2]
app.example.com
[app2:vars]
app-port=3001
app_domain=sub2.example.com
`

I would deploy an app like that: ansible-playbook -i envs/staging deploys/app2.yml -K

Now this doesn’t work unfortunately. In my example it always takes the configuration of app1, even if I deploy app2. In my playbook for app2 I do have the following line: hosts: app2.

So my question would be, why is the playbook for app2 taking configuration options defined for app1? Do I use inventory variables wrong? Do otherpeople use ansible to deploy apps and how do they keep the environments separated?

UnlockYourBrain GmbH
c/o Smart Mobile Factory GmbH

Französische Str. 24

10117 Berlin

Germany

www.unlockyourbrain.com

Download at Google Play

Like us on Facebook

Geschäftsführer: Felix Nienstädt, Simon Smend

Unternehmenssitz: Berlin

Eingetragen am Handelsgericht Charlottenburg unter HRB 149905 B

Groups at the same level of depth will definitely override each other.

What you need to do is consider using role variables, or vars_files, or “vars”, etc, or name the variable something like “app2_port”

There are some good tips on http://docs.ansible.com/playbooks_variables.html

Okay, thanks for this info. I worked around it by creating a group_var file for each environment and import it into the playbook using vars_file. It works good enough for me.

I’m fairly new to Ansible, so maybe what I’m trying to do is bad anyway.

I use ansible to deploy all of our web applications. I use a special deploy playbook for every app. Now every app can be deployed to staging or production. For each of those environments I use a separate inventory such as `envs/staging’. Inside the inventory I use one group for every app and set certain environment specific variables in this way"

`
[app1]
app.example.com
[app1:vars]
app-port=3000
app_domain=sub1.example.com

[app2]
app.example.com
[app2:vars]
app-port=3001
app_domain=sub2.example.com
`

I would deploy an app like that: ansible-playbook -i envs/staging deploys/app2.yml -K

Now this doesn’t work unfortunately. In my example it always takes the configuration of app1, even if I deploy app2. In my playbook for app2 I do have the following line: hosts: app2.

So my question would be, why is the playbook for app2 taking configuration options defined for app1? Do I use inventory variables wrong? Do otherpeople use ansible to deploy apps and how do they keep the environments separated?

UnlockYourBrain GmbH
c/o Smart Mobile Factory GmbH

Französische Str. 24

10117 Berlin

Germany

www.unlockyourbrain.com

Download at Google Play

Like us on Facebook

Geschäftsführer: Felix Nienstädt, Simon Smend

Unternehmenssitz: Berlin

Eingetragen am Handelsgericht Charlottenburg unter HRB 149905 B

UnlockYourBrain GmbH
c/o Smart Mobile Factory GmbH

Französische Str. 24

10117 Berlin

Germany

www.unlockyourbrain.com

Download at Google Play

Like us on Facebook

Geschäftsführer: Felix Nienstädt, Simon Smend

Unternehmenssitz: Berlin

Eingetragen am Handelsgericht Charlottenburg unter HRB 149905 B

Definitely do not import things out of “group_vars” with the vars_files directive, it would be ok to keep it in a different directory, but you are going to find this would confuse most ansible users using the directory for two purposes.

yeah, good point. Thanks.