Ansible: Run tasks on specific hosts with var/fact from another host I'm trying to get the output from a command on the first host in group managers and execute it on the other hosts in the same group.
hosts file
[managers]
k8s01
k8s02
k8s03

[workers]
worker1
worker2
...

main.yaml
- name: Create token to join cluster
  shell: kubeadm token create --print-join-command
  run_once: true
  changed_when: false
  when: inventory_hostname == groups['managers'][0]
  register: kubernetes_join_command_result

-name: Set join command for managers
- set_fact:
    kubernetes_join_command_controlplane: &quot;{{ kubernetes_join_command_result.stdout }} --control-plane&quot;
  when: kubernetes_join_command_result.stdout is defined
  delegate_to: &quot;{{ item }}&quot;
  delegate_facts: true
  with_items: &quot;{{ groups['managers'] }}&quot;   


- name: Debug var
  debug:
    msg: 'Join Command: {{ kubernetes_join_command_result.stdout }}'

- name: Join managers into cluster      
  shell: &quot;{{ kubernetes_join_command_controlplane.stdout }}&quot;
  args:
    warn: no
  when: inventory_hostname != groups['managers'][0] and inventory_hostname in groups['managers']

Instead of running the join command in hosts k8s02 and k8s03, I get the output below.
TASK [managers: Join managers into cluster] ********************
skipping: [k8s01] =&gt; {&quot;changed&quot;: false, &quot;skip_reason&quot;: &quot;Conditional result was False&quot;}
TASK [manager: another task] ***************************
The condition works as expected. For example, the inventory and the playbook below
shell&gt; cat hosts
[managers]
k8s01
k8s02
k8s03

[workers]
worker1
worker2

shell&gt; cat playbook.yml 
- hosts: all
  gather_facts: false
  tasks:
    - debug:
        var: inventory_hostname
      when:
        - inventory_hostname != groups.managers.0
        - inventory_hostname in groups.managers

give
shell&gt; ansible-playbook -i hosts playbook.yml 

PLAY [all] ****

TASK [debug] ****
skipping: [worker1]
ok: [k8s02] =&gt; {
    &quot;inventory_hostname&quot;: &quot;k8s02&quot;
}
skipping: [k8s01]
skipping: [worker2]
ok: [k8s03] =&gt; {
    &quot;inventory_hostname&quot;: &quot;k8s03&quot;
}