Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

roles: shutdown_env: Align role with ansible-lint-6.0 #544

Merged
merged 3 commits into from
Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion automation/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ cd "$COLLECTION_DIR"

ansible-test sanity
antsibull-changelog lint -v
ansible-lint roles/* --exclude roles/hosted_engine_setup --exclude roles/shutdown_env --exclude roles/disaster_recovery --exclude roles/remove_stale_lun -x experimental
ansible-lint roles/* --exclude roles/hosted_engine_setup --exclude roles/disaster_recovery --exclude roles/remove_stale_lun -x experimental

cd "$ROOT_PATH"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
bugfixes:
- shutdown_env - Align role with ansible-lint-6.0 (https://github.com/oVirt/ovirt-ansible-collection/pull/544).
62 changes: 36 additions & 26 deletions roles/shutdown_env/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
---
- block:
- name: shutdown_env role
block:

- name: Populate service facts
service_facts:
ansible.builtin.service_facts:

- name: Enforce ovirt-engine machine
fail:
ansible.builtin.fail:
msg: >
This role has be designed to be run only against the machine
where ovirt-engine is running.
when: '"ovirt-engine.service" not in ansible_facts.services'

- name: Enforce ovirt-engine status
fail:
ansible.builtin.fail:
msg: >
ovirt-engine is required to be enabled and running in order
to correctly run this role.
Expand All @@ -38,41 +39,41 @@
register: hosts_result

- name: Set a variable
set_fact:
ansible.builtin.set_fact:
startup: false

- name: Set a variable
set_fact:
ansible.builtin.set_fact:
startup: true
tags: ['never', 'startup']

- name: Define a query for HE hosts
set_fact:
ansible.builtin.set_fact:
he_hosts: >-
{{ hosts_result.ovirt_hosts | selectattr('hosted_engine', 'defined') | selectattr('hosted_engine.configured') | list }}

- name: Define a query for non HE hosts
set_fact:
ansible.builtin.set_fact:
non_he_hosts: >-
{{ hosts_result.ovirt_hosts | difference(he_hosts) }}

- name: Define a query for non HE hosts with power management
set_fact:
ansible.builtin.set_fact:
non_he_hosts_ipmi: >-
{{ non_he_hosts | selectattr('power_management', 'defined') |
selectattr('power_management.enabled') | list }}

- name: Define a query for non HE hosts without power management
set_fact:
ansible.builtin.set_fact:
non_he_hosts_noipmi: "{{ non_he_hosts | difference(non_he_hosts_ipmi) }}"

- name: Define a query for hosts with power management
set_fact:
ansible.builtin.set_fact:
hosts_ipmi: >-
{{ hosts_result.ovirt_hosts | selectattr('power_management', 'defined') | selectattr('power_management.enabled') | list }}

- name: Define commands
set_fact:
ansible.builtin.set_fact:
he_shutdown_cmd: >-
while hosted-engine --vm-status | grep "\"vm\": \"up\"" >/dev/null;
do sleep 5;
Expand All @@ -92,7 +93,8 @@
all_content: true
register: vm_result

- block:
- name: Shoutdown VMs and hosts
block:
- name: Shutdown all VMs, except HostedEngine
ovirt_vm:
state: stopped
Expand All @@ -102,7 +104,7 @@
when: "item.origin != 'managed_hosted_engine'"
with_items:
- "{{ vm_result.ovirt_vms }}"
ignore_errors: true
failed_when: false

- name: Refresh VM list
ovirt_vm_info:
Expand Down Expand Up @@ -130,7 +132,7 @@
- "{{ non_he_hosts_ipmi }}"

- name: Shutdown remaining non HE hosts
command: >-
ansible.builtin.command: >-
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no
-i /etc/pki/ovirt-engine/keys/engine_id_rsa
-p {{ item.ssh.port }}
Expand All @@ -140,53 +142,59 @@
poll: 0
with_items:
- "{{ non_he_hosts_noipmi }}"
ignore_errors: true
failed_when: false
changed_when: false

- name: Set global maintenance mode
command: >-
ansible.builtin.command: >-
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no
-i /etc/pki/ovirt-engine/keys/engine_id_rsa
-p {{ item.ssh.port }} -t root@{{ item.address }}
'{{ gmaintenance_mode_cmd }}'
with_items:
- "{{ he_hosts }}"
ignore_errors: true
changed_when: false
register: globalmm

- set_fact:
- name: Set globalmm_set variable
ansible.builtin.set_fact:
globalmm_set: "{{ globalmm.results | rejectattr('failed') | list | length }}"
when: globalmm is defined and globalmm.results is defined

- name: Enforce global maintenance mode
fail:
ansible.builtin.fail:
msg: >
Failed setting global maintenance mode.
when: he_hosts|length > 0 and globalmm_set|int == 0

- name: Warn about HE global maintenace mode
debug:
ansible.builtin.debug:
msg: >
HE global maintenance mode has been set; you have to exit it to get the engine VM started when needed
when: globalmm_set|int > 0

- name: Shutdown of HE hosts
command: >-
ansible.builtin.command: >-
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no
-i /etc/pki/ovirt-engine/keys/engine_id_rsa -p {{ item.ssh.port }}
-t root@{{ item.address }} '{{ he_shutdown_cmd }}'
async: 1000
poll: 0
with_items:
- "{{ he_hosts }}"
changed_when: false

- name: Shutdown engine host/VM
command: shutdown -h now
ansible.builtin.command: shutdown -h now
async: 1000
poll: 0
changed_when: false

when: not startup

- block:
- name: Startup mode
block:
- name: Power-on IPMI configured hosts
ovirt_host:
state: started
Expand All @@ -196,21 +204,23 @@
- "{{ hosts_ipmi }}"

- name: Unset global maintenance mode
command: >-
ansible.builtin.command: >-
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no
-i /etc/pki/ovirt-engine/keys/engine_id_rsa -p {{ item.ssh.port }}
-t root@{{ item.address }} '{{ ugmaintenance_mode_cmd }}'
with_items:
- "{{ he_hosts }}"
ignore_errors: true
changed_when: false
register: uglobalmm

- set_fact:
- name: Set globalmm_set variable
ansible.builtin.set_fact:
globalmm_set: "{{ uglobalmm.results | rejectattr('failed') | list | length }}"
when: uglobalmm is defined and uglobalmm.results is defined

- name: Enforce no global maintenance mode
fail:
ansible.builtin.fail:
msg: >
Failed unsetting global maintenance mode.
when: he_hosts|length > 0 and globalmm_set|int == 0
Expand Down