Skip to content

Commit

Permalink
fixed linting errors and specified fqdn module names
Browse files Browse the repository at this point in the history
  • Loading branch information
thedatabaseme committed Apr 27, 2023
1 parent 26ba29a commit d71b2ea
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 43 deletions.
75 changes: 40 additions & 35 deletions patch_vm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@
block:

- name: Extract Node and VM ID for VM {{ item.vm_name }}
set_fact:
ansible.builtin.set_fact:
vm_id: "{{ proxmox_cluster_information.json.data | selectattr('name', 'equalto', item.vm_name) | map(attribute='vmid') | first }}"
vm_node: "{{ proxmox_cluster_information.json.data | selectattr('name', 'equalto', item.vm_name) | map(attribute='node') | first }}"
vm_type: "{{ proxmox_cluster_information.json.data | selectattr('name', 'equalto', item.vm_name) | map(attribute='type') | first }}"

- name: Get VM Status
uri:
ansible.builtin.uri:
method: GET
validate_certs: no
validate_certs: false
url: "https://{{ proxmox_api_host }}:{{ proxmox_api_port }}/api2/json/nodes/{{ vm_node }}/{{ vm_type }}/{{ vm_id }}/status/current"
headers: "{{ proxmox_api_cookie }}"
register: vm_status

- name: Start qemu VM {{ item.vm_name }}
uri:
ansible.builtin.uri:
method: POST
validate_certs: no
validate_certs: false
url: "https://{{ proxmox_api_host }}:{{ proxmox_api_port }}/api2/json/nodes/{{ vm_node }}/{{ vm_type }}/{{ vm_id }}/status/start"
headers: "{{ proxmox_api_cookie }}"
body_format: form-urlencoded
Expand All @@ -29,28 +29,28 @@
when: vm_status.json.data.status == 'stopped' and vm_type == 'qemu'

- name: Start lxc VM {{ item.vm_name }}
uri:
ansible.builtin.uri:
method: POST
validate_certs: no
validate_certs: false
url: "https://{{ proxmox_api_host }}:{{ proxmox_api_port }}/api2/json/nodes/{{ vm_node }}/{{ vm_type }}/{{ vm_id }}/status/start"
headers: "{{ proxmox_api_cookie }}"
body_format: form-urlencoded
when: vm_status.json.data.status == 'stopped' and vm_type == 'lxc'

- name: Waiting for the VM to boot up
pause:
ansible.builtin.pause:
seconds: "{{ boot_time }}"
when: vm_status.json.data.status == 'stopped'

- name: Gather VM Facts
gather_facts:
ansible.builtin.gather_facts:
register: vm_facts
delegate_to: "{{ item.vm_name }}"

- name: Take a VM Snapshot of qemu VM {{ item.vm_name }}
uri:
ansible.builtin.uri:
method: POST
validate_certs: no
validate_certs: false
url: "https://{{ proxmox_api_host }}:{{ proxmox_api_port }}/api2/json/nodes/{{ vm_node }}/{{ vm_type }}/{{ vm_id }}/snapshot"
headers: "{{ proxmox_api_cookie }}"
body_format: form-urlencoded
Expand All @@ -59,11 +59,11 @@
description: "Snapshot taken by Update Automation"
vmstate: 1
when: (item.snapshot|default(false)) and vm_type == 'qemu'

- name: Take a VM Snapshot of lxc VM {{ item.vm_name }}
uri:
ansible.builtin.uri:
method: POST
validate_certs: no
validate_certs: false
url: "https://{{ proxmox_api_host }}:{{ proxmox_api_port }}/api2/json/nodes/{{ vm_node }}/{{ vm_type }}/{{ vm_id }}/snapshot"
headers: "{{ proxmox_api_cookie }}"
body_format: form-urlencoded
Expand All @@ -73,32 +73,32 @@
when: (item.snapshot|default(false)) and vm_type == 'lxc'

- name: Waiting for the VM to finish Snapshot
pause:
ansible.builtin.pause:
seconds: "{{ boot_time }}"
when: (item.snapshot|default(false))

