feat: Create role

This commit is contained in:
2024-05-13 18:03:03 +02:00
parent ff3a9a8e69
commit 0d467d737d
11 changed files with 569 additions and 4 deletions

24
tasks/get_images.yml Normal file
View File

@@ -0,0 +1,24 @@
---
# tasks file for proxmox
- name: Create images storage directory
ansible.builtin.file:
path: "{{ item.dest | ansible.builtin.dirname }}"
state: directory
mode: u=rwX,g=rX,o=rX
become: true
delegate_to: "{{ proxmox_delegate_to | default(omit) }}"
loop: "{{ proxmox_images }}"
loop_control:
label: "{{ item.dest | ansible.builtin.dirname }}"
- name: Download images
ansible.builtin.get_url:
url: "{{ item.url }}"
dest: "{{ item.dest }}"
mode: u=rw,g=r,o=r
become: true
delegate_to: "{{ proxmox_delegate_to | default(omit) }}"
loop: "{{ proxmox_images }}"
loop_control:
label: "{{ item.dest | ansible.builtin.basename }}"

41
tasks/ha.yml Normal file
View File

@@ -0,0 +1,41 @@
---
# tasks file for proxmox
- name: Add Proxmox VE host as HA group
ansible.builtin.blockinfile:
path: /etc/pve/ha/groups.cfg
append_newline: true
prepend_newline: true
block: |
group: {{ proxmox_instance_node }}
nodes {{ proxmox_instance_node }}
nofailback 0
restricted 0
marker: "# {mark} {{ proxmox_instance_node }}"
become: true
delegate_to: "{{ proxmox_delegate_to }}"
- name: Add VM to HA group
community.proxmox.proxmox_cluster_ha_resources:
api_host: "{{ proxmox_api_host }}"
api_token_id: "{{ proxmox_api_token_id }}"
api_token_secret: "{{ proxmox_api_token_secret }}"
api_user: "{{ proxmox_api_user }}"
group: "{{ proxmox_instance_ha.group }}"
hastate: "{{ proxmox_instance_ha.state | default('started') }}"
max_relocate: "{{ proxmox_instance_ha.max_relocate | default(2) }}"
max_restart: "{{ proxmox_instance_ha.max_restart | default(3) }}"
name: "{{ inventory_hostname }}"
state: present
delegate_to: "{{ proxmox_delegate_to | default(omit) }}"
- name: Add ansible managed header
ansible.builtin.lineinfile:
path: "{{ item }}"
line: "# Ansible managed"
insertafter: BOF
become: true
loop:
- /etc/pve/ha/groups.cfg
- /etc/pve/ha/resources.cfg
delegate_to: "{{ proxmox_delegate_to }}"

31
tasks/main.yml Normal file
View File

@@ -0,0 +1,31 @@
---
# tasks file for proxmox
- name: Install prerequisite
ansible.builtin.apt:
name: python3-proxmoxer
update_cache: true
become: true
run_once: true
delegate_to: "{{ proxmox_delegate_to | default(omit) }}"
- name: Import image download tasks
ansible.builtin.import_tasks:
file: get_images.yml
when: proxmox_instance_type == "vm"
- name: Import instance creation tasks
ansible.builtin.import_tasks:
file: vm_template.yml
when: proxmox_instance_type == "vm"
- name: Import instance configuration tasks
ansible.builtin.import_tasks:
file: vm_configuration.yml
when: proxmox_instance_type == "vm"
# Disabled until the community.proxmox module is compatible with Proxmox VE 9
# - name: Import HA configuration tasks
# ansible.builtin.import_tasks:
# file: ha.yml
# when: proxmox_instance_ha | length > 0

73
tasks/update_pve.yml Normal file
View File

@@ -0,0 +1,73 @@
---
- name: Update apt cache
ansible.builtin.apt:
update_cache: true
become: true
- name: List upgradable packages
ansible.builtin.command:
argv:
- apt
- list
- --upgradable
register: apt_upgradable
changed_when: apt_upgradable.stdout_lines | length > 1
notify: Upgrade the node
check_mode: false
- name: Flush handlers
ansible.builtin.meta: flush_handlers
- name: Clean apt things
ansible.builtin.apt:
clean: true
become: true
- name: List installed packages
ansible.builtin.command: # noqa: no-changed-when
argv:
- apt
- list
- --installed
register: apt_list
check_mode: false
- name: Remove old kernels
ansible.builtin.apt:
name: "{{ installed_kernels[:-2] }}"
state: absent
when: installed_kernels | length > 2
become: true
vars:
installed_kernels:
apt_list.stdout_lines |
select('search', '^proxmox-kernel') |
select('search', 'automatic') |
split('/') | first | list
check_mode: true
- name: Disable maintenance mode
ansible.builtin.command: # noqa: no-changed-when
argv:
- ha-manager
- crm-command
- node-maintenance
- disable
- "{{ inventory_hostname_short }}"
become: true
- name: Wait node is active in ha-manager
ansible.builtin.command: # noqa: no-changed-when
argv:
- ha-manager
- status
become: true
register: ha_manager_status
retries: 10
delay: 60
until: >
not
ha_manager_status.stdout_lines |
select('search', 'lrm ' + inventory_hostname_short) |
regex_search('active', ignorecase=true)

