Add new_lxc.yml
This commit is contained in:
+75
@@ -0,0 +1,75 @@
|
||||
---
|
||||
- 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 ---
|
||||
# The public key to inject into the /root/.ssh/authorized_keys of the new container
|
||||
lxc_ssh_key: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINegitHaQG8i+s412h3wTWf/KvpeX1CEpYFva9MhHR+7 ansible-key"
|
||||
|
||||
tasks:
|
||||
- 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 }}"
|
||||
storage: "{{ lxc_storage }}"
|
||||
disk: "{{ lxc_disk_size }}"
|
||||
|
||||
# Configure network for DHCP on the specified bridge
|
||||
netif: '{"net0":"name=eth0,bridge={{ lxc_bridge }},ip=dhcp"}'
|
||||
|
||||
# Explicitly set the DNS nameserver
|
||||
nameserver: "{{ lxc_dns }}"
|
||||
|
||||
# Inject the SSH public key
|
||||
pubkey: "{{ lxc_ssh_key }}"
|
||||
|
||||
# Highly recommended security flag for Proxmox containers
|
||||
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 }}"
|
||||
Reference in New Issue
Block a user