# \`run\_once\` used together with \`when\` not generating expected result

**URL:** <https://forum.ansible.com/t/run-once-used-together-with-when-not-generating-expected-result/20247>\
**Category:** Ansible Project\
**Created:** [January 18, 2016, 8:11am UTC](https://forum.ansible.com/t/run-once-used-together-with-when-not-generating-expected-result/20247 "2016-01-18T08:11:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Qiu\_Yu](https://avatars.discourse-cdn.com/v4/letter/q/d26b3c/32.png) [@Qiu\_Yu](https://forum.ansible.com/u/Qiu_Yu)\
**Post date:** [January 18, 2016, 8:11am UTC](https://forum.ansible.com/t/run-once-used-together-with-when-not-generating-expected-result/20247/1 "2016-01-18T08:11:36Z")

</div>

Hi,

I have following playbook, which I expect it to run only once, on one of the `group2` hosts.

site.yml  
`

---

<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:** [January 18, 2016, 3:30pm UTC](https://forum.ansible.com/t/run-once-used-together-with-when-not-generating-expected-result/20247/2 "2016-01-18T15:30:48Z")

</div>

run\_once limits the task to run on the 'first host' in the current  
play, so the when only evaluates for that host, if that host is not in  
'group2' it will be skipped an no other host will be evaluated.

---

<div class="post-metadata">

**Author:** ![Qiu\_Yu](https://avatars.discourse-cdn.com/v4/letter/q/d26b3c/32.png) [@Qiu\_Yu](https://forum.ansible.com/u/Qiu_Yu)\
**Post date:** [January 18, 2016, 6:44pm UTC](https://forum.ansible.com/t/run-once-used-together-with-when-not-generating-expected-result/20247/3 "2016-01-18T18:44:44Z")

</div>

Does that mean the `include` statement will be evaluated first no matter if `when` statement is satisfied or not?

QY

---

<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:** [January 18, 2016, 6:49pm UTC](https://forum.ansible.com/t/run-once-used-together-with-when-not-generating-expected-result/20247/4 "2016-01-18T18:49:02Z")

</div>

This is tricky, task includes are now dynamic in 2.0. Before 2.0 the  
when: does not stop the include from happening, it just gets applied  
to each included task, in 2.0 it does that only if the when cannot be  
resolved with the information available at the time of the include.

---

<div class="post-metadata">

**Author:** ![Qiu\_Yu](https://avatars.discourse-cdn.com/v4/letter/q/d26b3c/32.png) [@Qiu\_Yu](https://forum.ansible.com/u/Qiu_Yu)\
**Post date:** [January 18, 2016, 7:00pm UTC](https://forum.ansible.com/t/run-once-used-together-with-when-not-generating-expected-result/20247/5 "2016-01-18T19:00:16Z")

</div>

That explains a lot! Thanks Brian!!

QY

---

<div class="post-metadata">

**Author:** ![Larry\_Smith1](https://avatars.discourse-cdn.com/v4/letter/l/d26b3c/32.png) [@Larry\_Smith1](https://forum.ansible.com/u/Larry_Smith1)\
**Post date:** [January 18, 2016, 7:54pm UTC](https://forum.ansible.com/t/run-once-used-together-with-when-not-generating-expected-result/20247/6 "2016-01-18T19:54:34Z")

</div>

I banged my head against this same thing months ago until it finally clicked on what was going on!

---

<div class="post-metadata">

**Author:** ![Kevin\_Csuka](https://avatars.discourse-cdn.com/v4/letter/k/7feea3/32.png) [@Kevin\_Csuka](https://forum.ansible.com/u/Kevin_Csuka)\
**Post date:** [August 26, 2020, 3:42pm UTC](https://forum.ansible.com/t/run-once-used-together-with-when-not-generating-expected-result/20247/7 "2020-08-26T15:42:33Z")

</div>

Yes but what is the solution to combine those?  
I need to execute a task on the second host in the play.