149
tasks/vm_configuration.yml Normal file
View File

@@ -0,0 +1,149 @@
---
# tasks file for proxmox
- name: Build disks list
ansible.builtin.set_fact:
proxmox_instance_disks: "{{ proxmox_instance_disks + specific }}"
when: specific | length > 0
loop: "{{ lookup('ansible.builtin.varnames', '^proxmox_instance_disks_.+', wantlist=True) }}"
vars:
specific: "{{ lookup('ansible.builtin.vars', item, default='') }}"
- name: Configure disks
community.proxmox.proxmox_disk:
api_host: "{{ proxmox_api_host }}"
api_user: "{{ proxmox_api_user }}"
api_token_id: "{{ proxmox_api_token_id }}"
api_token_secret: "{{ proxmox_api_token_secret }}"
aio: "{{ item.aio | default(omit) }}"
backup: "{{ item.backup | default(omit) }}"
cache: "{{ item.cache | default(omit) }}"
disk: "{{ item.disk }}"
iothread: "{{ item.iothread | default(omit) }}"
mbps: "{{ item.mbps | default(omit) }}"
name: "{{ inventory_hostname }}"
storage: "{{ item.storage }}"
size: "{{ item.size }}"
state: present
vmid: "{{ proxmox_instance_vmid }}"
loop: "{{ proxmox_instance_disks }}"
loop_control:
label: "{{ item.disk }}"
index_var: disk_number
delegate_to: "{{ proxmox_delegate_to | default(omit) }}"
- name: Get instance informations
community.proxmox.proxmox_vm_info:
api_host: "{{ proxmox_api_host }}"
api_user: "{{ proxmox_api_user }}"
api_token_id: "{{ proxmox_api_token_id }}"
api_token_secret: "{{ proxmox_api_token_secret }}"
config: current
name: "{{ inventory_hostname }}"
vmid: "{{ proxmox_instance_vmid }}"
delegate_to: "{{ proxmox_delegate_to | default(omit) }}"
register: instance_current_infos
- name: Grow disk size
community.proxmox.proxmox_disk:
api_host: "{{ proxmox_api_host }}"
api_user: "{{ proxmox_api_user }}"
api_token_id: "{{ proxmox_api_token_id }}"
api_token_secret: "{{ proxmox_api_token_secret }}"
aio: "{{ item.aio | default(omit) }}"
backup: "{{ item.backup | default(omit) }}"
cache: "{{ item.cache | default(omit) }}"
disk: "{{ item.disk }}"
iothread: "{{ item.iothread | default(omit) }}"
mbps: "{{ item.mbps | default(omit) }}"
name: "{{ inventory_hostname }}"
storage: "{{ item.storage }}"
size: "{{ item.size }}G"
state: resized
vmid: "{{ proxmox_instance_vmid }}"
when:
- proxmox_instance_disks | length > 0
- instance_current_infos.proxmox_vms | length > 0
- formated_size not in instance_current_infos.proxmox_vms[0].config[item.disk]
loop: "{{ proxmox_instance_disks }}"
loop_control:
label: "{{ item.disk }}"
index_var: disk_number
delegate_to: "{{ proxmox_delegate_to | default(omit) }}"
vars:
formated_size: "size={{ item.size }}G"
- name: Reconfigure instance
community.proxmox.proxmox_kvm:
api_host: "{{ proxmox_api_host }}"
api_user: "{{ proxmox_api_user }}"
api_token_id: "{{ proxmox_api_token_id }}"
api_token_secret: "{{ proxmox_api_token_secret }}"
agent: "enabled=1,fstrim_cloned_disks=1"
autostart: "{{ proxmox_instance_autostart }}"
cores: "{{ proxmox_instance_cores }}"
cpu: "{{ proxmox_instance_cpu }}"
hotplug: "{{ proxmox_instance_hotplug | join(',') }}"
ipconfig: "{{ proxmox_instance_ipconfig }}"
memory: "{{ proxmox_instance_memory }}"
name: "{{ inventory_hostname }}"
nameservers: "{{ proxmox_instance_cloudinit_nameservers }}"
node: "{{ proxmox_instance_node }}"
numa_enabled: "{{ proxmox_instance_numa }}"
onboot: "{{ proxmox_instance_onboot }}"
protection: "{{ proxmox_instance_protection }}"
tablet: false
vmid: "{{ proxmox_instance_vmid }}"
update: true
update_unsafe: true
delegate_to: "{{ proxmox_delegate_to | default(omit) }}"
- name: Get changed information about the instance
community.proxmox.proxmox_vm_info:
api_host: "{{ proxmox_api_host }}"
api_user: "{{ proxmox_api_user }}"
api_token_id: "{{ proxmox_api_token_id }}"
api_token_secret: "{{ proxmox_api_token_secret }}"
config: pending
name: "{{ inventory_hostname }}"
vmid: "{{ proxmox_instance_vmid }}"
node: "{{ proxmox_instance_node }}"
when: not create_instance.changed
delegate_to: "{{ proxmox_delegate_to | default(omit) }}"
register: instance_pending_infos
- name: Reboot the instance
community.proxmox.proxmox_kvm:
api_host: "{{ proxmox_api_host }}"
api_user: "{{ proxmox_api_user }}"
api_token_id: "{{ proxmox_api_token_id }}"
api_token_secret: "{{ proxmox_api_token_secret }}"
name: "{{ inventory_hostname }}"
node: "{{ proxmox_instance_node }}"
state: restarted
timeout: 300
when:
- proxmox_reboot_instance
- not create_instance.changed
- instance_current_infos.proxmox_vms[0].config != instance_pending_infos.proxmox_vms[0].config
delegate_to: "{{ proxmox_delegate_to | default(omit) }}"
notify: Attendre que le port SSH soit ouvert
register: rebooted
- name: Flush handlers
ansible.builtin.meta: flush_handlers
- name: Ensure instance is started
community.proxmox.proxmox_kvm:
api_host: "{{ proxmox_api_host }}"
api_user: "{{ proxmox_api_user }}"
api_token_id: "{{ proxmox_api_token_id }}"
api_token_secret: "{{ proxmox_api_token_secret }}"
name: "{{ inventory_hostname }}"
node: "{{ proxmox_instance_node }}"
state: started
delegate_to: "{{ proxmox_delegate_to | default(omit) }}"
notify: Attendre que le port SSH soit ouvert
- name: Flush handlers
ansible.builtin.meta: flush_handlers

