refactor(virt): adopt module_defaults for hypervisor credentials
This commit is contained in:
@@ -1,95 +1,94 @@
|
||||
---
|
||||
- name: Deploy VM on Proxmox
|
||||
delegate_to: localhost
|
||||
vars:
|
||||
virtualization_proxmox_scsi: >-
|
||||
{%- set out = {} -%}
|
||||
{%- for disk in system_cfg.disks -%}
|
||||
{%- set _ = out.update({ 'scsi' ~ loop.index0: hypervisor_cfg.storage ~ ':' ~ (disk.size | int) }) -%}
|
||||
{%- endfor -%}
|
||||
{{ out }}
|
||||
virtualization_proxmox_net: >-
|
||||
{%- set out = {} -%}
|
||||
{%- for iface in system_cfg.network.interfaces -%}
|
||||
{%- set val = 'virtio,bridge=' ~ iface.bridge -%}
|
||||
{%- if iface.vlan | default('') | string | length > 0 -%}
|
||||
{%- set val = val ~ ',tag=' ~ iface.vlan -%}
|
||||
{%- endif -%}
|
||||
{%- set _ = out.update({ 'net' ~ loop.index0: val }) -%}
|
||||
{%- endfor -%}
|
||||
{{ out }}
|
||||
virtualization_proxmox_ipconfig: >-
|
||||
{%- set out = {} -%}
|
||||
{%- for iface in system_cfg.network.interfaces -%}
|
||||
{%- if iface.ip | default('') | string | length > 0 -%}
|
||||
{%- set val = 'ip=' ~ iface.ip ~ '/' ~ iface.prefix
|
||||
~ ((',gw=' ~ iface.gateway) if (iface.gateway | default('') | length > 0) else '') -%}
|
||||
{%- else -%}
|
||||
{%- set val = 'ip=dhcp' -%}
|
||||
{%- endif -%}
|
||||
{%- set _ = out.update({ 'ipconfig' ~ loop.index0: val }) -%}
|
||||
{%- endfor -%}
|
||||
{{ out }}
|
||||
community.proxmox.proxmox_kvm:
|
||||
api_host: "{{ hypervisor_cfg.url }}"
|
||||
api_user: "{{ hypervisor_cfg.username }}"
|
||||
api_password: "{{ hypervisor_cfg.password }}"
|
||||
ciuser: "{{ system_cfg.users[0].name }}"
|
||||
cipassword: "{{ system_cfg.users[0].password }}"
|
||||
ciupgrade: false
|
||||
node: "{{ hypervisor_cfg.host }}"
|
||||
vmid: "{{ system_cfg.id }}"
|
||||
name: "{{ hostname }}"
|
||||
cpu: host
|
||||
cores: "{{ system_cfg.cpus }}"
|
||||
memory: "{{ system_cfg.memory }}"
|
||||
balloon: "{{ system_cfg.balloon if system_cfg.balloon is defined and system_cfg.balloon | int > 0 else omit }}"
|
||||
numa_enabled: true
|
||||
hotplug: network,disk
|
||||
update: "{{ virtualization_tpm2_enabled | bool }}"
|
||||
update_unsafe: "{{ virtualization_tpm2_enabled | bool }}"
|
||||
bios: ovmf
|
||||
machine: "{{ 'q35' if virtualization_tpm2_enabled | bool else omit }}"
|
||||
boot: ac
|
||||
scsihw: virtio-scsi-single
|
||||
scsi: "{{ virtualization_proxmox_scsi }}"
|
||||
efidisk0:
|
||||
efitype: 4m
|
||||
format: raw
|
||||
pre_enrolled_keys: false
|
||||
storage: "{{ hypervisor_cfg.storage }}"
|
||||
tpmstate0: >-
|
||||
{{
|
||||
{'storage': hypervisor_cfg.storage, 'version': '2.0'}
|
||||
if virtualization_tpm2_enabled | bool
|
||||
else omit
|
||||
}}
|
||||
ide:
|
||||
ide0: "{{ boot_iso }},media=cdrom"
|
||||
ide1: "{{ rhel_iso + ',media=cdrom' if rhel_iso is defined and rhel_iso | length > 0 else omit }}"
|
||||
ide2: "{{ hypervisor_cfg.storage }}:cloudinit"
|
||||
net: "{{ virtualization_proxmox_net }}"
|
||||
ipconfig: "{{ virtualization_proxmox_ipconfig }}"
|
||||
nameservers: "{{ system_cfg.network.dns.servers if system_cfg.network.dns.servers | length else omit }}"
|
||||
searchdomains: "{{ system_cfg.network.dns.search if system_cfg.network.dns.search | length else omit }}"
|
||||
onboot: true
|
||||
state: present
|
||||
no_log: true
|
||||
module_defaults:
|
||||
community.proxmox.proxmox_kvm:
|
||||
api_host: "{{ hypervisor_cfg.url }}"
|
||||
api_user: "{{ hypervisor_cfg.username }}"
|
||||
api_password: "{{ hypervisor_cfg.password }}"
|
||||
node: "{{ hypervisor_cfg.host }}"
|
||||
block:
|
||||
- name: Create VM on Proxmox
|
||||
vars:
|
||||
virtualization_proxmox_scsi: >-
|
||||
{%- set out = {} -%}
|
||||
{%- for disk in system_cfg.disks -%}
|
||||
{%- set _ = out.update({ 'scsi' ~ loop.index0: hypervisor_cfg.storage ~ ':' ~ (disk.size | int) }) -%}
|
||||
{%- endfor -%}
|
||||
{{ out }}
|
||||
virtualization_proxmox_net: >-
|
||||
{%- set out = {} -%}
|
||||
{%- for iface in system_cfg.network.interfaces -%}
|
||||
{%- set val = 'virtio,bridge=' ~ iface.bridge -%}
|
||||
{%- if iface.vlan | default('') | string | length > 0 -%}
|
||||
{%- set val = val ~ ',tag=' ~ iface.vlan -%}
|
||||
{%- endif -%}
|
||||
{%- set _ = out.update({ 'net' ~ loop.index0: val }) -%}
|
||||
{%- endfor -%}
|
||||
{{ out }}
|
||||
virtualization_proxmox_ipconfig: >-
|
||||
{%- set out = {} -%}
|
||||
{%- for iface in system_cfg.network.interfaces -%}
|
||||
{%- if iface.ip | default('') | string | length > 0 -%}
|
||||
{%- set val = 'ip=' ~ iface.ip ~ '/' ~ iface.prefix
|
||||
~ ((',gw=' ~ iface.gateway) if (iface.gateway | default('') | length > 0) else '') -%}
|
||||
{%- else -%}
|
||||
{%- set val = 'ip=dhcp' -%}
|
||||
{%- endif -%}
|
||||
{%- set _ = out.update({ 'ipconfig' ~ loop.index0: val }) -%}
|
||||
{%- endfor -%}
|
||||
{{ out }}
|
||||
community.proxmox.proxmox_kvm:
|
||||
ciuser: "{{ system_cfg.users[0].name }}"
|
||||
cipassword: "{{ system_cfg.users[0].password }}"
|
||||
ciupgrade: false
|
||||
vmid: "{{ system_cfg.id }}"
|
||||
name: "{{ hostname }}"
|
||||
cpu: host
|
||||
cores: "{{ system_cfg.cpus }}"
|
||||
memory: "{{ system_cfg.memory }}"
|
||||
balloon: "{{ system_cfg.balloon if system_cfg.balloon is defined and system_cfg.balloon | int > 0 else omit }}"
|
||||
numa_enabled: true
|
||||
hotplug: network,disk
|
||||
update: "{{ virtualization_tpm2_enabled | bool }}"
|
||||
update_unsafe: "{{ virtualization_tpm2_enabled | bool }}"
|
||||
bios: ovmf
|
||||
machine: "{{ 'q35' if virtualization_tpm2_enabled | bool else omit }}"
|
||||
boot: ac
|
||||
scsihw: virtio-scsi-single
|
||||
scsi: "{{ virtualization_proxmox_scsi }}"
|
||||
efidisk0:
|
||||
efitype: 4m
|
||||
format: raw
|
||||
pre_enrolled_keys: false
|
||||
storage: "{{ hypervisor_cfg.storage }}"
|
||||
tpmstate0: >-
|
||||
{{
|
||||
{'storage': hypervisor_cfg.storage, 'version': '2.0'}
|
||||
if virtualization_tpm2_enabled | bool
|
||||
else omit
|
||||
}}
|
||||
ide:
|
||||
ide0: "{{ boot_iso }},media=cdrom"
|
||||
ide1: "{{ rhel_iso + ',media=cdrom' if rhel_iso is defined and rhel_iso | length > 0 else omit }}"
|
||||
ide2: "{{ hypervisor_cfg.storage }}:cloudinit"
|
||||
net: "{{ virtualization_proxmox_net }}"
|
||||
ipconfig: "{{ virtualization_proxmox_ipconfig }}"
|
||||
nameservers: "{{ system_cfg.network.dns.servers if system_cfg.network.dns.servers | length else omit }}"
|
||||
searchdomains: "{{ system_cfg.network.dns.search if system_cfg.network.dns.search | length else omit }}"
|
||||
onboot: true
|
||||
state: present
|
||||
no_log: true
|
||||
|
||||
- name: Start VM on Proxmox
|
||||
delegate_to: localhost
|
||||
community.proxmox.proxmox_kvm:
|
||||
api_host: "{{ hypervisor_cfg.url }}"
|
||||
api_user: "{{ hypervisor_cfg.username }}"
|
||||
api_password: "{{ hypervisor_cfg.password }}"
|
||||
node: "{{ hypervisor_cfg.host }}"
|
||||
name: "{{ hostname }}"
|
||||
vmid: "{{ system_cfg.id }}"
|
||||
state: started
|
||||
no_log: true
|
||||
register: virtualization_proxmox_start_result
|
||||
- name: Start VM on Proxmox
|
||||
community.proxmox.proxmox_kvm:
|
||||
name: "{{ hostname }}"
|
||||
vmid: "{{ system_cfg.id }}"
|
||||
state: started
|
||||
no_log: true
|
||||
register: virtualization_proxmox_start_result
|
||||
|
||||
- name: Set VM created fact
|
||||
ansible.builtin.set_fact:
|
||||
virtualization_vm_created_in_run: true
|
||||
when: virtualization_proxmox_start_result is defined and virtualization_proxmox_start_result.changed | bool
|
||||
- name: Set VM created fact
|
||||
ansible.builtin.set_fact:
|
||||
virtualization_vm_created_in_run: true
|
||||
when: virtualization_proxmox_start_result is defined and virtualization_proxmox_start_result.changed | bool
|
||||
|
||||
Reference in New Issue
Block a user