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 ansible-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/9c58e144-46b7-4998-8769-bdfaee91c5da%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to