Refactor playbook_microk8s.yml and ansible-apply.yaml

Simplify playbook_microk8s.yml by removing duplicate lines and whitespace changes.
Update ansible-apply.yaml to include a scheduled job for Terraform Apply.
This commit is contained in:
2024-09-28 21:27:24 +02:00
parent bad1668115
commit 1f060541c3
12 changed files with 136 additions and 233 deletions
+108 -78
View File
@@ -1,118 +1,148 @@
---
- name: Create LUKS encrypted file system with key file fetched from HTTP server
- name: Setup encrypted volume for a13labs
hosts: contabo
become: yes
become: true
vars:
disk_image_path: /a13labs.img
disk_image_size: 1G
luks_device_name: luks_device
luks_device_name: a13labs
mount_point: /a13labs
key_file_path: /root/luks_keyfile
key_file_path: /root/a13labs.key
key_file_url: https://a13labs-infra-{{ lookup('env','BUCKET_SUFFIX') }}.s3.fr-par.scw.cloud
key_fetch_service: fetch-a13labs-key.service
key_fetch_service: a13labs-key.service
tasks:
- name: Install cryptsetup and curl
apt:
ansible.builtin.apt:
name:
- cryptsetup
- curl
state: present
when: ansible_os_family == 'Debian'
- name: Get mac address
ansible.builtin.shell: |
set -o pipefail
ip link show eth0 | awk '/ether/ {print $2}'
args:
executable: /bin/bash
register: mac_address
changed_when: false
- name: Sanitize mac address
ansible.builtin.set_fact:
mac_address_sanitized: "{{ mac_address.stdout | regex_replace('[:]', '_') }}"
changed_when: false
- name: Create disk image
ansible.builtin.command: fallocate -l {{ disk_image_size }} {{ disk_image_path }}
args:
creates: "{{ disk_image_path }}"
register: disk_image
- name: Create encrypted volume
- name: Create bucket URL
ansible.builtin.set_fact:
key_file_url: "{{ key_file_url }}/keys/{{ mac_address_sanitized }}.key"
changed_when: false
- name: Create encrypted volume # noqa no-handler
when: disk_image is changed
block:
- name: Get mac address
ansible.builtin.shell: |
set -o pipefail
ip link show eth0 | awk '/ether/ {print $2}'
args:
executable: /bin/bash
register: mac_address
changed_when: false
block:
- name: Sanitize mac address
set_fact:
mac_address_sanitized: "{{ mac_address.stdout | regex_replace('[:]', '%3A') }}"
changed_when: false
- name: Download key file from Key server
ansible.builtin.uri:
url: "{{ key_file_url }}"
dest: "{{ key_file_path }}"
mode: '0600'
args:
creates: "{{ key_file_path }}"
- name: Create bucket URL
set_fact:
key_file_url: "{{ key_file_url }}/keys/{{ mac_address_sanitized }}.key"
changed_when: false
- name: Set up LUKS encryption with key file
ansible.builtin.command: cryptsetup -q luksFormat {{ disk_image_path }} {{ key_file_path }}
changed_when: false
- name: Download key file from HTTP server
ansible.builtin.uri:
url: "{{ key_file_url }}"
dest: "{{ key_file_path }}"
mode: '0600'
args:
creates: "{{ key_file_path }}"
- name: Open LUKS device with key file
ansible.builtin.command: cryptsetup luksOpen {{ disk_image_path }} {{ luks_device_name }} -d {{ key_file_path }}
register: luks_device
changed_when: false
- name: Set up LUKS encryption with key file
ansible.builtin.command: cryptsetup -q luksFormat {{ disk_image_path }} {{ key_file_path }}
- name: Create file system
ansible.builtin.command: mkfs.ext4 /dev/mapper/{{ luks_device_name }}
changed_when: false
- name: Open LUKS device with key file
ansible.builtin.command: cryptsetup luksOpen {{ disk_image_path }} {{ luks_device_name }} -d {{ key_file_path }}
register: luks_device
- name: Close LUKS device
ansible.builtin.command: cryptsetup close {{ luks_device_name }}
changed_when: false
- name: Delete key file
ansible.builtin.file:
path: "{{ key_file_path }}"
state: absent
- name: Delete key file
ansible.builtin.file:
path: "{{ key_file_path }}"
state: absent
- name: Create file system
ansible.builtin.command: mkfs.ext4 /dev/mapper/{{ luks_device_name }}
- name: Create mount point
ansible.builtin.file:
path: "{{ mount_point }}"
state: directory
mode: "0750"
- name: Create mount point
file:
path: "{{ mount_point }}"
state: directory
- name: Create systemd service to fetch key file and open LUKS device
ansible.builtin.copy:
dest: /etc/systemd/system/{{ key_fetch_service }}
mode: '0644'
content: |
[Unit]
Description=Open LUKS device
After=network-online.target
- name: Mount the file system
ansible.builtin.command: mount /dev/mapper/{{ luks_device_name }} {{ mount_point }}
[Service]
Type=oneshot
RemainAfterExit=true
ExecStartPre=/usr/bin/curl -o {{ key_file_path }} "{{ key_file_url }}"
ExecStart=/usr/sbin/cryptsetup luksOpen {{ disk_image_path }} {{ luks_device_name }} -d {{ key_file_path }}
ExecStartPost=/bin/rm -f {{ key_file_path }}
ExecStop=/usr/sbin/cryptsetup close {{ luks_device_name }}
- name: Ensure the file system is mounted
mount:
path: "{{ mount_point }}"
src: /dev/mapper/{{ luks_device_name }}
fstype: ext4
state: mounted
[Install]
WantedBy=remote-fs.target
- name: Create systemd service to fetch key file and open LUKS device
copy:
dest: /etc/systemd/system/{{ key_fetch_service }}
content: |
[Unit]
Description=Fetch LUKS key file from HTTP server and open LUKS device
After=network-online.target
Before=cryptsetup.target
- name: Create systemd service to mount the file system
ansible.builtin.copy:
dest: /etc/systemd/system/{{ mount_point | basename }}.mount
mode: '0644'
content: |
[Unit]
Description=Mount LUKS device
After=remote-fs.target
[Service]
Type=oneshot
ExecStartPre=/usr/bin/curl -o {{ key_file_path }} {{ key_file_url }}
ExecStart=/usr/bin/cryptsetup luksOpen {{ disk_image_path }} {{ luks_device_name }} -d {{ key_file_path }}
ExecStartPost=/bin/rm -f {{ key_file_path }}
[Mount]
What=/dev/mapper/{{ luks_device_name }}
Where={{ mount_point }}
Type=ext4
Options=defaults
[Install]
WantedBy=multi-user.target
[Install]
WantedBy=multi-user.target
- name: Reload systemd daemon
ansible.builtin.command: systemctl daemon-reload
- name: Reload systemd daemon
ansible.builtin.systemd:
daemon_reload: true
- name: Enable the key fetch service
ansible.builtin.command: systemctl enable {{ key_fetch_service }}
- name: Enable the key fetch service
ansible.builtin.systemd:
name: "{{ key_fetch_service }}"
enabled: true
- name: Add entry to /etc/fstab
lineinfile:
path: /etc/fstab
line: "/dev/mapper/{{ luks_device_name }} {{ mount_point }} ext4 defaults 0 2"
create: yes
- name: Enable the mount device
ansible.builtin.systemd:
name: "{{ mount_point | basename }}.mount"
enabled: true
- name: Start the key fetch service
ansible.builtin.systemd:
name: "{{ key_fetch_service }}"
state: started
- name: Start the mount device
ansible.builtin.systemd:
name: "{{ mount_point | basename }}.mount"
state: started
+2 -2
View File
@@ -1,6 +1,6 @@
---
- name: Microk8s Setup
hosts: microk8s
- name: Microk8s Setup
hosts: microk8s
gather_facts: true
roles:
+3 -3
View File
@@ -124,7 +124,7 @@
- k8s::iac
- name: Get new config SHA1
set_fact:
ansible.builtin.set_fact:
patched_kubeconfig_sha1: "{{ patched_kubeconfig_yaml | to_nice_yaml | sha1 }}"
tags:
- k8s::iac
@@ -136,6 +136,6 @@
dest: "{{ k8s_kubeconfig }}"
owner: "{{ k8s_iac_user }}"
mode: "0600"
when: kubeconfig_exists.stat.exists == false or patched_kubeconfig_sha1 != kubeconfig_exists.stat.checksum
when: not kubeconfig_exists.stat.exists or patched_kubeconfig_sha1 != kubeconfig_exists.stat.checksum
tags:
- k8s::iac
- k8s::iac
@@ -25,7 +25,7 @@
- name: Check if secret exists
ansible.builtin.shell: |
set -o pipefail
microk8s.kubectl -n {{ item.namespace | default('default') }} get secret | grep {{ item.username }}-token | wc -l | awk '{ print $1 }'
microk8s.kubectl -n {{ item.namespace | default('default') }} get secret | grep {{ item.username }}-token | wc -l | awk '{ print $1 }'
args:
executable: /bin/bash
register: secret_exists
@@ -41,7 +41,7 @@
- name: Get the secret name
ansible.builtin.shell: |
set -o pipefail
microk8s.kubectl -n {{ item.namespace | default('default') }} get secret | grep {{ item.username }}-token | awk '{ print $1 }'
microk8s.kubectl -n {{ item.namespace | default('default') }} get secret | grep {{ item.username }}-token | awk '{ print $1 }'
args:
executable: /bin/bash
register: secret_name
@@ -53,7 +53,7 @@
- name: Describe the secret and extract the token
ansible.builtin.shell: |
set -o pipefail
microk8s.kubectl -n {{ item.namespace | default('default') }} describe secret {{ secret_name.stdout }} | grep token: | awk '{ print $2 }'
microk8s.kubectl -n {{ item.namespace | default('default') }} describe secret {{ secret_name.stdout }} | grep token: | awk '{ print $2 }'
args:
executable: /bin/bash
register: token
@@ -103,7 +103,7 @@
- k8s::users
- name: Get new config SHA1
set_fact:
ansible.builtin.set_fact:
patched_kubeconfig_sha1: "{{ patched_kubeconfig_yaml | to_nice_yaml | sha1 }}"
tags:
- k8s::users
@@ -115,6 +115,6 @@
dest: "{{ user_registered.home }}/.kube/config"
owner: "{{ item.username }}"
mode: "0600"
when: kubeconfig_exists.stat.exists == false or patched_kubeconfig_sha1 != kubeconfig_exists.stat.checksum
when: not kubeconfig_exists.stat.exists or patched_kubeconfig_sha1 != kubeconfig_exists.stat.checksum
tags:
- k8s::users
@@ -0,0 +1,12 @@
---
apiVersion: apiserver.config.k8s.io/v1
kind: EncryptionConfiguration
resources:
- resources:
- secrets
providers:
- aescbc:
keys:
- name: k8s-crypto
secret: {{ secret }}
- identity: {}