<p>I believe your <code>hosts</code> is set to <code>k8s01</code> or other group not containing k8s02 and
k8s03.<br />
That's why they are not run.</p>
<p>You shall add <code>with_items: &quot;{{ groups['managers'] }}&quot;</code> and delegate as you done in the second task <code>-name: Set join command for managers</code>.</p>
<pre><code>- 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']
  delegate_to: &quot;{{ item }}&quot;
  delegate_facts: true
  with_items: &quot;{{ groups['managers'] }}&quot; 
</code></pre>
<p>Alternatively, depending on your design and scope.
Do as answer from Botka and set the hosts as:<br />
<code>hosts: all</code><br />
or<br />
<code>hosts: managers</code></p>

CC BY-SA 4.0
2020-09-14T03:15:51.913Z
2020-09-14T03:22:19.393Z
2020-09-14T03:22:19.393Z
akane
<p>The condition works as expected. For example, the inventory and the playbook below</p>
<pre class="lang-yaml prettyprint-override"><code>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
</code></pre>
<p>give</p>
<pre class="lang-yaml prettyprint-override"><code>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;
}
</code></pre>

CC BY-SA 4.0
2020-09-11T12:48:15.850Z
Thank you for your answer but this not help me. I have different behavior with the same condition. Ansible don't apply the task in k8s02 and k8s03.
Joao  Vitorino
CC BY-SA 4.0
2020-09-11T05:34:43.783Z
2020-09-11T05:43:10.313Z
2020-09-11T05:43:10.313Z
Vladimir Botka
<p>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.</p>
<p>hosts file</p>
<pre><code>[managers]
k8s01
k8s02
k8s03

[workers]
worker1
worker2
...
</code></pre>
<p>main.yaml</p>
<pre><code>- 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']
</code></pre>
<p>Instead of running the join command in hosts k8s02 and k8s03, I get the output below.</p>
<pre><code>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] ***************************
</code></pre>

CC BY-SA 4.0
2020-09-11T05:38:49.440Z
Why do you think all the `shell` commands are needed to reproduce the problem? See [mcve](https://stackoverflow.com/help/minimal-reproducible-example), post the missing parts (e.g. minimal complete playbook), and remove redundant parts (e.g. k8s stuff).
Vladimir Botka
CC BY-SA 4.0
2020-09-11T11:19:26.710Z
I don't see why this is not a mcve.  I was very specific in which task ansible doesn't work as expected. Tell what you think is missing and I will post.
Joao  Vitorino
<ansible>
Ansible: Run tasks on specific hosts with var/fact from another host
Joao  Vitorino
# Ansible: Run tasks on specific hosts with var/fact from another host

(Asked by: username_0 on 2020-09-11)

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: "{{ kubernetes_join_command_result.stdout }} --control-plane"
  when: kubernetes_join_command_result.stdout is defined
  delegate_to: "{{ item }}"
  delegate_facts: true
  with_items: "{{ groups['managers'] }}"   


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

- name: Join managers into cluster      
  shell: "{{ kubernetes_join_command_controlplane.stdout }}"
  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] => {"changed": false, "skip_reason": "Conditional result was False"}
TASK [manager: another task] ***************************

**Tags:** ansible

> **Comments:**
> **username_1**: Why do you think all the `shell` commands are needed to reproduce the problem? See [mcve](https://stackoverflow.com/help/minimal-reproducible-example), post the missing parts (e.g. minimal complete playbook), and remove redundant parts (e.g. k8s stuff).  
> **username_0**: I don't see why this is not a mcve.  I was very specific in which task ansible doesn't work as expected. Tell what you think is missing and I will post.  

---

## 2 answers

### Answer by username_2 on 2020-09-14. Score: 1 | Accepted answer

I believe your hosts is set to k8s01 or other group not containing k8s02 and
k8s03.
That's why they are not run.
You shall add with_items: "{{ groups['managers'] }}" and delegate as you done in the second task -name: Set join command for managers.
- name: Join managers into cluster      
  shell: "{{ kubernetes_join_command_controlplane.stdout }}"
  args:
    warn: no
  when: inventory_hostname != groups['managers'][0] and inventory_hostname in groups['managers']
  delegate_to: "{{ item }}"
  delegate_facts: true
  with_items: "{{ groups['managers'] }}" 

Alternatively, depending on your design and scope.
Do as answer from Botka and set the hosts as:
hosts: all
or
hosts: managers


---

### Answer by username_1 on 2020-09-11. Score: 2

The condition works as expected. For example, the inventory and the playbook below
shell> cat hosts
[managers]
k8s01
k8s02
k8s03

[workers]
worker1
worker2

shell> 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> ansible-playbook -i hosts playbook.yml 

PLAY [all] ****

TASK [debug] ****
skipping: [worker1]
ok: [k8s02] => {
    "inventory_hostname": "k8s02"
}
skipping: [k8s01]
skipping: [worker2]
ok: [k8s03] => {
    "inventory_hostname": "k8s03"
}

> **Comments:**
> **username_0**: Thank you for your answer but this not help me. I have different behavior with the same condition. Ansible don't apply the task in k8s02 and k8s03.  

---

