# How to run a play based on a variable

**URL:** <https://forum.ansible.com/t/how-to-run-a-play-based-on-a-variable/42267>\
**Category:** Get Help\
**Created:** [April 29, 2025, 8:02pm UTC](https://forum.ansible.com/t/how-to-run-a-play-based-on-a-variable/42267 "2025-04-29T20:02:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rwagnergit](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/rwagnergit/32/7219_2.png) [@rwagnergit](https://forum.ansible.com/u/rwagnergit)\
**Post date:** [April 29, 2025, 8:02pm UTC](https://forum.ansible.com/t/how-to-run-a-play-based-on-a-variable/42267/1 "2025-04-29T20:02:02Z")

</div>

Hey all - I’m trying to figure out a way to run a play based on the value of a variable (i.e., if variable is set, run the play, otherwise skip the play). Scouring the internet, it looks like we used to be able to do this with [https://docs.ansible.com/ansible/2.9/modules/include\_module.html:](https://docs.ansible.com/ansible/2.9/modules/include_module.html:)

```auto
- name: run stuff on standby
  include: standby_play.yml
  when: standby_cluster is defined

```

But the include module has been removed. import\_playbook won’t work because it imports statically. include\_tasks won’t work because I need to specify the hosts, not just a task list.

I can’t figure out another elegant way to do this, so asking for help.

What’s the use case? I run a lot of Oracle databases, and we do everything with Ansible. Occasionally, we run Oracle Data Guard, where we have a primary database and a standby (replica) database. When we provision (or update) a database, we need to run plays on the primary cluster hosts and, if standby\_cluster is defined in config.yml, we also need to run plays on the standby cluster hosts. Right now, I have separate playbooks: one for the primary cluster, and a second for the standby (replica) cluster. I’d like to combine those into one playbook so folks don’t have to remember to run the latter if they are supporting a standby database.

Any ideas out there?

Thanks  
Rob

---

<div class="post-metadata">

**Author:** ![Lyle\_McKarns](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/lyle_mckarns/32/8142_2.png) [@Lyle\_McKarns](https://forum.ansible.com/u/Lyle_McKarns)\
**Post date:** [April 29, 2025, 8:22pm UTC](https://forum.ansible.com/t/how-to-run-a-play-based-on-a-variable/42267/2 "2025-04-29T20:22:53Z")

</div>

[This](https://docs.ansible.com/ansible/2.9/user_guide/playbooks_conditionals.html#applying-when-to-roles-imports-and-includes) seems to imply that you should be able to do

```auto
- name: stuff to run on standby
  import_tasks: standby_play.yml
  when: standby_cluster is defined

```

and, per the doc: “All the tasks get evaluated, but the conditional is applied to each and every task”

which _sounds like_ all the tasks will get imported, but they’ll only _run_ on hosts that have that var defined.

---

<div class="post-metadata">

**Author:** ![flowerysong](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/flowerysong/32/2305_2.png) [@flowerysong](https://forum.ansible.com/u/flowerysong)\
**Post date:** [April 29, 2025, 11:05pm UTC](https://forum.ansible.com/t/how-to-run-a-play-based-on-a-variable/42267/3 "2025-04-29T23:05:54Z")

</div>

> [@rwagnergit](#):
>
> the include module has been removed. import\_playbook won’t work because it imports statically

`import_playbook` does exactly the same thing that `include` at the playbook level did. If you cannot do it now with `import_playbook`, it did not work when `import_playbook` was called `include`.

---

<div class="post-metadata">

**Author:** ![wayt](https://avatars.discourse-cdn.com/v4/letter/w/3be4f8/32.png) [@wayt](https://forum.ansible.com/u/wayt)\
**Post date:** [April 30, 2025, 2:27am UTC](https://forum.ansible.com/t/how-to-run-a-play-based-on-a-variable/42267/4 "2025-04-30T02:27:48Z")

</div>

Alternatively, create an inventory group for your oracle data guard hosts and target the playbook to that group. It’ll do the selection at the playbook level to limit the hosts in the play loop instead of a lot skips from the static import.

---

<div class="post-metadata">

**Author:** ![rwagnergit](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/rwagnergit/32/7219_2.png) [@rwagnergit](https://forum.ansible.com/u/rwagnergit)\
**Post date:** [April 30, 2025, 2:01pm UTC](https://forum.ansible.com/t/how-to-run-a-play-based-on-a-variable/42267/5 "2025-04-30T14:01:22Z")

</div>

Thanks all. Not elegant, but I managed to do it with include\_tasks and a when: condition.
