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] ***************************
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).