Created
September 29, 2022 18:12
-
-
Save johnsimcall/090549aa4b20782615a1cc89c30f47a7 to your computer and use it in GitHub Desktop.
Ansible playbook to extract virtual machine and disk details from oVirt/RHVM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# file: query-vms.yml | |
# prerequisites: | |
# sudo dnf --enablerepo=rhv-4.4-manager-for-rhel-8-x86_64-rpms install "python*ovirt-engine-sdk4" | |
# or | |
# sudo dnf install https://resources.ovirt.org/pub/ovirt-4.4/rpm/el8/x86_64/python3-ovirt-engine-sdk4-4.4.15-1.el8.x86_64.rpm | |
# description: | |
# Simple playbook to extract basic details of VMs | |
# example invocation: | |
# ansible-playbook query-vms.yml | |
--- | |
- hosts: localhost | |
gather_facts: no | |
vars: | |
output_dir: . | |
rhvm_fqdn: rhvm.example.com | |
rhvm_username: admin@internal | |
rhvm_password: myPassw0rd | |
tasks: | |
- name: Record the date and time | |
command: date +%Y-%m-%d-%H%M%S | |
register: date_cmd | |
- name: Create RHV auth token | |
ovirt_auth: | |
url: https://{{ rhvm_fqdn }}/ovirt-engine/api | |
insecure: yes | |
username: "{{ rhvm_username }}" | |
password: "{{ rhvm_password }}" | |
- name: Query VMs | |
ovirt_vm_info: | |
auth: "{{ ovirt_auth }}" | |
fetch_nested: true | |
register: vm_result | |
- name: Query VM disks | |
ovirt_disk_info: | |
auth: "{{ ovirt_auth }}" | |
fetch_nested: true | |
register: disk_result | |
- name: Remove RHV auth token | |
ovirt_auth: | |
ovirt_auth: "{{ ovirt_auth }}" | |
state: absent | |
- name: Save VM results | |
copy: | |
dest: "{{ output_dir }}/query-results-vms-{{ date_cmd.stdout }}.json" | |
content: "{{ vm_result.ovirt_vms }}" | |
register: vm_result_filename | |
- name: Save disk results | |
copy: | |
dest: "{{ output_dir }}/query-results-disks-{{ date_cmd.stdout }}.json" | |
content: "{{ disk_result.ovirt_disks }}" | |
register: disk_result_filename | |
- name: Create a tar.gz file | |
archive: | |
path: | |
- "{{ vm_result_filename.dest }}" | |
- "{{ disk_result_filename.dest }}" | |
dest: "{{ output_dir }}/query-results-{{ date_cmd.stdout }}.tar.gz" | |
remove: yes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment