- name: debug stuff hosts: all tasks: - name: set token fact for k3s-nodes hosts run_once: true with_items: '{{ groups["k3s-nodes"] }}' delegate_to: '{{ item }}' set_fact: fake_hostname: '{{ hostvars[item]["inventory_hostname"] }} for {{ item }}' - name: debug token fact for k3s-nodes hosts run_once: true with_items: '{{ groups["k3s-nodes"] }}' delegate_to: '{{ item }}' debug: msg: '{{ hostvars[item]["fake_hostname"] }} for {{ inventory_hostname }}' - name: install k3s on k3s-nodes hosts: k3s-nodes gather_facts: yes roles: - role: k3s-node # TODO: ensure odd number of k3s-control-nodes hosts - name: intialize k3s cluster on first control plane node hosts: k3s-control-nodes[0] tasks: - name: create config directory file: recurse: yes path: /etc/rancher/k3s/ state: directory mode: '0700' - name: copy initial config become: true copy: src: ./templates/k3s-initial-server-config.yaml dest: /etc/rancher/k3s/config.yaml - name: run initial k3s server become: true ansible.builtin.command: systemctl enable --now k3s - name: fetch token become: true slurp: src: /var/lib/rancher/k3s/server/node-token register: slurped_k3s_node_token - name: set token fact set_fact: k3s_node_token: '{{ slurped_k3s_node_token.content | b64decode }}' - name: configure remaining nodes hosts: k3s-nodes,!k3s-control-nodes[0] gather_facts: no tasks: - name: debug info ansible.builtin.debug: msg: '{{ ansible_facts }}' # - name: debug token fact for k3s-nodes hosts # run_once: true # debug: # msg: '{{ hostvars[groups["k3s-control-nodes"][0]]["k3s_node_token"] }}' - name: set token fact set_fact: k3s_node_token: '{{ hostvars[groups["k3s-control-nodes"][0]]["k3s_node_token"] }}' - name: configure k3s server with token lineinfile: path: /etc/systemd/system/k3s.service.env state: present line: K3S_TOKEN={{ k3s_node_token }} create: yes - name: configure k3s agent with token lineinfile: path: /etc/systemd/system/k3s-agent.service.env state: present line: K3S_TOKEN={{ k3s_node_token }} create: yes - name: create config directory file: recurse: yes path: /etc/rancher/k3s/ state: directory mode: '0700' - name: configure k3s server endpoint lineinfile: path: /etc/rancher/k3s/config.yaml state: present line: 'server: https://{{ hostvars[groups["k3s-control-nodes"][0]]["ansible_default_ipv4"]["address"] }}:6443' create: yes # TODO: assert token is valid? # - name: debug info2 # ansible.builtin.debug: # msg: '{{ k3s_node_token }}' # TODO: wait for initial control node to come up? - name: start remaining control nodes hosts: k3s-control-nodes,!k3s-control-nodes[0] tasks: - name: run k3s become: true ansible.builtin.command: systemctl enable --now k3s - name: start remaining agent nodes hosts: k3s-agent-nodes tasks: - name: run k3s become: true ansible.builtin.command: systemctl enable --now k3s-agent