- name: Update VM {{ item.vm_name }} with apt
apt:
force_apt_get: yes
ansible.builtin.apt:
force_apt_get: true
name: "*"
state: latest
update_cache: yes
update_cache: true
become: true
delegate_to: "{{ item.vm_name }}"
when: vm_facts.ansible_facts.ansible_os_family == 'Debian'

- name: Update VM {{ item.vm_name }} with yum
yum:
ansible.builtin.yum:
name: "*"
state: latest
update_cache: yes
update_cache: true
lock_timeout: 120
become: true
delegate_to: "{{ item.vm_name }}"
when: vm_facts.ansible_facts.ansible_os_family == 'RedHat'

- name: Install Prerequisite Packages for OpenSuse / SLES
package:
ansible.builtin.package:
name:
- python-xml
- zypper
Expand All @@ -109,19 +109,19 @@
when: vm_facts.ansible_facts.ansible_os_family == 'Suse'

- name: Update VM {{ item.vm_name }} with zypper
zypper:
community.general.zypper:
name: "*"
state: latest
update_cache: yes
update_cache: true
type: patch
become: true
delegate_to: "{{ item.vm_name }}"
when: vm_facts.ansible_facts.ansible_os_family == 'Suse'

- name: Shutdown VM {{ item.vm_name }} when it was stopped before patching
uri:
ansible.builtin.uri:
method: POST
validate_certs: no
validate_certs: false
url: "https://{{ proxmox_api_host }}:{{ proxmox_api_port }}/api2/json/nodes/{{ vm_node }}/{{ vm_type }}/{{ vm_id }}/status/shutdown"
headers: "{{ proxmox_api_cookie }}"
body_format: form-urlencoded
Expand All @@ -130,19 +130,24 @@
when: vm_status.json.data.status == 'stopped'

- name: Check if a Reboot is required for Debian / Ubuntu
stat:
ansible.builtin.stat:
path: /var/run/reboot-required
become: true
register: reboot_required
delegate_to: "{{ item.vm_name }}"
when: (not vm_status.json.data.status == 'stopped') and (item.reboot_if_required|default(false)) and (vm_facts.ansible_facts.ansible_os_family == 'Debian')
when:
- (not vm_status.json.data.status == 'stopped')
- (item.reboot_if_required|default(false))
- (vm_facts.ansible_facts.ansible_os_family == 'Debian')

- name: Reboot VM after Update
reboot:
ansible.builtin.reboot:
become: true
delegate_to: "{{ item.vm_name }}"
when: (reboot_required.stat.exists|default(false))

rescue:
- debug:
msg: "There was an Error during Patch Installation on {{ item.vm_name }}. Maybe you misspelled the VM Name. Be aware, that Proxmox VM Names are case sensitive!"
- ansible.builtin.debug:
msg:
- "There was an Error during Patch Installation on {{ item.vm_name }}."
- "Maybe you misspelled the VM Name. Be aware, that Proxmox VM Names are case sensitive!"
16 changes: 8 additions & 8 deletions update_proxmox_vm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
tasks:

- name: Logon to Proxmox Server
uri:
ansible.builtin.uri:
method: POST
validate_certs: no
return_content: yes
validate_certs: false
return_content: true
url: "https://{{ proxmox_api_host }}:8006/api2/json/access/ticket"
body_format: json
body:
Expand All @@ -31,20 +31,20 @@
become: false

- name: parse cookie data
set_fact:
ansible.builtin.set_fact:
proxmox_api_cookie:
Cookie: "PVEAuthCookie={{ proxmox_session.json.data.ticket }}"
CSRFPreventionToken: "{{ proxmox_session.json.data.CSRFPreventionToken }}"
no_log: true

- name: get information about all vms in the cluster
uri:
ansible.builtin.uri:
method: GET
validate_certs: no
validate_certs: false
url: "https://{{ proxmox_api_host }}:8006/api2/json/cluster/resources?type=vm"
headers: "{{ proxmox_api_cookie }}"
register: proxmox_cluster_information

- name: Patch VM
include_tasks: patch_vm.yml
loop: "{{ vm_list }}"
ansible.builtin.include_tasks: patch_vm.yml
loop: "{{ vm_list }}"

0 comments on commit d71b2ea

Please sign in to comment.