96 lines
2.8 KiB
YAML
Executable File
96 lines
2.8 KiB
YAML
Executable File
# -----------------------------
|
|
# 002_sub_create_ct.yaml
|
|
# -----------------------------
|
|
|
|
- name: Reset facts
|
|
set_fact:
|
|
net_config: ""
|
|
features_string: ""
|
|
|
|
- name: Normalize storage names
|
|
set_fact:
|
|
template_storage_clean: "{{ item['Template Storage'].split(' ')[0] | trim }}"
|
|
disk_storage_clean: "{{ item['Disk Storage'].split(' ')[0] | trim }}"
|
|
|
|
- name: Build ostemplate path
|
|
set_fact:
|
|
ostemplate_path: "{{ template_storage_clean }}:vztmpl/{{ item.Template }}"
|
|
|
|
- name: Build network config
|
|
set_fact:
|
|
net_config: "{{ 'name=eth0,bridge=' ~ item.Bridge ~ ',ip=' ~ (item['IPv4/CIDR'] | default('dhcp')) ~ (',gw=' ~ item['Gateway(IPv4)'] if item['Gateway(IPv4)'] else '') }}"
|
|
|
|
- name: Build features string
|
|
set_fact:
|
|
features_string: "{{ ['nesting=1' if item.Nesting | default(False) else '', 'keyctl=1' if item.get('Keyctl', False) else ''] | reject('equalto','') | join(',') }}"
|
|
|
|
- name: Show CT variables
|
|
debug:
|
|
msg:
|
|
- "CT ID: {{ item['CT ID'] }}"
|
|
- "Node: {{ item.Node }}"
|
|
- "Hostname: {{ item.Hostname }}"
|
|
- "Net: {{ net_config }}"
|
|
- "Features: {{ features_string }}"
|
|
- "Template Path: {{ ostemplate_path }}"
|
|
|
|
- name: Cleanup strings
|
|
set_fact:
|
|
net_config: "{{ net_config | trim }}"
|
|
features_string: "{{ features_string | trim }}"
|
|
|
|
# -----------------------------
|
|
# CHECK IF CT EXISTS
|
|
# -----------------------------
|
|
- name: Check if CT exists
|
|
ansible.builtin.uri:
|
|
url: "https://{{ hostvars[node_map[item.Node]].ansible_host }}:8006/api2/json/nodes/{{ item.Node }}/lxc/{{ item['CT ID'] }}/status/current"
|
|
method: GET
|
|
headers:
|
|
Authorization: "PVEAPIToken=root@pam!ansible={{ api_token_secret }}"
|
|
validate_certs: false
|
|
register: ct_check
|
|
failed_when: false
|
|
delegate_to: localhost
|
|
|
|
- name: Set CT exists fact
|
|
set_fact:
|
|
ct_exists: "{{ ct_check.status == 200 }}"
|
|
|
|
# -----------------------------
|
|
# CREATE CT
|
|
# -----------------------------
|
|
- name: Create LXC Container if not exists
|
|
community.general.proxmox:
|
|
api_host: "{{ hostvars[node_map[item.Node]].ansible_host }}"
|
|
api_user: "root@pam"
|
|
api_token_id: "ansible"
|
|
api_token_secret: "{{ api_token_secret }}"
|
|
validate_certs: false
|
|
|
|
node: "{{ item.Node }}"
|
|
vmid: "{{ item['CT ID'] }}"
|
|
hostname: "{{ item.Hostname }}"
|
|
cores: "{{ item.Cores }}"
|
|
memory: "{{ item.Memory }}"
|
|
swap: "{{ item.Swap }}"
|
|
|
|
ostemplate: "{{ ostemplate_path | trim }}"
|
|
disk: "{{ item.Disk | default('8') }}"
|
|
storage: "{{ disk_storage_clean | trim }}"
|
|
|
|
netif:
|
|
net0: "{{ net_config }}"
|
|
|
|
features: "{{ features_string }}"
|
|
|
|
state: present
|
|
when: not ct_exists
|
|
|
|
# -----------------------------
|
|
# DEBUG RESULT
|
|
# -----------------------------
|
|
- name: Show result
|
|
debug:
|
|
msg: "CT {{ item['CT ID'] }} created"
|
|
when: not ct_exists |