Copy .kube config file from master to all nodes using ansible

7/6/2019

I would like to copy the kube admin config file from the Kubernetes master host to the nodes using ansible synchronize but that fails due to a missing python interpreter, but I have already installed docker on all machines without any issue.

See my task

- name: admin conf file to nodes environment: ANSIBLE_PYTHON_INTERPRETER: python3 synchronize: src: /home/{{ansible_ssh_user}}/.kube/config dest: /home/{{ansible_ssh_user}} delegate_to: "{{item}}" loop: "{{groups.node}}"

-- Herve Meftah
ansible
kubernetes

1 Answer

7/6/2019

You can use synchronize module only when rsync is enabled either in source server (kube master in your case) or in the kube nodes.

Method 1: to push from master, need rsync enabled in master

Synchronize use push mode by default

- hosts: nodes
  tasks:
    - name: Transfer file from master to nodes
      synchronize:
        src: /etc/kubernetes/admin.conf
        dest: $HOME/.kube/config
      delegate_to: "{{ master }}"

Method 2: to use fetch and copy modules

 - hosts: all
   tasks:
     - name: Fetch the file from the master to ansible
       run_once: yes
       fetch: src=/etc/kubernetes/admin.conf dest=temp/ flat=yes
       when: "{{ ansible_hostname == 'master' }}"
     - name: Copy the file from the ansible to nodes
       copy: src=temp/admin.conf dest=$HOME/.kube/config
       when: "{{ ansible_hostname != 'master' }}"

Hope this helps.

-- Prakash Krishna
Source: StackOverflow