# Conditionally execute some tasks on dev hosts only

**URL:** <https://forum.ansible.com/t/conditionally-execute-some-tasks-on-dev-hosts-only/14845>\
**Category:** Ansible Project\
**Created:** [March 24, 2014, 9:26pm UTC](https://forum.ansible.com/t/conditionally-execute-some-tasks-on-dev-hosts-only/14845 "2014-03-24T21:26:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shaunak\_Kashyap](https://avatars.discourse-cdn.com/v4/letter/s/c68b51/32.png) [@Shaunak\_Kashyap](https://forum.ansible.com/u/Shaunak_Kashyap)\
**Post date:** [March 24, 2014, 9:26pm UTC](https://forum.ansible.com/t/conditionally-execute-some-tasks-on-dev-hosts-only/14845/1 "2014-03-24T21:26:40Z")

</div>

Hi,

I would like to conditionally execute some tasks within a play (or even the entire play itself) only if I’m setting up a development environment. What is the best way to perform such conditional execution?

One way I can think of is to define a variable (say “deploy\_env=true”) in my dev inventory file (hosts.dev), then use the “when” statement within the conditional tasks to look for that variable (i.e. when: deploy\_env == true). Is this a good approach or is there a better way to define and run environment-specific tasks/plays?

Thanks,

Shaunak

---

<div class="post-metadata">

**Author:** ![Paul\_Durivage](https://avatars.discourse-cdn.com/v4/letter/p/94ad74/32.png) [@Paul\_Durivage](https://forum.ansible.com/u/Paul_Durivage)\
**Post date:** [March 26, 2014, 4:14am UTC](https://forum.ansible.com/t/conditionally-execute-some-tasks-on-dev-hosts-only/14845/2 "2014-03-26T04:14:11Z")

</div>

I think setting a variable is one straight forward way to go about solving the problem, conditionally running tasks if the variable is set. See this: [http://docs.ansible.com/playbooks\_conditionals.html#applying-when-to-roles-and-includes](http://docs.ansible.com/playbooks_conditionals.html#applying-when-to-roles-and-includes)

Another way is to create a group of devel servers in inventory or dynamically, and create a play that only runs on the devel group.
