feat/update-cloud-init-template #5
@@ -1,8 +1,7 @@
|
|||||||
---
|
---
|
||||||
- name: Create or update a cloud-init Ubuntu template
|
- name: Create or update a cloud-init Ubuntu template
|
||||||
hosts: zenith.mgmt.vezpi.com
|
hosts: nodes
|
||||||
become: true
|
become: true
|
||||||
gather_facts: false
|
|
||||||
vars:
|
vars:
|
||||||
template_vmid: 900
|
template_vmid: 900
|
||||||
template_os: ubuntu
|
template_os: ubuntu
|
||||||
@@ -21,6 +20,41 @@
|
|||||||
template_cores: 1
|
template_cores: 1
|
||||||
|
|
||||||
tasks:
|
tasks:
|
||||||
|
- name: Check whether the template VMID already exists
|
||||||
|
ansible.builtin.command: qm status {{ template_vmid }}
|
||||||
|
register: template_status
|
||||||
|
changed_when: false
|
||||||
|
failed_when: false
|
||||||
|
|
||||||
|
- name: Check if the cloud image file exists
|
||||||
|
ansible.builtin.stat:
|
||||||
|
path: "{{ image_dest }}"
|
||||||
|
register: image_exists
|
||||||
|
changed_when: false
|
||||||
|
failed_when: false
|
||||||
|
|
||||||
|
- name: Set candidate information
|
||||||
|
ansible.builtin.set_fact:
|
||||||
|
has_image_file: "{{ image_exists.stat.exists }}"
|
||||||
|
root_free_space: "{{ (ansible_mounts | selectattr('mount', 'equalto', '/') | first).size_available }}"
|
||||||
|
|
||||||
|
- name: Select target host
|
||||||
|
ansible.builtin.set_fact:
|
||||||
|
target_host: >-
|
||||||
|
{{ (ansible_play_hosts
|
||||||
|
| map('extract', hostvars)
|
||||||
|
| selectattr('has_target_file', 'equalto', true)
|
||||||
|
| sort(attribute='root_free_space', reverse=true)
|
||||||
|
| map(attribute='inventory_hostname')
|
||||||
|
| first)
|
||||||
|
| default(ansible_play_hosts
|
||||||
|
| map('extract', hostvars)
|
||||||
|
| sort(attribute='root_free_space', reverse=true)
|
||||||
|
| map(attribute='inventory_hostname')
|
||||||
|
| first,
|
||||||
|
true) }}
|
||||||
|
run_once: true
|
||||||
|
|
||||||
- name: Download the current Ubuntu cloud image
|
- name: Download the current Ubuntu cloud image
|
||||||
ansible.builtin.get_url:
|
ansible.builtin.get_url:
|
||||||
url: "{{ image_url }}"
|
url: "{{ image_url }}"
|
||||||
@@ -28,20 +62,19 @@
|
|||||||
force: false
|
force: false
|
||||||
mode: "0644"
|
mode: "0644"
|
||||||
register: image_download
|
register: image_download
|
||||||
|
when: inventory_hostname == target_host
|
||||||
|
|
||||||
- name: Check whether the template VMID already exists
|
|
||||||
ansible.builtin.command: qm status {{ template_vmid }}
|
|
||||||
register: template_status
|
|
||||||
changed_when: false
|
|
||||||
failed_when: false
|
|
||||||
|
|
||||||
- name: Rebuild the template
|
|
||||||
when: image_download.changed or template_status.rc != 0
|
|
||||||
block:
|
|
||||||
- name: Destroy existing template if present
|
- name: Destroy existing template if present
|
||||||
ansible.builtin.command: qm destroy {{ template_vmid }} --purge
|
ansible.builtin.command: qm destroy {{ template_vmid }} --purge
|
||||||
when: template_status.rc == 0
|
when: template_status.rc == 0
|
||||||
|
|
||||||
|
- name: Rebuild the template
|
||||||
|
when:
|
||||||
|
- inventory_hostname == target_host
|
||||||
|
- image_download.changed or
|
||||||
|
template_status.rc != 0
|
||||||
|
block:
|
||||||
|
|
||||||
- name: Create the base VM shell
|
- name: Create the base VM shell
|
||||||
ansible.builtin.command: >
|
ansible.builtin.command: >
|
||||||
qm create {{ template_vmid }}
|
qm create {{ template_vmid }}
|
||||||
|
|||||||
Reference in New Issue
Block a user