2024-09-01 23:09:31 +07:00
|
|
|
---
|
|
|
|
- name: rroot disk name
|
|
|
|
ansible.builtin.shell: |
|
|
|
|
fdisk -l | grep -E '^(Devi|/dev)' | sort -nk2,2 |grep -B1 -A1 -w `findmnt -n -o SOURCE /` >/tmp/disks; if [[ `wc -l /tmp/disks |awk '{print $1}'` -gt 1 && `wc -l /tmp/disks |awk '{print $1}'` -ne 3 ]]; then sed '1!D' /tmp/disks; elif [[ `wc -l /tmp/disks |awk '{print $1}'` -eq 3 ]]; then sed '3!D' /tmp/disks; fi |awk '{print $1}'|grep -o '[^/]*$'
|
2024-09-02 02:11:39 +07:00
|
|
|
args:
|
|
|
|
executable: /bin/bash
|
2024-09-01 23:09:31 +07:00
|
|
|
register: rroot_disk
|
|
|
|
ignore_errors: true
|
2024-09-02 02:11:39 +07:00
|
|
|
|
|
|
|
- name: Output result
|
|
|
|
debug:
|
|
|
|
var: rroot_disk.stdout
|
2024-09-01 23:09:31 +07:00
|
|
|
|
|
|
|
- name: Create keyfile
|
|
|
|
ansible.builtin.shell: |
|
|
|
|
openssl genrsa -out /root/keyfile_rroot; chmod 0400 /root/keyfile_rroot
|
|
|
|
ignore_errors: true
|
2024-09-02 02:11:39 +07:00
|
|
|
when: rroot_disk.stdout | length > 0
|
2024-09-01 23:09:31 +07:00
|
|
|
|
|
|
|
- name: Encrypt rroot disk
|
|
|
|
ansible.builtin.shell: |
|
2024-09-02 02:11:39 +07:00
|
|
|
cryptsetup -q luksFormat /dev/{{ rroot_disk.stdout }} --key-file /root/keyfile_rroot
|
2024-09-01 23:09:31 +07:00
|
|
|
ignore_errors: true
|
2024-09-02 02:11:39 +07:00
|
|
|
when: rroot_disk.stdout | length > 0
|
2024-09-01 23:09:31 +07:00
|
|
|
|
|
|
|
- name: Open encrypted rroot disk
|
|
|
|
ansible.builtin.shell: |
|
2024-09-02 02:11:39 +07:00
|
|
|
cryptsetup luksOpen /dev/{{ rroot_disk.stdout }} {{ rroot_disk.stdout }} --key-file /root/keyfile_rroot
|
2024-09-01 23:09:31 +07:00
|
|
|
ignore_errors: true
|
|
|
|
register: rroot_disk_status
|
2024-09-02 02:11:39 +07:00
|
|
|
when: rroot_disk.stdout | length > 0
|
2024-09-01 23:09:31 +07:00
|
|
|
|
|
|
|
- name: Format the encrypted rroot disk
|
2024-09-02 02:11:39 +07:00
|
|
|
command: mkfs.ext4 /dev/mapper/{{ rroot_disk.stdout }}
|
|
|
|
when: rroot_disk.stdout | length > 0
|
2024-09-01 23:09:31 +07:00
|
|
|
|
|
|
|
- name: Ensure the mount point exists
|
|
|
|
file:
|
2024-09-02 02:11:39 +07:00
|
|
|
path: /mnt/{{ rrot_disk.stdout }}
|
2024-09-01 23:09:31 +07:00
|
|
|
state: directory
|
2024-09-02 02:11:39 +07:00
|
|
|
when: rroot_disk.stdout | length > 0
|
2024-09-01 23:09:31 +07:00
|
|
|
|
|
|
|
- name: Mount the encrypted second disk
|
|
|
|
mount:
|
2024-09-02 02:11:39 +07:00
|
|
|
path: /mnt/{{ rroot_disk.stdout }}
|
|
|
|
src: /dev/mapper/{{ rroot_disk.stdout }}
|
2024-09-01 23:09:31 +07:00
|
|
|
fstype: ext4
|
|
|
|
state: mounted
|
2024-09-02 02:11:39 +07:00
|
|
|
when: rroot_disk.stdout | length > 0
|
2024-09-01 23:09:31 +07:00
|
|
|
|
|
|
|
- name: Add crypttab
|
|
|
|
ansible.builtin.shell: |
|
2024-09-02 02:11:39 +07:00
|
|
|
printf "{{ rroot_disk.stdout }} /dev/{{ rroot_disk.stdout }} /root/keyfile luks\n">/etc/crypttab
|
|
|
|
when: rroot_disk.stdout | length > 0
|
2024-09-01 23:09:31 +07:00
|
|
|
|