ansible/roles/proxmox/tasks/main.yml
2024-09-06 21:32:54 +02:00

134 lines
4.2 KiB
YAML

- name: Get VM list from Proxmox
community.general.proxmox_vm_info:
api_host: "{{ proxmox_host }}"
api_user: "{{ proxmox_user|default('root@pam') }}"
api_token_id: '{{ proxmox_token_id }}'
api_token_secret: '{{ proxmox_token_secret }}'
type: lxc
register: vm_list
- name: Filter managed LXC
set_fact:
lxc_list: |
{%- set r = [] -%}
{%- for lxc in (vm_list['proxmox_vms']) -%}
{%- if "tags" in lxc and 'ansible_managed' in lxc['tags'] -%}
{%- set _ = r.append(lxc) -%}
{%- endif -%}
{%- endfor -%}
{{ r }}
- name: List all LXC names
set_fact:
name_list: |
{%- set r = [] -%}
{%- for lxc in lxc_list -%}
{%- set _ = r.append(lxc['name']) -%}
{%- endfor -%}
{{ r }}
- name: Generate a list of LXC to create
set_fact:
to_create: |
{%- set r = [] -%}
{%- for name in groups['lxc'] -%}
{%- if name not in name_list -%}
{%- set _ = r.append(name) -%}
{%- endif -%}
{%- endfor -%}
{{ r }}
- name: Generate a list of LXC to delete
set_fact:
to_delete: |
{%- set r = [] -%}
{%- for lxc in lxc_list -%}
{%- if lxc['name'] not in groups['lxc'] -%}
{%- set _ = r.append(lxc) -%}
{%- endif -%}
{%- endfor -%}
{{ r }}
- name: Generate a list of LXC to modify
set_fact:
to_modify: |
{%- set r = [] -%}
{%- for lxc in lxc_list -%}
{%- if lxc['name'] in groups['lxc']
and not (lxc['maxcpu'] == hostvars[lxc['name']]['resources']['cpu']
and lxc['maxmem'] == hostvars[lxc['name']]['resources']['ram']*1024*1024
and lxc['maxswap'] == hostvars[lxc['name']]['resources']['swap']*1024*1024)
-%}
{%- set _ = r.append(lxc) -%}
{%- endif -%}
{%- endfor -%}
{{ r }}
- name: debug
debug:
msg: "to create: {{ to_create }}, to delete: {{ to_delete|map(attribute='name') }}, to modify: {{ to_modify|map(attribute='name') }}"
- name: Create LXCs
community.general.proxmox:
vmid: "{{ 200 + (hostvars[item]['ansible_host'].split('.')[-1]|int) }}"
node: "{{ proxmox_node }}"
api_host: "{{ proxmox_host }}"
api_user: "{{ proxmox_user|default('root@pam') }}"
api_token_id: '{{ proxmox_token_id }}'
api_token_secret: '{{ proxmox_token_secret }}'
hostname: "{{ item }}"
pubkey: "{{ proxmox_ssh_pubkey }}"
ostemplate: "{{ proxmox_lxc_image }}"
netif:
net0: "name=eth0,gw={{ hostvars[item]['gateway']|default('10.255.3.254') }},ip={{ hostvars[item]['ansible_host'] }}/24,bridge=vmbr0"
cores: "{{ hostvars[item]['resources']['cpu'] }}"
memory: "{{ hostvars[item]['resources']['ram'] }}"
swap: "{{ hostvars[item]['resources']['swap'] }}"
tags:
- "ansible_managed"
onboot: true
state: present
disk: "zpool1:{{ hostvars[item]['resources']['disk'] }}"
features: "nesting=1"
loop: "{{ to_create }}"
- name: Start created LXCs
community.general.proxmox:
vmid: "{{ 200 + (hostvars[item]['ansible_host'].split('.')[-1]|int) }}"
node: "{{ proxmox_node }}"
api_host: "{{ proxmox_host }}"
api_user: "{{ proxmox_user|default('root@pam') }}"
api_token_id: '{{ proxmox_token_id }}'
api_token_secret: '{{ proxmox_token_secret }}'
state: started
loop: "{{ to_create }}"
- name: Delete LXCs
community.general.proxmox:
vmid: "{{ item['vmid'] }}"
node: "{{ proxmox_node }}"
api_host: "{{ proxmox_host }}"
api_user: "{{ proxmox_user|default('root@pam') }}"
api_token_id: '{{ proxmox_token_id }}'
api_token_secret: '{{ proxmox_token_secret }}'
state: absent
loop: "{{ to_delete }}"
when: allow_deletion
- name: Modify LXCs
community.general.proxmox:
vmid: "{{ item['vmid'] }}"
node: "{{ proxmox_node }}"
api_host: "{{ proxmox_host }}"
api_user: "{{ proxmox_user|default('root@pam') }}"
api_token_id: '{{ proxmox_token_id }}'
api_token_secret: '{{ proxmox_token_secret }}'
cores: "{{ hostvars[item['name']]['resources']['cpu'] }}"
memory: "{{ hostvars[item['name']]['resources']['ram'] }}"
swap: "{{ hostvars[item['name']]['resources']['swap'] }}"
hostname: "{{ item['name'] }}"
update: true
loop: "{{ to_modify }}"