aboutsummaryrefslogtreecommitdiff
path: root/roles/host/tasks/main.yaml
blob: 007d44d9cf2d0964e95f85b2c38763a73d43958a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
- name: Install firewalld and systemd-networkd packages
  ansible.builtin.dnf:
    name:
      - firewalld
      - systemd-container
      - systemd-networkd
      - openssl
      - python3-libsemanage
      - policycoreutils
    state: present

- name: Configure Polkit to allow machinectl fast user auth
  ansible.builtin.copy:
    src: 60-machinectl-fast-user-auth.rules
    dest: /etc/polkit-1/rules.d/60-machinectl-fast-user-auth.rules
    mode: "0644"
  notify: Restart Polkit

- name: Ensure firewalld and systemd-networkd are running
  ansible.builtin.systemd:
    name: "{{ item }}"
    state: started
    enabled: yes
  loop:
    - firewalld
    - systemd-networkd

- name: Enable masquerading for container internet access
  ansible.posix.firewalld:
    masquerade: yes
    state: enabled
    permanent: yes
    zone: public
  notify: Reload Firewalld

- name: Open HTTP and HTTPS (TCP) for Nginx
  ansible.posix.firewalld:
    service: "{{ item }}"
    state: enabled
    permanent: yes
    zone: public
  loop:
    - http
    - https
  notify: Reload Firewalld

- name: Open HTTPS (UDP) for HTTP/3 QUIC support
  ansible.posix.firewalld:
    port: 443/udp
    state: enabled
    permanent: yes
    zone: public
  notify: Reload Firewalld

- name: Configure Host Firewall Port Forwarding dynamically
  ansible.posix.firewalld:
    port_forward:
      - port: "{{ item.1 }}"
        proto: tcp
        toaddr: "{{ item.0.value.ip }}"
        toport: "{{ item.1 }}"
    permanent: yes
    state: enabled
  loop: "{{ containers | dict2items | subelements('value.forward_ports', skip_missing=True) }}"
  notify: Reload Firewalld

- name: Create bridge netdev on host
  ansible.builtin.copy:
    dest: /etc/systemd/network/10-nspawn-br0.netdev
    content: |
      [NetDev]
      Name=nspawn-br0
      Kind=bridge
  notify: Restart Host systemd-networkd

- name: Assign IP to the host bridge
  ansible.builtin.copy:
    dest: /etc/systemd/network/10-nspawn-br0.network
    content: |
      [Match]
      Name=nspawn-br0
      [Network]
      Address={{ bridge_ip }}/24
  notify: Restart Host systemd-networkd

- name: Apply networking changes immediately
  ansible.builtin.meta: flush_handlers

- name: Ensure containers are bootstrapped (AlmaLinux 10)
  ansible.builtin.dnf:
    installroot: "/var/lib/machines/{{ item.key }}"
    releasever: "10"
    name:
      - almalinux-release
      - systemd
      - dbus
      - systemd-networkd
      - passwd
      - dnf
      - iproute
    state: present
  loop: "{{ containers | dict2items }}"

- name: Initialize machine-id for containers
  ansible.builtin.command: systemd-machine-id-setup --root=/var/lib/machines/{{ item.key }}
  args:
    creates: "/var/lib/machines/{{ item.key }}/etc/machine-id"
  loop: "{{ containers | dict2items }}"

- name: Fix SELinux contexts in container rootfs
  ansible.builtin.command: restorecon -R /var/lib/machines/{{ item.key }}
  register: restorecon_result
  changed_when: "'Relabeled' in restorecon_result.stdout"
  loop: "{{ containers | dict2items }}"

- name: Ensure systemd-nspawn directory exists
  ansible.builtin.file:
    path: /etc/systemd/nspawn
    state: directory
    mode: "0755"

- name: Ensure Let's Encrypt mock directories exist
  ansible.builtin.file:
    path: "{{ item }}"
    state: directory
    mode: '0755'
  loop:
    - "/etc/letsencrypt/archive/{{ vault_public_domain }}"
    - "/etc/letsencrypt/live/{{ vault_public_domain }}"

- name: Check if Let's Encrypt certificate exists
  ansible.builtin.stat:
    path: "/etc/letsencrypt/live/{{ vault_public_domain }}/fullchain.pem"
  register: le_cert

- name: Generate self-signed fallback cert in Let's Encrypt paths if missing
  ansible.builtin.command: >
    openssl req -x509 -nodes -days 365
    -newkey ec -pkeyopt ec_paramgen_curve:prime256v1
    -keyout /etc/letsencrypt/live/{{ vault_public_domain }}/privkey.pem
    -out /etc/letsencrypt/live/{{ vault_public_domain }}/fullchain.pem
    -subj "/CN={{ vault_public_domain }}"
  args:
    creates: "/etc/letsencrypt/live/{{ vault_public_domain }}/fullchain.pem"
  when: not le_cert.stat.exists

- name: Create systemd-nspawn configuration
  ansible.builtin.template:
    src: nspawn.j2
    dest: "/etc/systemd/nspawn/{{ item.key }}.nspawn"
  loop: "{{ containers | dict2items }}"
  notify: Restart Containers

- name: Enable and Start Containers
  ansible.builtin.systemd:
    name: "systemd-nspawn@{{ item.key }}"
    state: started
    enabled: yes
  loop: "{{ containers | dict2items }}"

- name: Install Nginx
  ansible.builtin.dnf:
    name: nginx
    state: present

- name: Allow Nginx to connect to upstream servers (SELinux)
  ansible.posix.seboolean:
    name: httpd_can_network_connect
    state: yes
    persistent: yes

- name: Configure Nginx dynamically
  ansible.builtin.template:
    src: nginx.conf.j2
    dest: /etc/nginx/nginx.conf
    validate: nginx -t -c %s
  notify: Restart Nginx

- name: Ensure Nginx is enabled and running
  ansible.builtin.systemd:
    name: nginx
    state: started
    enabled: yes

- name: Ensure Let's Encrypt renewal hook directory exists
  ansible.builtin.file:
    path: /etc/letsencrypt/renewal-hooks/post
    state: directory
    mode: '0755'

- name: Deploy Let's Encrypt post-renewal hook for Nginx
  ansible.builtin.template:
    src: nginx-reload.sh.j2
    dest: /etc/letsencrypt/renewal-hooks/post/nginx-reload.sh
    mode: '0755'