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
6 changed files with 17 additions and 14 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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