I think you're confused by what the issue is. Whether I use delegate_to or not is irrelevant. I don't care which host it runs on, and if I did, I would use the delegate_to. Even if I use delegate_to, it will still be skipped, since it evaluates whether to run the task at all based on the first host. I'm sorry I didn't include a delegate_to in my example, which lead to this confusion.
http://docs.ansible.com/ansible/latest/playbooks_delegation.html#run-once makes no mention of the fact that *even with delegate_to* it decides *whether to run at all* based on the first host. The mention of delegate_to actually makes this more confusing, since that The delegate_to should control where the execution happens (something irrelevant to this issue), not whether it runs at all. That part is at least consistent, since delegate_to does not control whether to run it. The issue is that run_once is not actually running once. It is "run only if the first node in the play says to run it", not "run one time if it should run for any host in the play". The latter is intuitive behavior. You talk of predictable results, and it is not predictable to have behavior that changes based on the order of hosts in your inventory file (the current behavior). Please note that in my example, the when clause is NOT on the task with run_once. If we make reusable code, we may be including that piece in many places, with or without the when clause. On Monday, March 19, 2018 at 12:15:13 PM UTC-7, flowerysong wrote: > > On Monday, March 19, 2018 at 2:49:12 PM UTC-4, Alex Hunt wrote: >> >> When running a task with run_once, if the first node is skipped, the >> entire task is skipped, rather than running on the first host that is not >> skipped. >> >> This behavior is not what is intuitively understood, this behavior is not >> mentioned in the docs, and this behavior is almost certainly not what most >> of people want it to do. There are discussions of this in multiple github >> issues, the most detailed of which is at >> https://github.com/ansible/ansible/issues/19966, but there are also at >> least https://github.com/ansible/ansible/issues/11496, >> https://github.com/ansible/ansible/issues/13226, and >> https://github.com/ansible/ansible/issues/23594. >> > > It may confuse some people, but it's both the documented behaviour and the > least surprising way to do things. Conditionals should not affect the > number of times a run_once task is evaluated even if they result in the > task being skipped. > > https://docs.ansible.com/ansible/latest/playbooks_delegation.html#run-once > says: "When “run_once” is not used with “delegate_to” it will execute on > the first host, as defined by inventory, in the group(s) of hosts targeted > by the play - e.g. webservers[0] if the play targeted “hosts: webservers”." > > >> Below is an untested simple example of a scenario that would skip the >> run_once task, when it should (according to the docs, and common sense) run >> on one of either host2 or host3. >> >> Inventory >> [all] >> host1 >> host2 >> host3 >> >> Playbook >> - name: Test Play >> hosts: all >> tasks: >> - include: outer-task.yml >> >> outer-task.yml >> - name: Outer task >> include: inner-task.yml >> when: inventory_hostname != 'host1' >> >> inner-task.yml >> - name: Inner task >> command: do_something >> run_once: True >> >> This issue is exacerbated by the fact that the inner task may have no >> idea why the first host is skipped (IE: we're including a reusable task >> that may get run many times in different ways). In those cases, there is no >> way to work around the issue with a simple `when: inventory_hostname == >> something`, since we don't know what to check against. >> > > You're mixing different ways of limiting where a task runs, with > predictable results (the task is assigned to one host, and the conditional > results in it being skipped). If you don't care which host it runs on, use > run_once without a conditional. If you want to run it on a specific host, > use delegate_to with run_once or a conditional without run_once. > -- 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/1b542a8f-8f75-462a-8c11-0fd0c76054dc%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
