# managing multi-node environments

**URL:** <https://forum.ansible.com/t/managing-multi-node-environments/17132>\
**Category:** Ansible Project\
**Created:** [December 23, 2014, 3:22am UTC](https://forum.ansible.com/t/managing-multi-node-environments/17132 "2014-12-23T03:22:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bence\_Takacs](https://avatars.discourse-cdn.com/v4/letter/b/c6cbf5/32.png) [@Bence\_Takacs](https://forum.ansible.com/u/Bence_Takacs)\
**Post date:** [December 23, 2014, 3:22am UTC](https://forum.ansible.com/t/managing-multi-node-environments/17132/1 "2014-12-23T03:22:15Z")

</div>

I have environments that contain different nodes of different roles/types like: databases, webservers, load-balancers.  
Sometimes I need to call every server of the same role/type, but sometimes I need to address an environment.

e.g.:

> [databases]  
> [test1.db.my.com](http://test1.db.my.com) ansible\_ssh\_host=192.168.1.200 ansible\_ssh\_user=…  
> [test2.db.my.com](http://test2.db.my.com) ansible\_ssh\_host=192.168.1.201 ansible\_ssh\_user=…
> 
> [webservers]  
> [test1.web.my.com](http://test1.web.my.com) ansible\_ssh\_host=192.168.1.300 ansible\_ssh\_user=…  
> [test2.web.my.com](http://test2.web.my.com) ansible\_ssh\_host=192.168.1.301 ansible\_ssh\_user=…
> 
> [load-balancers]  
> [test1.lb.my.com](http://test1.lb.my.com) ansible\_ssh\_host=192.168.1.400 ansible\_ssh\_user=…  
> [test2.lb.my.com](http://test2.lb.my.com) ansible\_ssh\_host=192.168.1.401 ansible\_ssh\_user=…
> 
> [test1]  
> [test1.db.my.com](http://test1.db.my.com)  
> [test1.web.my.com](http://test1.web.my.com)  
> [test1.lb.my.com](http://test1.lb.my.com)
> 
> [test2]  
> [test2.db.my.com](http://test2.db.my.com)  
> [test2.web.my.com](http://test2.web.my.com)  
> [test2.lb.my.com](http://test2.lb.my.com)

So sometimes I need to run some update on all ‘webservers’, but sometimes I want to stop and reconfigure all servers in ‘test2’.

At last I need to address a specific environment and do things on different nodes iin sequence, e.g.

- ping the ‘database’ inside
- ‘df -h’ all the webservers
- then ‘free -m’ all the load-balancers

How could I tell that the following playbook is only for ‘test1’ environment and I need to run the ‘databases’ part of the playbook only for the databases inside that environment?

What are the starting points or steps? Topics I should read?

Regards:  
Bence

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [December 23, 2014, 12:35pm UTC](https://forum.ansible.com/t/managing-multi-node-environments/17132/2 "2014-12-23T12:35:03Z")

</div>

hosts: databases:&test1

^ will make the playbook target only hosts that exist in both groups  
[http://docs.ansible.com/intro\_patterns.html#patterns](http://docs.ansible.com/intro_patterns.html#patterns)

---

<div class="post-metadata">

**Author:** ![Bevan\_Bennett](https://avatars.discourse-cdn.com/v4/letter/b/76d3ee/32.png) [@Bevan\_Bennett](https://forum.ansible.com/u/Bevan_Bennett)\
**Post date:** [December 25, 2014, 1:04am UTC](https://forum.ansible.com/t/managing-multi-node-environments/17132/3 "2014-12-25T01:04:47Z")

</div>

We followed the recommendation at

[http://docs.ansible.com/playbooks\_best\_practices.html#stage-vs-production](http://docs.ansible.com/playbooks_best_practices.html#stage-vs-production)  
and have separate inventory files for each distinct environment (in your case test1 and test2).

We then invoke ansible with “-i test1” or “-i test2” depending on what environment is being targeted. The ability to hit -all- webservers with one command is lost, but you usually don’t want to do that and it’s safer to update test1 before test2 (or production).

So far this has worked well.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [December 25, 2014, 1:34am UTC](https://forum.ansible.com/t/managing-multi-node-environments/17132/4 "2014-12-25T01:34:19Z")

</div>

You don't loose anything if you put both inventories in the same  
directory, then you can call that directory as a 'global unified  
inventory' with -i /dir/with/test1andtest2 and still address all  
webservers.

---

<div class="post-metadata">

**Author:** ![Bevan\_Bennett](https://avatars.discourse-cdn.com/v4/letter/b/76d3ee/32.png) [@Bevan\_Bennett](https://forum.ansible.com/u/Bevan_Bennett)\
**Post date:** [December 25, 2014, 2:18am UTC](https://forum.ansible.com/t/managing-multi-node-environments/17132/5 "2014-12-25T02:18:50Z")

</div>

Interesting!

So if, instead of having top-level files called “env1”, “env2” and “env3”, we created a “inv” folder and moved them all into there…  
You’re saying we could run on just env1 with “-i inv/env1” or all envs with “-i inv”?

Will it still pull in the values from group\_vars/env1 when we do the former?

---

<div class="post-metadata">

**Author:** ![Bence\_Takacs](https://avatars.discourse-cdn.com/v4/letter/b/c6cbf5/32.png) [@Bence\_Takacs](https://forum.ansible.com/u/Bence_Takacs)\
**Post date:** [December 25, 2014, 3:41pm UTC](https://forum.ansible.com/t/managing-multi-node-environments/17132/6 "2014-12-25T15:41:17Z")

</div>

Thanks, Brian

…and what if i just don’t want to declare it in the playbook?  
Can I make the environment a parameter for the playbook?

> hosts: “databases:&{{env}}”

or when calling the playbook:

> ansible-playbook example.yml --limit test1

Regards:  
Bence

1. december 23., kedd 13:35:07 UTC+1 időpontban Brian Coca a következőt írta:

---

<div class="post-metadata">

**Author:** ![Bence\_Takacs](https://avatars.discourse-cdn.com/v4/letter/b/c6cbf5/32.png) [@Bence\_Takacs](https://forum.ansible.com/u/Bence_Takacs)\
**Post date:** [December 25, 2014, 4:17pm UTC](https://forum.ansible.com/t/managing-multi-node-environments/17132/7 "2014-12-25T16:17:39Z")

</div>

I found that:  
[http://stackoverflow.com/questions/18195142/safely-limiting-ansible-playbooks-to-a-single-machine](http://stackoverflow.com/questions/18195142/safely-limiting-ansible-playbooks-to-a-single-machine)

1. december 25., csütörtök 16:41:17 UTC+1 időpontban Bence Takács a következőt írta:

---

<div class="post-metadata">

**Author:** ![Bence\_Takacs](https://avatars.discourse-cdn.com/v4/letter/b/c6cbf5/32.png) [@Bence\_Takacs](https://forum.ansible.com/u/Bence_Takacs)\
**Post date:** [February 23, 2015, 2:45pm UTC](https://forum.ansible.com/t/managing-multi-node-environments/17132/8 "2015-02-23T14:45:56Z")

</div>

Here is a detailed description of my original idea: [http://rosstuck.com/multistage-environments-with-ansible/](http://rosstuck.com/multistage-environments-with-ansible/)

It seems that the concept was not obvious for others either. But afer a little digging they come up with the same concept… 😉

Regards:

Bence
