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
for stuff like that i normally have a 'pre play' with hosts: localhost.
“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?
@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.
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
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