92 lines
3.1 KiB
YAML
92 lines
3.1 KiB
YAML
---
|
|
- name: Create and Configure New LXC Container
|
|
hosts: localhost
|
|
gather_facts: no
|
|
vars_files:
|
|
- "../secrets.yml"
|
|
- "../vars.yml"
|
|
vars_prompt:
|
|
- name: container_name
|
|
prompt: "Enter the new container name"
|
|
private: no
|
|
- name: container_ip
|
|
prompt: "Enter the IP address"
|
|
private: no
|
|
- name: container_id
|
|
prompt: "Enter the Container ID (VMID)"
|
|
private: no
|
|
- name: container_cores
|
|
prompt: "Enter CPU Cores"
|
|
default: "2"
|
|
private: no
|
|
- name: container_memory
|
|
prompt: "Enter Memory in MB"
|
|
default: "256"
|
|
private: no
|
|
|
|
tasks:
|
|
- name: Normalize IP address (append /24 if missing)
|
|
set_fact:
|
|
container_ip_cidr: "{{ container_ip if '/' in container_ip else container_ip + '/24' }}"
|
|
|
|
- name: Create LXC container on Proxmox
|
|
community.general.proxmox:
|
|
api_host: "{{ proxmox_host | default('10.0.0.1') }}"
|
|
api_user: "{{ proxmox_api_user }}"
|
|
api_token_id: "{{ proxmox_api_token_id }}"
|
|
api_token_secret: "{{ proxmox_api_token_secret }}"
|
|
node: "{{ proxmox_node }}"
|
|
storage: "{{ proxmox_storage }}"
|
|
ostemplate: '{{ proxmox_storage }}:vztmpl/ubuntu-24.04-standard_24.04-2_amd64.tar.zst'
|
|
vmid: "{{ container_id }}"
|
|
hostname: "{{ container_name }}"
|
|
password: "{{ ansible_password }}"
|
|
netif:
|
|
net0: "name=eth0,gw=10.0.0.1,ip={{ container_ip_cidr }},bridge=vmbr0"
|
|
cores: "{{ container_cores }}"
|
|
memory: "{{ container_memory }}"
|
|
swap: 512
|
|
state: present
|
|
unprivileged: yes
|
|
features:
|
|
- nesting=1
|
|
register: proxmox_creation
|
|
|
|
- name: Start the container
|
|
community.general.proxmox:
|
|
api_host: "{{ proxmox_host | default('10.0.0.1') }}"
|
|
api_user: "{{ proxmox_api_user }}"
|
|
api_token_id: "{{ proxmox_api_token_id }}"
|
|
api_token_secret: "{{ proxmox_api_token_secret }}"
|
|
vmid: "{{ container_id }}"
|
|
state: started
|
|
|
|
- name: Wait for container to be reachable
|
|
wait_for:
|
|
host: "{{ container_ip_cidr | split('/') | first }}"
|
|
port: 22
|
|
search_regex: OpenSSH
|
|
delay: 10
|
|
timeout: 300
|
|
|
|
- name: Add new host to in-memory inventory (group 'new')
|
|
add_host:
|
|
name: "{{ container_name }}"
|
|
groups: new
|
|
ansible_host: "{{ container_ip_cidr | split('/') | first }}"
|
|
ansible_user: root
|
|
ansible_ssh_pass: "{{ ansible_password }}"
|
|
# We need to ignore host key checking for the fresh container to avoid interactive prompt
|
|
ansible_ssh_common_args: '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null'
|
|
|
|
- name: Add new host to local hosts.ini file (persistency)
|
|
ansible.builtin.blockinfile:
|
|
path: "{{ playbook_dir }}/../inventory/hosts.ini"
|
|
block: |
|
|
{{ container_name }} ansible_host={{ container_ip_cidr | split('/') | first }} ansible_user=root
|
|
insertafter: "^\\[new\\]"
|
|
marker: "# {mark} ANSIBLE MANAGED BLOCK FOR NEW HOST {{ container_name }}"
|
|
|
|
- name: Run Standard Setup on New Host
|
|
import_playbook: lxc_setup_ubuntu.yml
|