On Monday, March 19, 2018 at 9:42:58 PM UTC-4, Josh Smift wrote:
>
> My use case involved roles. I had something like
>
> - hosts: web:app:db
> roles:
> - role: myrole
> when: color == "blue"
>
> In the role, there was a task that ran on localhost (via delegate_to), but
> only once (via run_once) for the whole batch of hosts.
>
> Everything worked fine, except that if the first host in inventory
> happened not to be blue, the run_once caused the localhost task to be
> skipped. The order of the hosts in inventory was completely arbitrary --
> these were EC2 instances at AWS.
>
> The eventual workaround was to add the `when` to every single task in the
> role *except* the run_once one, which made both the playbook and the role
> less readable.
>
Disregarding the implementation details, `run_once: true` is effectively
the same as adding `when: inventory_hostname == ansible_play_batch.0` to
the task . As I said before, if that's not what you want you should instead
write a conditional that expresses your actual intent.
Here's one approach:
- group_by:
key: color_{{ color | default('octarine') }}
- name: Run on localhost once
delegate_to: localhost
debug:
msg: His pills, his hands, his jeans
when: inventory_hostname == groups.color_blue.0
Or you might opt for something like this, which is overly clever and
requires a very recent version of Jinja:
- name: Run on localhost once
delegate_to: localhost
debug:
msg: Suddenly I was a lilac sky
vars:
first_host: "{{ ansible_play_hosts | map('extract', hostvars) |
selectattr('color', 'defined') | selectattr('color', 'equalto', 'blue') |
first }}"
when: inventory_hostname == first_host.inventory_hostname
Or you might restructure the playbook so it only runs the role on blue
hosts and doesn't need a separate conditional, and use `run_once` on the
task. The best approach depends on personal taste and other decisions made
in writing the playbook and setting up your Ansible environment.
--
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/ba1a1714-7c7b-4f2c-bcdf-e200b938e495%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.