Files
deploy-vm/tasks/configure.yml
T

96 lines
4.2 KiB
YAML

---
# Applique la configuration hardware + cloud-init sur la VM clonée
- name: deploy-vm | configure resources and cloud-init for '{{ vm_name }}'
community.general.proxmox_kvm:
api_host: "{{ proxmox_host }}"
api_user: "{{ proxmox_api_user }}"
api_password: "{{ proxmox_api_password }}"
node: "{{ resolved_node }}"
vmid: "{{ resolved_vm_id }}"
name: "{{ vm_name }}"
cores: "{{ vm_cores }}"
sockets: "{{ vm_sockets }}"
memory: "{{ vm_memory }}"
onboot: "{{ vm_start_on_boot }}"
ipconfig:
ipconfig0: "ip={{ vm_ip }},gw={{ vm_gateway }}"
nameservers: "{{ vm_dns }}"
ciuser: "{{ vm_ciuser | default(omit, true) }}"
cipassword: "{{ vm_cipassword | default(omit, true) }}"
sshkeys: "{{ vm_sshkeys | default(omit, true) }}"
state: present
update: true
- name: deploy-vm | resize disk '{{ vm_disk_name }}' → {{ vm_disk_size }}
community.general.proxmox_disk:
api_host: "{{ proxmox_host }}"
api_user: "{{ proxmox_api_user }}"
api_password: "{{ proxmox_api_password }}"
vmid: "{{ resolved_vm_id }}"
disk: "{{ vm_disk_name }}"
size: "{{ vm_disk_size }}"
state: resized
when: vm_disk_size | length > 0
# Création des disques supplémentaires via l'API Proxmox directement (uri).
# Raison : proxmox_kvm update:true n'envoie pas les paramètres disque à l'API
# ("no options specified"), et proxmox_disk state:present envoie 'storage:20G'
# qui est invalide pour Ceph/RBD.
# L'appel PUT /config avec 'storage:0,size=XG' est compatible avec tous les backends.
# Idempotence : on lit la config courante au préalable et on saute les slots déjà occupés.
- name: deploy-vm | get Proxmox API ticket for extra disk creation
ansible.builtin.uri:
url: "https://{{ proxmox_host }}:8006/api2/json/access/ticket"
method: POST
body_format: form-urlencoded
body:
username: "{{ proxmox_api_user }}"
password: "{{ proxmox_api_password }}"
validate_certs: "{{ proxmox_validate_certs }}"
register: _proxmox_disk_auth
no_log: true
when: vm_extra_disks | length > 0
- name: deploy-vm | read current VM config (idempotence check for extra disks)
ansible.builtin.uri:
url: "https://{{ proxmox_host }}:8006/api2/json/nodes/{{ resolved_node }}/qemu/{{ resolved_vm_id }}/config"
method: GET
headers:
Cookie: "PVEAuthCookie={{ _proxmox_disk_auth.json.data.ticket }}"
validate_certs: "{{ proxmox_validate_certs }}"
register: _vm_current_config
when: vm_extra_disks | length > 0
- name: "deploy-vm | add extra disk '{{ _disk.disk }}' ({{ _disk.size }})"
vars:
# Proxmox API format : storage:SIZE_GB[,options]
# SIZE_GB doit être un entier en GB (sans unité).
# Formats acceptés : entier brut (20 → 20 GB), chaîne avec unité G ("20G") ou T ("2T").
_size_gb: >-
{{ (_disk.size | string)[:-1] | int * 1024 if (_disk.size | string)[-1] | upper == 'T'
else (_disk.size | string)[:-1] | int if (_disk.size | string)[-1] | upper == 'G'
else _disk.size | int }}
_opts: "{{ _disk.storage | default(proxmox_storage) }}:{{ _size_gb\
}}{{ ',ssd=1' if _disk.ssd | default(false) | bool else ''\
}}{{ ',iothread=1' if _disk.iothread | default(false) | bool else ''\
}}{{ ',backup=0' if not (_disk.backup | default(true) | bool) else ''\
}}{{ ',cache=' + _disk.cache if _disk.cache | default('') | length > 0 else '' }}"
ansible.builtin.uri:
url: "https://{{ proxmox_host }}:8006/api2/json/nodes/{{ resolved_node }}/qemu/{{ resolved_vm_id }}/config"
method: PUT
headers:
Cookie: "PVEAuthCookie={{ _proxmox_disk_auth.json.data.ticket }}"
CSRFPreventionToken: "{{ _proxmox_disk_auth.json.data.CSRFPreventionToken }}"
body_format: form-urlencoded
body: "{{ {_disk.disk: _opts} }}"
validate_certs: "{{ proxmox_validate_certs }}"
status_code: [200]
loop: "{{ vm_extra_disks }}"
loop_control:
loop_var: _disk
label: "{{ _disk.disk }} ({{ _disk.size }})"
when:
- vm_extra_disks | length > 0
- _disk.disk not in (_vm_current_config.json.data | default({}))