1

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] ***************************
2
  • Why do you think all the shell commands are needed to reproduce the problem? See mcve, post the missing parts (e.g. minimal complete playbook), and remove redundant parts (e.g. k8s stuff). Commented Sep 11, 2020 at 5:38
  • 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. Commented Sep 11, 2020 at 11:19

2 Answers 2

1

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

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"
}
1
  • 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. Commented Sep 11, 2020 at 12:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.