- name: Installation k3s hosts: all:!localhost user: "{{ user_prompt }}" gather_facts: true tasks: - name: Update apt become: true # become_method: su ansible.builtin.shell: cmd: apt update -y - name: Install necessary packages become: true # become_method: su package: name: - sudo - curl - grep - expect - adduser state: present - name: Test if the current user is a sudoer ansible.builtin.shell: cmd: groups {{ ansible_user_id }} | grep -q 'sudo' register: sudoer failed_when: sudoer.rc not in [ 0, 1 ] - name: Adding user to sudoers become: true # become_method: su user: name: "{{ ansible_user_id }}" append: true groups: sudo when: sudoer.rc == 1 - name: Reset ssh connection to allow user changes to affect ansible user ansible.builtin.meta: reset_connection when: sudoer.rc == 1 - name: Attendre que la déconnexion soit effective wait_for: port: 22 delay: 10 timeout: 120 when: sudoer.rc == 1 - name: Download k3s ansible.builtin.uri: url: "https://get.k3s.io" method: GET dest: ./install_k3s.sh status_code: 200 headers: Content-Type: "application/json" - name: Install k3s become: true # become_method: su ansible.builtin.shell: cmd : sh install_k3s.sh - name: Add k3s group become: true # become_method: su group: name: k3s state: present - name: Add user to k3s group become: true # become_method: su user: name: "{{ ansible_user_id }}" append: true groups: k3s - name: Ensure .kube directory exists ansible.builtin.file: path: ~/.kube state: directory mode: '0700' - name: Copy kubeconfig file become: true ansible.builtin.copy: src: /etc/rancher/k3s/k3s.yaml dest: /home/{{ user_prompt }}/.kube/config remote_src: true mode: '0600' owner: "{{ ansible_user_id }}" group: "{{ ansible_user_gid }}" - name: Set KUBECONFIG environment variable in .bashrc ansible.builtin.lineinfile: path: ~/.bashrc line: 'export KUBECONFIG=$HOME/.kube/config' - name: Ensure kubectl autocompletion is enabled ansible.builtin.lineinfile: path: ~/.bashrc line: 'source <(kubectl completion bash)' - name: Unconditionally reboot the machine with all defaults become: true # become_method: su ansible.builtin.reboot: