73 lines
2.7 KiB
YAML
73 lines
2.7 KiB
YAML
---
|
|
- name: Build Dynamic Rocky Linux LXC Container
|
|
hosts: localhost
|
|
gather_facts: false
|
|
|
|
vars:
|
|
# --- Container Specifications ---
|
|
lxc_hostname: "new_lxc"
|
|
lxc_template: "local:vztmpl/rockylinux-9-default_20240912_amd64.tar.xz"
|
|
lxc_cores: 2
|
|
lxc_memory: 1024
|
|
lxc_storage: "TB"
|
|
lxc_disk_size: 10
|
|
lxc_bridge: "vmbr0"
|
|
lxc_dns: "192.168.2.2"
|
|
target_node: "pve"
|
|
|
|
# --- Security & Access ---
|
|
lxc_ssh_key: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINegitHaQG8i+s412h3wTWf/KvpeX1CEpYFva9MhHR+7 ansible-key"
|
|
|
|
tasks:
|
|
# ========================================================================
|
|
# NEW FIX: Auto-install missing dependencies into Semaphore's isolated venv
|
|
# ========================================================================
|
|
- name: Ensure Proxmox API dependencies are installed in Semaphore
|
|
ansible.builtin.shell: "{{ ansible_playbook_python }} -m pip install proxmoxer requests"
|
|
changed_when: false
|
|
# ========================================================================
|
|
|
|
- name: Get the next available VMID from the Proxmox cluster
|
|
ansible.builtin.uri:
|
|
url: "https://{{ proxmox_api_host }}:8006/api2/json/cluster/nextid"
|
|
method: GET
|
|
headers:
|
|
Authorization: "PVEAPIToken {{ proxmox_api_user }}={{ proxmox_api_password }}"
|
|
validate_certs: false
|
|
return_content: true
|
|
register: nextid_response
|
|
|
|
- name: Extract the dynamic VMID from the API response
|
|
ansible.builtin.set_fact:
|
|
dynamic_vmid: "{{ nextid_response.json.data }}"
|
|
|
|
- name: Create the Rocky Linux LXC container
|
|
community.general.proxmox:
|
|
api_host: "{{ proxmox_api_host }}"
|
|
api_user: "{{ proxmox_api_user }}"
|
|
api_password: "{{ proxmox_api_password }}"
|
|
|
|
node: "{{ target_node }}"
|
|
vmid: "{{ dynamic_vmid }}"
|
|
hostname: "{{ lxc_hostname }}"
|
|
ostemplate: "{{ lxc_template }}"
|
|
cores: "{{ lxc_cores }}"
|
|
memory: "{{ lxc_memory }}"
|
|
disk: "{{ lxc_storage }}:{{ lxc_disk_size }}"
|
|
netif: '{"net0":"name=eth0,bridge={{ lxc_bridge }},ip=dhcp"}'
|
|
nameserver: "{{ lxc_dns }}"
|
|
pubkey: "{{ lxc_ssh_key }}"
|
|
unprivileged: true
|
|
state: present
|
|
|
|
- name: Ensure the new LXC container is powered on
|
|
community.general.proxmox:
|
|
api_host: "{{ proxmox_api_host }}"
|
|
api_user: "{{ proxmox_api_user }}"
|
|
api_password: "{{ proxmox_api_password }}"
|
|
vmid: "{{ dynamic_vmid }}"
|
|
state: started
|
|
|
|
- name: Output the assigned VMID to the Semaphore UI logs
|
|
ansible.builtin.debug:
|
|
msg: "Successfully built {{ lxc_hostname }} (Rocky Linux 9) with dynamic ID: {{ dynamic_vmid }}" |