Thanks everyone, I think the root of the issue is finally clear. I'd love to have a 'run_first_host_that_matches_when' keyword, though I understand there might be technical issues getting in the way. I think the most intuitive way to set the status would be to set it for all hosts that match the when, but that's a bit of an arbitrary thing.
The current set of keywords makes it very difficult to make reusable code. If we want to include roles or tasks conditionally, we simply can't use run_once safely. Even the "ugly-ass workarounds" mentioned above are not possible in some instances, since the conditions might be completely different depending on where it gets included. Another alternative would be to expose the list of hosts skipped by outer when clauses in another variable that can be used in inner when clauses. Something like "ansible_not_already_skipped_hosts". I know the logic is to skip the task, not the host, but when using includes, it's effectively the same. If we haven't done any includes with when clauses, it'd be effectively the same as ansible_play_hosts. If we had a variable like that, I could put something like this in the inner-task.yml: - name: Inner task command: hostname when: inventory_hostname == ansible_not_already_skipped_hosts[0] If I needed to save the result to all hosts, I could register the result and then follow up with a set_fact using the var from that one host. This isn't as clean as having a 'run_first_host_that_matches_when' keyword, but still prevents the "ugly-ass workarounds" from being needed on EVERY included task. On Tuesday, March 20, 2018 at 7:07:27 AM UTC-7, Brian Coca wrote: > > A couple of clarifications, these are important when you hit the > corner case in which it matters: > > - its not 'run on the first host in play/inventory' its 'run on the > first host that reaches the task' which means that hosts that fail > and/or are removed in previous tasks are not considered. Normally (in > the absence of failure) this does mean the first host in > play/inventory, changing to other strategies can affect this. > > - it is 'mostly' equivalent to `when: inventory_hostname == > ansible_play_batch.0` but there is one major difference, other hosts > are not 'skipped', they are all given the same status/return from the > single execution. > > > The feature should really be named > 'only_first_host_tries_to_run_and_applies_status_to_rest'. To make it > work as 'run_first_host_that_matches_when' would make the part of > applying the status to all hosts a lot more difficult to do sanely ... > do we set 'skipped' for the ones we skipped? do we set same status for > all hosts? > > At this point I don't see us modifying the feature (maybe clarifying > docs?), but I'm open to create a new set of keywords that allows for > the difference in range of behaviors not already available via > conditional construction. > > -- > ---------- > Brian Coca > -- You received this message because you are subscribed to the Google Groups "Ansible Project" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/d8e83919-a72c-4ca4-ac5b-b5a269cdc8e2%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
