# A better "run once" mechanism?

**URL:** <https://forum.ansible.com/t/a-better-run-once-mechanism/14756>\
**Category:** Ansible Project\
**Created:** [March 11, 2014, 10:39pm UTC](https://forum.ansible.com/t/a-better-run-once-mechanism/14756 "2014-03-11T22:39:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jesse\_Keating](https://avatars.discourse-cdn.com/v4/letter/j/3bc359/32.png) [@Jesse\_Keating](https://forum.ansible.com/u/Jesse_Keating)\
**Post date:** [March 11, 2014, 10:39pm UTC](https://forum.ansible.com/t/a-better-run-once-mechanism/14756/1 "2014-03-11T22:39:19Z")

</div>

I've got a play that ensures some servers are created, then ensures a domain for those servers exists in DNS, then ensures A records exist within that domain for each server.

I want the domain ensure to run once, not 10x time, so I have a construct like:

when: inventory\_hostname == play\_hosts[0]

so that it only runs on the first host of the play.

This works, so long as that first host doesn't have a failure ensuring that it exists.

This is pretty awkward, and I'm wondering what other folks have for a "run once" construct.

I had thought about touching a file locally when the DNS API call is done to create the domain, but that's racy, because this is a multi-fork play so multiple forks would be attempting this at once.

-jlk

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [March 11, 2014, 10:57pm UTC](https://forum.ansible.com/t/a-better-run-once-mechanism/14756/2 "2014-03-11T22:57:04Z")

</div>

for stuff like that i normally have a 'pre play' with hosts: localhost.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [March 11, 2014, 11:54pm UTC](https://forum.ansible.com/t/a-better-run-once-mechanism/14756/3 "2014-03-11T23:54:23Z")

</div>

“This works, so long as that first host doesn’t have a failure ensuring that it exists.”

Not sure I completely follow this part, do you mean no hosts in the first play?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [March 11, 2014, 11:57pm UTC](https://forum.ansible.com/t/a-better-run-once-mechanism/14756/4 "2014-03-11T23:57:26Z")

</div>

@jesse, actually play\_hosts[0] will always give you a ‘currently working host’, the list removes hosts that have errored out of the play.

it cannot save you if the target host suddenly becomes an issue, i would try do/until then.

---

<div class="post-metadata">

**Author:** ![Jesse\_Keating](https://avatars.discourse-cdn.com/v4/letter/j/3bc359/32.png) [@Jesse\_Keating](https://forum.ansible.com/u/Jesse_Keating)\
**Post date:** [March 12, 2014, 5:07am UTC](https://forum.ansible.com/t/a-better-run-once-mechanism/14756/5 "2014-03-12T05:07:10Z")

</div>

No, I mean that the host it is attempting to run the task on didn't finish the first task which has required data for the second task. So it tries to use data that doesn't exist, and boom.

Will follow up more on Brian's reply.

-jlk

---

<div class="post-metadata">

**Author:** ![Jesse\_Keating](https://avatars.discourse-cdn.com/v4/letter/j/3bc359/32.png) [@Jesse\_Keating](https://forum.ansible.com/u/Jesse_Keating)\
**Post date:** [March 12, 2014, 5:08am UTC](https://forum.ansible.com/t/a-better-run-once-mechanism/14756/6 "2014-03-12T05:08:35Z")

</div>

Oh! That's amazing. I told a small lie in my first mail, the play book currently has:

when: inventory\_hostname == hostvars.keys()[0]

because it was written before play\_hosts existed. Now that we're going to have access to play\_hosts I'll switch all my tasks like this over, and we'll avoid the current issue that came up today.

Thanks for the info!

-jlk
