0

I am trying to set up a Kubernetes cluster with a single master and two worker nodes. It is only possible to copy the kubeadm token from the master node to one worker node but not to the other (ssh keys have been set up on both).

In my host.ini file, I have set up the following settings:

[masters]
master ansible_host=ipaddr

[workers]
wk1 ansible_host=ipaddr1
wk2 ansible_host=ipaddr2

I copied the kubeadm join token to a file which is in my master node (pls note I run my playbook from role/master/tasks/main.yaml

- name: Get the token for joining the worker nodes
  tags: createjointoken
  become: yes
  command: kubeadm token create  --print-join-command
  register: join_worker_command

- name: copy the token to a file
  tags: createjointoken
  copy:
    content: "{{join_worker_command.stdout }}"
    dest: path to copy the file
    mode: 0644

Now I want to copy the file to my worker node so I run the following code from worker task(from /role/worker/tasks/main.yaml)

- name: Copy the file 
  tags: copytkn
  become: yes
  copy:
    remote_src: yes
    src: path to file
    dest: path to file

There is now a problem with the token being copied successfully to wk1 and not to wk2.

It says that the Source (filepath) not found. Could you please help me copy the files successfully to both worker nodes? The copying doesn't seem to be working to the other one, so I don't know why this is happening.

Thanks

galaxy01
  • 1
  • 1
  • If the file is on your master node, [`slurp`](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/slurp_module.html) it from there, with a `delegate_to: master`, and `run_once: true` (so, back on your ansible controller), then `copy` it back on your workers. – β.εηοιτ.βε Nov 08 '22 at 11:32
  • Does this answer your question? [Ansible: How to copy files remote to remote](https://stackoverflow.com/questions/48113332/ansible-how-to-copy-files-remote-to-remote) – β.εηοιτ.βε Nov 08 '22 at 11:41
  • Thanks, I tried fetch and copy and it worked !! – galaxy01 Nov 08 '22 at 16:07

1 Answers1

0

i also faced this problem when i wanted to create a kubernetes cluster and when i was searching for a solution i found some tasks that in my case were useful.

    - name: Get the token for joining the worker nodes
      become: yes
      become_user: kube
      shell: kubeadm token create  --print-join-command
      register: kubernetes_join_command

    - name: Copy join command to local file.
      become: yes
      local_action: copy content="{{ kubernetes_join_command.stdout_lines[0] }}" dest="/tmp/kubernetes_join_command" mode=0777

Then on the workers file i declared the following tasks

   - name: Copy join command from Ansiblehost to the worker nodes.
     become: yes
     copy:
       src: /tmp/kubernetes_join_command
       dest: /tmp/kubernetes_join_command
       mode: 0777

   - name: Join the Worker nodes to the cluster.
     become: yes
     command: sh /tmp/kubernetes_join_command
     register: joined_or_not

After further researches i found out that there is another very helpful tool also included in the kubernetes documentation regarding best practices called "kubespray". It is a very powerful tool to bootstrap kubernetes clusters along with other additional components that may be useful to you. Kubespray

devi12311
  • 16
  • 1