71
tasks/vm_template.yml Normal file
View File

@@ -0,0 +1,71 @@
---
# tasks file for proxmox
- name: Create instance
community.proxmox.proxmox_kvm:
api_host: "{{ proxmox_api_host }}"
api_user: "{{ proxmox_api_user }}"
api_token_id: "{{ proxmox_api_token_id }}"
api_token_secret: "{{ proxmox_api_token_secret }}"
agent: "enabled=1,fstrim_cloned_disks=1"
autostart: "{{ proxmox_instance_autostart }}"
cores: "{{ proxmox_instance_cores }}"
cpu: "{{ proxmox_instance_cpu }}"
hotplug: "{{ proxmox_instance_hotplug | join(',') }}"
ipconfig: "{{ proxmox_instance_ipconfig }}"
memory: "{{ proxmox_instance_memory }}"
name: "{{ inventory_hostname }}"
nameservers: "{{ proxmox_instance_cloudinit_nameservers }}"
net: "{{ proxmox_instance_net }}"
node: "{{ proxmox_instance_node }}"
numa_enabled: "{{ proxmox_instance_numa }}"
onboot: "{{ proxmox_instance_onboot }}"
scsihw: virtio-scsi-single
tablet: false
vmid: "{{ proxmox_instance_vmid }}"
delegate_to: "{{ proxmox_delegate_to | default(omit) }}"
register: create_instance
notify:
- Configure cloud-init
- Start instance
- Wait SSH port is open
- Wait cloud-init
- name: Import virtual disk
ansible.builtin.command:
cmd: >-
qm set {{ proxmox_instance_vmid }}
--{{ proxmox_instance_disks[0].disk }}
{{ proxmox_instance_disks[0].storage }}:0,import-from={{ proxmox_instance_disks[0].img }}
chdir: "{{ proxmox_instance_disks[0].img | ansible.builtin.dirname }}"
when: create_instance.changed # noqa: no-handler no-changed-when
become: true
delegate_to: "{{ proxmox_delegate_to | default(omit) }}"
- name: Workaround to add args to the VM
ansible.builtin.lineinfile:
path: /etc/pve/qemu-server/{{ proxmox_instance_vmid }}.conf
line: "args: {{ proxmox_instance_args }}"
state: present
when: proxmox_instance_args | length > 0
become: true
delegate_to: "{{ proxmox_delegate_to | default(omit) }}"
- name: Add configuration items
community.proxmox.proxmox_kvm:
api_host: "{{ proxmox_api_host }}"
api_user: "{{ proxmox_api_user }}"
api_token_id: "{{ proxmox_api_token_id }}"
api_token_secret: "{{ proxmox_api_token_secret }}"
boot: order=virtio0
name: "{{ inventory_hostname }}"
node: "{{ proxmox_instance_node }}"
serial:
serial0: socket
update: true
update_unsafe: true
vmid: "{{ proxmox_instance_vmid }}"
delegate_to: "{{ proxmox_delegate_to | default(omit) }}"
notify:
- Start instance
- Wait SSH port is open