Do not rely on ansible_user (#214)

* Apply "become" on roles instead of plays

This leads to facts being gathered for the "regular" login user, instead
of root.

* Do not rely on ansible_user

Instead of reading ansible_user (which may or may not be defined), this
patch lets the roles rely on Ansible facts [1].

[1]: https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_vars_facts.html
This commit is contained in:
Simon Leiner 2023-01-30 04:20:25 +01:00 committed by GitHub
parent e33cbe52c1
commit be3e72e173
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 17 additions and 14 deletions

View File

@ -2,8 +2,8 @@
- name: Reboot k3s_cluster
hosts: k3s_cluster
gather_facts: yes
become: yes
tasks:
- name: Reboot the nodes (and Wait upto 5 mins max)
become: true
reboot:
reboot_timeout: 300

View File

@ -2,12 +2,14 @@
- hosts: k3s_cluster
gather_facts: yes
become: yes
roles:
- role: reset
become: true
- role: raspberrypi
become: true
vars: {state: absent}
post_tasks:
- name: Reboot and wait for node to come back up
become: true
reboot:
reboot_timeout: 3600

View File

@ -1,5 +1,4 @@
---
ansible_user: root
server_init_args: >-
{% if groups['master'] | length > 1 %}
{% if ansible_hostname == hostvars[groups['master'][0]]['ansible_hostname'] %}

View File

@ -97,24 +97,24 @@
- name: Create directory .kube
file:
path: ~{{ ansible_user }}/.kube
path: "{{ ansible_user_dir }}/.kube"
state: directory
owner: "{{ ansible_user }}"
owner: "{{ ansible_user_id }}"
mode: "u=rwx,g=rx,o="
- name: Copy config file to user home directory
copy:
src: /etc/rancher/k3s/k3s.yaml
dest: ~{{ ansible_user }}/.kube/config
dest: "{{ ansible_user_dir }}/.kube/config"
remote_src: yes
owner: "{{ ansible_user }}"
owner: "{{ ansible_user_id }}"
mode: "u=rw,g=,o="
- name: Configure kubectl cluster to {{ endpoint_url }}
command: >-
k3s kubectl config set-cluster default
--server={{ endpoint_url }}
--kubeconfig ~{{ ansible_user }}/.kube/config
--kubeconfig {{ ansible_user_dir }}/.kube/config
changed_when: true
vars:
endpoint_url: >-

View File

@ -3,7 +3,7 @@
file:
path: /tmp/k3s
state: directory
owner: "{{ ansible_user }}"
owner: "{{ ansible_user_id }}"
mode: 0755
with_items: "{{ groups['master'] }}"
run_once: true
@ -12,7 +12,7 @@
template:
src: "metallb.crs.j2"
dest: "/tmp/k3s/metallb-crs.yaml"
owner: "{{ ansible_user }}"
owner: "{{ ansible_user_id }}"
mode: 0755
with_items: "{{ groups['master'] }}"
run_once: true

View File

@ -2,23 +2,25 @@
- hosts: k3s_cluster
gather_facts: yes
become: yes
roles:
- role: prereq
become: true
- role: download
become: true
- role: raspberrypi
become: true
- hosts: master
become: yes
roles:
- role: k3s/master
become: true
- hosts: node
become: yes
roles:
- role: k3s/node
become: true
- hosts: master
become: yes
roles:
- role: k3s/post
become: true