# Conditional import for playbooks import

**URL:** <https://forum.ansible.com/t/conditional-import-for-playbooks-import/12523>\
**Category:** Ansible Project\
**Tags:** ansible-project\
**Created:** [March 18, 2013, 1:34pm UTC](https://forum.ansible.com/t/conditional-import-for-playbooks-import/12523 "2013-03-18T13:34:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [March 18, 2013, 1:34pm UTC](https://forum.ansible.com/t/conditional-import-for-playbooks-import/12523/1 "2013-03-18T13:34:13Z")

</div>

You really should not try to include different playbooks based on  
environment levels in my opinion, but keep different \*variables\* based  
on those environments.

Different playbooks would most likely correspond to a branch in git, a  
different playbook file, and likely a different inventory source file  
(-i production or -i stage).

---

<div class="post-metadata">

**Author:** ![Sebastien\_Pastor](https://avatars.discourse-cdn.com/v4/letter/s/b487fb/32.png) [@Sebastien\_Pastor](https://forum.ansible.com/u/Sebastien_Pastor)\
**Post date:** [March 18, 2013, 2:18pm UTC](https://forum.ansible.com/t/conditional-import-for-playbooks-import/12523/2 "2013-03-18T14:18:06Z")

</div>

Using -i production -i stage works great when having a single playbook for both environment (ie: webservers.yml)  
When there is a different set of tasks, what you are saying is to use something like : -i stage webservers\_stage.yml ?  
Also, how would you handle common tasks between stage and production?

Thanks !

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [March 18, 2013, 2:51pm UTC](https://forum.ansible.com/t/conditional-import-for-playbooks-import/12523/3 "2013-03-18T14:51:20Z")

</div>

The ideal stage stage environment is a model of a production, and only  
differs in terms of groupvars/ and hostvars/ files.

You can also target plays super easily too:

hosts: webservers:&stage

all webservers in stage

hosts: webservers:!production

all webservers not in production

So you could have a small play in your playbook (webservers.yml) that  
just pays attention to the group.

Remember a playbook can include more than 1 play, and it becomes  
easier to do things like this.

---

<div class="post-metadata">

**Author:** ![Sebastien\_Pastor](https://avatars.discourse-cdn.com/v4/letter/s/b487fb/32.png) [@Sebastien\_Pastor](https://forum.ansible.com/u/Sebastien_Pastor)\
**Post date:** [March 18, 2013, 4:57pm UTC](https://forum.ansible.com/t/conditional-import-for-playbooks-import/12523/4 "2013-03-18T16:57:08Z")

</div>

Targeting plays this way is great. I am having a single yml file and still can address specific  
tasks to specific hosts. Thanks!

There is one thing that do not work with our set up though.

In /common we keep generic playbooks like this one:

# install\_x.yml

> - hosts: Debian\_6\_x86\_64  
> tasks:
> - include: “X\_install\_debian.yml”

The idea is to have OS agnostic ready to use common playbooks.

We use them by calling

- hosts: webservers:&prod  
user: root  
tasks:

- group\_by: key=“${ansible\_lsb.id}_${ansible\_lsb.major\_release}_${ansible\_machine}”

- include : common/install\_x.yml

The problem is that if later in the file, we set up a new group\_by

> - hosts: webservers:&stage  
> user: root  
> tasks:
> - group\_by: key=“${ansible\_lsb.id}_${ansible\_lsb.major\_release}_${ansible\_machine}”
> - include : common/install\_x.yml

The group Debian\_6\_x86\_64 will be composed by webservers:&prod AND webservers:&stage

I realise this could be a group\_by bug/feature. In another way having to re-create a group using group\_by seems a little heavy.  
I am very open to any suggestions that would make possible this very purpose: generic reusable playbooks with os type separated tasks.

Thanks again for your time.

Seb

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [March 19, 2013, 2:10am UTC](https://forum.ansible.com/t/conditional-import-for-playbooks-import/12523/5 "2013-03-19T02:10:25Z")

</div>

One way to fix this in your case, would be to change

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- group\_by:  
key="${ansible\_lsb.id}\_${ansible\_lsb.major\_release}\_${ansible\_machine}"

To:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- group\_by:  
key="stage-${ansible\_lsb.id}\_${ansible\_lsb.major\_release}\_${ansible\_machine}"

You can also decide to target just stage or prod in an easier way by  
keeping seperate inventory

ansible-playbook foo.yml -i stage

which is a less risky version of:

ansible-playbook foo.yml --limit stage

because if you leave off the limit, it would run on both, and that  
would be bad 🙂
