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

ovirt_remove_stale_lun: Use add_host instead of delegate_to #390

Merged
merged 1 commit into from
Nov 29, 2021
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
minor_changes:
- ovirt_remove_stale_lun - Use add_host instead of delegate_to (https://github.com/oVirt/ovirt-ansible-collection/pull/390).
23 changes: 16 additions & 7 deletions roles/remove_stale_lun/examples/remove_stale_lun.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
- name: oVirt remove stale LUN
- name: Discover hosts in DC and add to Ansible runtime inventory
hosts: localhost
connection: local
gather_facts: false
Expand All @@ -9,17 +9,26 @@
- passwords.yml

vars:
ansible_ssh_common_args: "-o StrictHostKeyChecking=no"
ansible_user: root
ansible_ssh_private_key_file: /etc/pki/ovirt-engine/keys/engine_id_rsa

engine_fqdn: ovirt.example.com
engine_user: admin@internal

data_center: default
roles:
- role: remove_stale_lun
fetch_hosts: true
collections:
- @NAMESPACE@.@NAME@

- name: Remove LUNs from the discovered hosts in DC
hosts: dc_hosts

vars:
ansible_ssh_common_args: "-o StrictHostKeyChecking=no"
ansible_user: root
ansible_ssh_private_key_file: /etc/pki/ovirt-engine/keys/engine_id_rsa
lun_wwid: 36001405a77a1ee25cbf4439b7ddd2062 36001405ddefe8392bb8443e89bde4b40

roles:
- remove_stale_lun
- role: remove_stale_lun
fetch_hosts: false
collections:
- @NAMESPACE@.@NAME@
45 changes: 45 additions & 0 deletions roles/remove_stale_lun/tasks/fetch_hosts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
## https://github.com/ansible/ansible/issues/22397
## Ansible 2.3 generates a WARNING when using {{ }} in defaults variables of role
## this workarounds it until Ansible resolves the issue:
- name: Initialize variables
set_fact:
data_center: "{{ data_center | mandatory }}"

- block:
- name: Login to oVirt
ovirt_auth:
url: "{{ engine_url | default(lookup('env','OVIRT_URL')) | default(omit) }}"
username: "{{ engine_user | default(lookup('env','OVIRT_USERNAME')) | default(omit) }}"
hostname: "{{ engine_fqdn | default(lookup('env','OVIRT_HOSTNAME')) | default(omit) }}"
password: "{{ engine_password | default(lookup('env','OVIRT_PASSWORD')) | default(omit) }}"
ca_file: "{{ engine_cafile | default(lookup('env','OVIRT_CAFILE')) | default(omit) }}"
token: "{{ engine_token | default(lookup('env','OVIRT_TOKEN')) | default(omit) }}"
insecure: "{{ engine_insecure | default(true) }}"
when: ovirt_auth is undefined or not ovirt_auth
register: login_result
tags:
- always

- name: Get datacenter hosts
ovirt_host_info:
auth: "{{ ovirt_auth }}"
pattern: "datacenter={{ data_center }}"
register: host_info

- name: Add hosts
ansible.builtin.add_host:
hostname: "{{ item.address }}"
groups: dc_hosts
with_items:
- "{{ host_info.ovirt_hosts }}"

always:
- name: Logout from oVirt
ovirt_auth:
state: absent
ovirt_auth: "{{ ovirt_auth }}"
when:
- login_result.skipped is defined and not login_result.skipped
tags:
- always
44 changes: 4 additions & 40 deletions roles/remove_stale_lun/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,41 +1,5 @@
---
## https://github.com/ansible/ansible/issues/22397
## Ansible 2.3 generates a WARNING when using {{ }} in defaults variables of role
## this workarounds it until Ansible resolves the issue:
- name: Initialize variables
set_fact:
data_center: "{{ data_center | mandatory }}"
lun_wwid: "{{ lun_wwid | mandatory }}"
- include_tasks: fetch_hosts.yml
when: fetch_hosts

- block:
- name: Login to oVirt
ovirt_auth:
url: "{{ engine_url | default(lookup('env','OVIRT_URL')) | default(omit) }}"
username: "{{ engine_user | default(lookup('env','OVIRT_USERNAME')) | default(omit) }}"
hostname: "{{ engine_fqdn | default(lookup('env','OVIRT_HOSTNAME')) | default(omit) }}"
password: "{{ engine_password | default(lookup('env','OVIRT_PASSWORD')) | default(omit) }}"
ca_file: "{{ engine_cafile | default(lookup('env','OVIRT_CAFILE')) | default(omit) }}"
token: "{{ engine_token | default(lookup('env','OVIRT_TOKEN')) | default(omit) }}"
insecure: "{{ engine_insecure | default(true) }}"
when: ovirt_auth is undefined or not ovirt_auth
register: login_result
tags:
- always

- name: Get datacenter hosts
ovirt_host_info:
auth: "{{ ovirt_auth }}"
pattern: "datacenter={{ data_center }}"
register: host_info

- include_tasks: remove_mpath_device.yml

always:
- name: Logout from oVirt
ovirt_auth:
state: absent
ovirt_auth: "{{ ovirt_auth }}"
when:
- login_result.skipped is defined and not login_result.skipped
tags:
- always
- include_tasks: remove_mpath_device.yml
when: not fetch_hosts
26 changes: 7 additions & 19 deletions roles/remove_stale_lun/tasks/remove_mpath_device.yml
Original file line number Diff line number Diff line change
@@ -1,33 +1,21 @@
- name: Initialize variables
set_fact:
lun_wwid: "{{ lun_wwid | mandatory }}"

- name: Get underlying disks (paths) for a multipath device(s) and turn them into a list.
ansible.builtin.shell: 'for dev in {{ lun_wwid }}; do dmsetup deps -o devname $dev | cut -f 2 |cut -c 3- |tr -d "()"|tr "\r\n" " "; done'
register: disks
delegate_to: "{{ host_item.address }}"
connection: ssh
check_mode: "no"
with_items:
- "{{ host_info.ovirt_hosts }}"
loop_control:
loop_var: host_item

- name: Remove from multipath device.
ansible.builtin.shell: "for dev in {{ lun_wwid }}; do multipath -f $dev || exit 1; done"
delegate_to: "{{ host_item.address }}"
connection: ssh
check_mode: "no"
with_items:
- "{{ host_info.ovirt_hosts }}"
register: flush_results
retries: 6
check_mode: "no"
until: flush_results.rc == 0
loop_control:
loop_var: host_item

- name: Remove each path from the SCSI subsystem.
ansible.builtin.shell: "for dev in {{ result.stdout }}; do echo 1 > /sys/block/$dev/device/delete; done"
delegate_to: "{{ result.host_item.address }}"
connection: ssh
ansible.builtin.shell: "for dev in {{ item.stdout }}; do echo 1 > /sys/block/$dev/device/delete; done"
check_mode: "no"
with_items:
- "{{ disks.results }}"
loop_control:
loop_var: result
- "{{ disks }}"