--
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-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/4d6c8e6a-478c-4736-9e10-c12032dd7c3fn%40googlegroups.com.
-- Todd
- name: Add hosts to groups ansible.builtin.add_host: name: "{{ inventory_hostname }}" groups: - nodes - "{{ 'nodes_prim' if is_active | d(false) else 'nodes_stby' }}"
-- Todd
- name: Set is_leader for tango and cloister ansible.builtin.set_fact: is_leader: '{{ true if inventory_hostname == "tango" else false }}'After that, I could play with ansible.builtin.add_host. And that's where the confusion starts. Everything works "as expected" except for theses add_host tasks. So I went back and read the document on add_host very closely. I had not appreciated this bit about the bypass_host_loop property:
Forces a ‘global’ task that does not execute per host, this bypasses per host templating and serial, throttle and other loop considerations
Conditionals will work as if
run_once
is being used, variables used will be from the first available hostThis action will not work normally outside of lockstep strategies
So you can't run ansible.builtin.add_host on all your hosts the same way you would, well, almost everything else. If you want to loop over all your hosts (which is the default behavior for most other modules), then you have to do it explicitly, like this:
- name: Display is_leader for all hosts ansible.builtin.debug: var: is_leader - name: Show groups BEFORE add_host ansible.builtin.debug: msg: "{{ groups }}" - name: Add hosts to either the nodes_prim or nodes_stby group ansible.builtin.add_host: name: "{{ item }}" groups: - "{{ 'nodes_prim' if hostvars[item].is_leader else 'notes_stby' }}" loop: "{{ ansible_play_hosts }}" - name: Show groups AFTER add_host ansible.builtin.debug: msg: "{{ groups }}"
-- Todd