2025-11-02 08:07:57 +01:00
|
|
|
|
---
|
|
|
|
|
|
- name: Create and provision LXC container on Proxmox
|
|
|
|
|
|
hosts: node0
|
2025-11-01 18:47:03 +01:00
|
|
|
|
gather_facts: no
|
2025-11-02 08:07:57 +01:00
|
|
|
|
become: yes
|
|
|
|
|
|
become_user: root
|
|
|
|
|
|
|
2025-11-01 18:47:03 +01:00
|
|
|
|
vars:
|
2025-11-04 19:18:58 +01:00
|
|
|
|
node_ip: "{{ hostvars['node0']['ansible_host'] }}"
|
|
|
|
|
|
mac_address: '8E:90:31:DE:31:36'
|
2025-11-02 08:07:57 +01:00
|
|
|
|
|
|
|
|
|
|
container_id: 200
|
2025-11-05 18:39:13 +01:00
|
|
|
|
container_template: '/var/lib/vz/template/cache/ubuntu-25.04-standard_25.04-1.1_amd64.tar.zst'
|
|
|
|
|
|
# container_template: '/var/lib/vz/template/cache/debian-13-standard_13.1-1_amd64.tar.zst'
|
2025-11-04 21:11:31 +01:00
|
|
|
|
# container_template: '/var/lib/vz/template/cache/debian-12-standard_12.12-1_amd64.tar.zst'
|
2025-11-05 18:39:13 +01:00
|
|
|
|
container_ostype: ubuntu
|
|
|
|
|
|
# container_ostype: debian
|
2025-11-04 19:18:58 +01:00
|
|
|
|
container_hostname: '{{ addc_hostname }}'
|
|
|
|
|
|
container_password: '{{ addc_admin_password }}'
|
2025-11-02 08:07:57 +01:00
|
|
|
|
container_storage: local-lvm
|
|
|
|
|
|
container_rootfs_size: 8G
|
|
|
|
|
|
container_memory: 1024
|
2025-11-02 08:20:59 +01:00
|
|
|
|
container_swap: 256
|
2025-11-02 08:07:57 +01:00
|
|
|
|
container_cores: 2
|
2025-11-02 08:36:08 +01:00
|
|
|
|
container_net: name=eth0,bridge=vmbr0,ip={{ addc_ansible_host }}/24,gw={{ location_gateway }},hwaddr={{ mac_address }}
|
2025-11-04 19:18:58 +01:00
|
|
|
|
container_features: 'keyctl=1,nesting=1,mount=cifs'
|
2025-11-02 08:27:47 +01:00
|
|
|
|
container_description: default lxc
|
2025-11-02 08:36:08 +01:00
|
|
|
|
container_onboot: 1
|
|
|
|
|
|
container_protection: 0
|
|
|
|
|
|
container_unprivileged: 1
|
2025-11-04 19:18:58 +01:00
|
|
|
|
container_pubkey: '{{ ssh_public_keys[0] }}'
|
2025-11-02 08:40:18 +01:00
|
|
|
|
container_tags:
|
|
|
|
|
|
- ansible_managed
|
|
|
|
|
|
- test
|
2025-11-02 08:20:59 +01:00
|
|
|
|
|
2025-11-01 18:47:03 +01:00
|
|
|
|
tasks:
|
2025-11-02 08:07:57 +01:00
|
|
|
|
|
2025-11-02 10:21:23 +01:00
|
|
|
|
- name: Combine SSH public keys into one file
|
|
|
|
|
|
ansible.builtin.copy:
|
2025-11-04 19:18:58 +01:00
|
|
|
|
dest: '{{ ssh_keys_file }}'
|
2025-11-02 10:21:23 +01:00
|
|
|
|
content: |
|
|
|
|
|
|
{% for key in ssh_public_keys %}
|
|
|
|
|
|
{{ key }}
|
|
|
|
|
|
{% endfor %}
|
|
|
|
|
|
mode: '0644'
|
|
|
|
|
|
|
2025-11-04 19:06:26 +01:00
|
|
|
|
- name: debug
|
|
|
|
|
|
# Comment
|
|
|
|
|
|
ansible.builtin.debug:
|
2025-11-04 19:18:58 +01:00
|
|
|
|
msg: 'Hello world! {{ container_password }}'
|
2025-11-04 19:06:26 +01:00
|
|
|
|
verbosity: 0
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-11-02 09:06:44 +01:00
|
|
|
|
- name: Create LXC container {{ container_hostname }} with id {{ container_id }} using pct command on shell
|
2025-11-02 07:35:28 +01:00
|
|
|
|
ansible.builtin.shell: |
|
2025-11-04 19:12:16 +01:00
|
|
|
|
set -e
|
2025-11-02 08:07:57 +01:00
|
|
|
|
pct create {{ container_id }} {{ container_template }} \
|
2025-11-02 08:20:59 +01:00
|
|
|
|
-ostype {{ container_ostype }} \
|
2025-11-02 08:07:57 +01:00
|
|
|
|
-hostname {{ container_hostname }} \
|
2025-11-04 19:18:58 +01:00
|
|
|
|
-password '{{ container_password }}' \
|
|
|
|
|
|
-ssh-public-keys '{{ ssh_keys_file }}' \
|
2025-11-02 08:07:57 +01:00
|
|
|
|
-cores {{ container_cores }} \
|
|
|
|
|
|
-memory {{ container_memory }} \
|
2025-11-02 08:20:59 +01:00
|
|
|
|
-swap {{ container_swap }} \
|
2025-11-04 19:18:58 +01:00
|
|
|
|
-net0 '{{container_net}}' \
|
2025-11-02 08:07:57 +01:00
|
|
|
|
-storage {{ container_storage }} \
|
2025-11-04 19:18:58 +01:00
|
|
|
|
-description '{{ container_description }}' \
|
2025-11-02 08:36:08 +01:00
|
|
|
|
-onboot {{ container_onboot }} \
|
|
|
|
|
|
-protection {{ container_protection }} \
|
|
|
|
|
|
-unprivileged {{ container_unprivileged }} \
|
2025-11-04 19:18:58 +01:00
|
|
|
|
-tags '{{ container_tags | join(',') }}' \
|
|
|
|
|
|
-features '{{ container_features }}'
|
2025-11-02 08:20:59 +01:00
|
|
|
|
args:
|
2025-11-04 19:18:58 +01:00
|
|
|
|
creates: '/etc/pve/lxc/{{ container_id }}.conf'
|
2025-11-04 17:49:52 +01:00
|
|
|
|
no_log: false
|
2025-11-03 17:38:12 +01:00
|
|
|
|
# -password {{ container_password }} \
|
2025-11-02 12:26:02 +01:00
|
|
|
|
# -rootfs {{ container_storage }}:{{ container_id }}/vm-{{ container_id }}-disk-0.raw,size=7G \
|
|
|
|
|
|
|
2025-11-02 08:07:57 +01:00
|
|
|
|
# -timezone: {{ localization_timezone }} \
|
2025-11-02 09:20:37 +01:00
|
|
|
|
|
2025-11-02 12:36:17 +01:00
|
|
|
|
- name: Check if LXC container {{ container_hostname }} is running
|
|
|
|
|
|
ansible.builtin.command:
|
|
|
|
|
|
cmd: pct status {{ container_id }}
|
|
|
|
|
|
register: pct_status
|
|
|
|
|
|
changed_when: false
|
|
|
|
|
|
|
|
|
|
|
|
- name: Start the LXC container {{ container_hostname }} if stopped
|
2025-11-02 09:20:37 +01:00
|
|
|
|
ansible.builtin.command:
|
|
|
|
|
|
cmd: pct start {{ container_id }}
|
2025-11-02 12:36:17 +01:00
|
|
|
|
when: "'status: stopped' in pct_status.stdout"
|
2025-11-02 09:20:37 +01:00
|
|
|
|
register: start_result
|
2025-11-02 12:36:17 +01:00
|
|
|
|
changed_when: "'status: stopped' in pct_status.stdout"
|
2025-11-02 09:20:37 +01:00
|
|
|
|
|
2025-11-02 11:11:01 +01:00
|
|
|
|
- name: Wait until container has an IP address
|
2025-11-02 17:06:49 +01:00
|
|
|
|
ansible.builtin.shell: "pct exec {{ container_id }} -- hostname -I | awk '{print $1}'"
|
2025-11-02 11:11:01 +01:00
|
|
|
|
register: lxc_ip
|
2025-11-04 19:18:58 +01:00
|
|
|
|
until: lxc_ip.stdout != ''
|
2025-11-02 11:11:01 +01:00
|
|
|
|
retries: 10
|
|
|
|
|
|
delay: 5
|
|
|
|
|
|
changed_when: false
|
2025-11-04 19:18:58 +01:00
|
|
|
|
failed_when: lxc_ip.stdout == ''
|
2025-11-02 11:11:01 +01:00
|
|
|
|
|
|
|
|
|
|
- name: Wait for SSH to become available
|
|
|
|
|
|
ansible.builtin.wait_for:
|
2025-11-04 19:18:58 +01:00
|
|
|
|
host: '{{ lxc_ip.stdout }}'
|
2025-11-02 11:11:01 +01:00
|
|
|
|
port: 22
|
|
|
|
|
|
delay: 5
|
|
|
|
|
|
timeout: 60
|
2025-11-02 16:59:48 +01:00
|
|
|
|
retries: 10
|
|
|
|
|
|
delay: 5
|
|
|
|
|
|
changed_when: false
|
|
|
|
|
|
|
|
|
|
|
|
# --- DC‑1 Provisioning Play --------------------------------------------
|
2025-11-02 11:04:30 +01:00
|
|
|
|
|
2025-11-02 09:53:07 +01:00
|
|
|
|
- name: Provision dc1 LXC
|
2025-11-02 09:33:36 +01:00
|
|
|
|
hosts: dc1
|
2025-11-04 20:41:13 +01:00
|
|
|
|
gather_facts: true
|
2025-11-02 09:53:07 +01:00
|
|
|
|
become: true
|
|
|
|
|
|
|
2025-11-04 17:31:48 +01:00
|
|
|
|
# vars:
|
2025-11-04 19:18:58 +01:00
|
|
|
|
# addc_admin_password: '{{ addc_adminpass }}'
|
2025-11-03 21:16:30 +01:00
|
|
|
|
|
2025-11-02 09:53:07 +01:00
|
|
|
|
tasks:
|
2025-11-02 10:04:28 +01:00
|
|
|
|
|
2025-11-02 11:04:30 +01:00
|
|
|
|
# - name: Ensure SSH authorized keys are present
|
|
|
|
|
|
# ansible.posix.authorized_key:
|
|
|
|
|
|
# user: root
|
2025-11-04 19:18:58 +01:00
|
|
|
|
# key: '{{ item }}'
|
2025-11-02 11:04:30 +01:00
|
|
|
|
# state: present
|
2025-11-04 19:18:58 +01:00
|
|
|
|
# loop: '{{ ssh_public_keys }}'
|
2025-11-02 09:53:07 +01:00
|
|
|
|
|
2025-11-02 11:59:14 +01:00
|
|
|
|
- name: Install useful packages
|
2025-11-02 09:53:07 +01:00
|
|
|
|
ansible.builtin.package:
|
|
|
|
|
|
name:
|
|
|
|
|
|
- nano
|
2025-11-02 11:04:30 +01:00
|
|
|
|
- tzdata
|
2025-11-02 10:42:14 +01:00
|
|
|
|
# - openssh-server
|
2025-11-02 09:53:07 +01:00
|
|
|
|
state: present
|
2025-11-02 11:04:30 +01:00
|
|
|
|
|
2025-11-02 16:59:48 +01:00
|
|
|
|
- name: Update all packages, autoclean, and autoremove
|
2025-11-02 11:59:14 +01:00
|
|
|
|
ansible.builtin.apt:
|
2025-11-04 19:18:58 +01:00
|
|
|
|
name: '*'
|
2025-11-02 11:59:14 +01:00
|
|
|
|
state: latest
|
|
|
|
|
|
autoclean: yes
|
|
|
|
|
|
autoremove: yes
|
|
|
|
|
|
purge: true
|
|
|
|
|
|
|
2025-11-02 11:04:30 +01:00
|
|
|
|
- name: Set timezone to {{ localization_timezone }}
|
|
|
|
|
|
community.general.timezone:
|
2025-11-04 19:18:58 +01:00
|
|
|
|
name: '{{ localization_timezone }}'
|
2025-11-02 16:59:48 +01:00
|
|
|
|
notify: Restart sshd
|
|
|
|
|
|
tags: [timezone]
|
|
|
|
|
|
|
2025-11-05 19:11:44 +01:00
|
|
|
|
- name: Ensure update-manager-core is installed
|
|
|
|
|
|
ansible.builtin.apt:
|
|
|
|
|
|
name: update-manager-core
|
|
|
|
|
|
state: present
|
|
|
|
|
|
update_cache: true
|
|
|
|
|
|
|
|
|
|
|
|
- name: Run Ubuntu release upgrade (non-interactive)
|
|
|
|
|
|
ansible.builtin.command:
|
|
|
|
|
|
cmd: do-release-upgrade -f DistUpgradeViewNonInteractive
|
|
|
|
|
|
register: upgrade_output
|
|
|
|
|
|
changed_when: "'No new release found' not in upgrade_output.stdout"
|
|
|
|
|
|
|
|
|
|
|
|
- name: Reboot if kernel updated
|
|
|
|
|
|
ansible.builtin.reboot:
|
|
|
|
|
|
msg: "Rebooting after Ubuntu upgrade"
|
|
|
|
|
|
connect_timeout: 5
|
|
|
|
|
|
reboot_timeout: 600
|
|
|
|
|
|
when: upgrade_output is changed
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-11-03 17:47:09 +01:00
|
|
|
|
- name: Deploy the Samba AD DC role
|
|
|
|
|
|
ansible.builtin.include_role:
|
|
|
|
|
|
name: ansible_samba_ad_dc
|
2025-11-02 20:44:01 +01:00
|
|
|
|
|
2025-11-02 16:59:48 +01:00
|
|
|
|
# --- Global Handlers ----------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
handlers:
|
|
|
|
|
|
- name: Restart sshd
|
|
|
|
|
|
ansible.builtin.service:
|
|
|
|
|
|
name: ssh
|
|
|
|
|
|
state: restarted
|
|
|
|
|
|
when: localization_timezone | bool
|