From ed407dec92d8932116afa2f2d931d015aa217437 Mon Sep 17 00:00:00 2001 From: Vickie Karasic Date: Fri, 18 Oct 2024 14:19:07 -0400 Subject: [PATCH 1/6] adding a new playbook for testing veeam backups to .csvs Co-authored-by: G. Philippe Menos Co-authored-by: John Kazmierski --- playbooks/utils/veeam_backup_list_to_csvs.yml | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 playbooks/utils/veeam_backup_list_to_csvs.yml diff --git a/playbooks/utils/veeam_backup_list_to_csvs.yml b/playbooks/utils/veeam_backup_list_to_csvs.yml new file mode 100644 index 0000000000..6cbeb33bfd --- /dev/null +++ b/playbooks/utils/veeam_backup_list_to_csvs.yml @@ -0,0 +1,94 @@ +--- +- name: Get info on VEEAM backup tags from vSphere and save to CSV + hosts: localhost + + vars_files: + - ../../group_vars/vsphere/vault.yml + - ../../group_vars/vsphere/{{ runtime_env | default('staging') }}.yml + + tasks: + - name: Gather info from vSphere on VMs, tags, and storage + community.vmware.vmware_vm_info: + hostname: "{{ vcenter_hostname }}" + username: "{{ vcenter_username }}" + password: "{{ vcenter_password }}" + validate_certs: false + show_tag: true + show_allocated: true + vm_type: vm + register: vms_and_tags + + - name: Find all unique tags + ansible.builtin.set_fact: + unique_tags: "{{ vms_and_tags.virtual_machines | map(attribute='tags') | flatten | map(attribute='name') | unique }}" + + - name: Create CSV file for tags + copy: + dest: "/tmp/tags_report.csv" + content: "Tag\n" + delegate_to: localhost + + - name: Append tags to CSV + lineinfile: + path: "/tmp/tags_report.csv" + line: "{{ item }}" + loop: "{{ unique_tags }}" + delegate_to: localhost + + - name: Append report date and time to tags CSV + lineinfile: + path: "/tmp/tags_report.csv" + line: "Report Date/Time {{ lookup('pipe', 'date +%m.%d.%Y %H:%M') }}" + delegate_to: localhost + + - name: Find VMs without tags + ansible.builtin.set_fact: + untagged_vms: "{{ vms_and_tags.virtual_machines | rejectattr('tags') | map(attribute='guest_name') }}" + + - name: Create CSV file for VMs without tags + copy: + dest: "/tmp/untagged_vms_report.csv" + content: "VM Name\n" + delegate_to: localhost + + - name: Append untagged VMs to CSV + lineinfile: + path: "/tmp/untagged_vms_report.csv" + line: "{{ item }}" + loop: "{{ untagged_vms }}" + delegate_to: localhost + + - name: Append report date and time to untagged VMs CSV + lineinfile: + path: "/tmp/untagged_vms_report.csv" + line: "Report Date/Time {{ lookup('pipe', 'date +%m.%d.%Y %H:%M') }}" + delegate_to: localhost + + - name: Find VMs with tags + ansible.builtin.set_fact: + tagged_vms: "{{ vms_and_tags | community.general.json_query(not_null) }}" + vars: + not_null: "virtual_machines[?not_null(tags)]" + + - name: Create a dict of fields we want from tagged VMs + ansible.builtin.set_fact: + slim_tagged_vms: "{{ tagged_vms | community.general.json_query('[*].{VM_name: guest_name, Tag_name: tags[0].name, Disk_size: allocated.storage}') }}" + + - name: Create CSV file for VMs with tags + copy: + dest: "/tmp/tagged_vms_report.csv" + content: "VM Name,Tag Name,Disk Size\n" + delegate_to: localhost + + - name: Append tagged VMs to CSV + lineinfile: + path: "/tmp/tagged_vms_report.csv" + line: "{{ item.VM_name }},{{ item.Tag_name }},{{ item.Disk_size }}" + loop: "{{ slim_tagged_vms }}" + delegate_to: localhost + + - name: Append report date and time to tagged VMs CSV + lineinfile: + path: "/tmp/tagged_vms_report.csv" + line: "Report Date/Time {{ lookup('pipe', 'date +%m.%d.%Y %H:%M') }}" + delegate_to: localhost \ No newline at end of file From e91d0ca553f98af773b9421338ad6d83f6b0dc72 Mon Sep 17 00:00:00 2001 From: Vickie Karasic Date: Fri, 18 Oct 2024 14:54:52 -0400 Subject: [PATCH 2/6] adding .csvs output to see what happens Co-authored-by: Alicia Cozine Co-authored-by: G. Philippe Menos Co-authored-by: John Kazmierski --- playbooks/utils/veeam_backup_list.yml | 72 +++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/playbooks/utils/veeam_backup_list.yml b/playbooks/utils/veeam_backup_list.yml index 161421b8e5..22af88f3ee 100644 --- a/playbooks/utils/veeam_backup_list.yml +++ b/playbooks/utils/veeam_backup_list.yml @@ -56,3 +56,75 @@ - which have disk sizes of {{ slim_tagged_vms | selectattr('Tag_name', 'equalto', item) | map(attribute='Disk_size') }} - the total size of the {{ item }} backup is {{ slim_tagged_vms | selectattr('Tag_name', 'equalto', item) | map(attribute='Disk_size') | sum | human_readable }} loop: "{{ unique_tags }}" + +#Trying this with .csvs output + - name: Create CSV file for tags + copy: + dest: "/tmp/tags_report.csv" + content: "Tag\n" + delegate_to: localhost + + - name: Append tags to CSV + lineinfile: + path: "/tmp/tags_report.csv" + line: "{{ item }}" + loop: "{{ unique_tags }}" + delegate_to: localhost + + - name: Append report date and time to tags CSV + lineinfile: + path: "/tmp/tags_report.csv" + line: "Report Date/Time {{ lookup('pipe', 'date +%m.%d.%Y %H:%M') }}" + delegate_to: localhost + + - name: Find VMs without tags + ansible.builtin.set_fact: + untagged_vms: "{{ vms_and_tags.virtual_machines | rejectattr('tags') | map(attribute='guest_name') }}" + + - name: Create CSV file for VMs without tags + copy: + dest: "/tmp/untagged_vms_report.csv" + content: "VM Name\n" + delegate_to: localhost + + - name: Append untagged VMs to CSV + lineinfile: + path: "/tmp/untagged_vms_report.csv" + line: "{{ item }}" + loop: "{{ untagged_vms }}" + delegate_to: localhost + + - name: Append report date and time to untagged VMs CSV + lineinfile: + path: "/tmp/untagged_vms_report.csv" + line: "Report Date/Time {{ lookup('pipe', 'date +%m.%d.%Y %H:%M') }}" + delegate_to: localhost + + - name: Find VMs with tags + ansible.builtin.set_fact: + tagged_vms: "{{ vms_and_tags | community.general.json_query(not_null) }}" + vars: + not_null: "virtual_machines[?not_null(tags)]" + + - name: Create a dict of fields we want from tagged VMs + ansible.builtin.set_fact: + slim_tagged_vms: "{{ tagged_vms | community.general.json_query('[*].{VM_name: guest_name, Tag_name: tags[0].name, Disk_size: allocated.storage}') }}" + + - name: Create CSV file for VMs with tags + copy: + dest: "/tmp/tagged_vms_report.csv" + content: "VM Name,Tag Name,Disk Size\n" + delegate_to: localhost + + - name: Append tagged VMs to CSV + lineinfile: + path: "/tmp/tagged_vms_report.csv" + line: "{{ item.VM_name }},{{ item.Tag_name }},{{ item.Disk_size }}" + loop: "{{ slim_tagged_vms }}" + delegate_to: localhost + + - name: Append report date and time to tagged VMs CSV + lineinfile: + path: "/tmp/tagged_vms_report.csv" + line: "Report Date/Time {{ lookup('pipe', 'date +%m.%d.%Y %H:%M') }}" + delegate_to: localhost From 3140dfd1b30e71cf7fd0c97be3ee6d19755a558d Mon Sep 17 00:00:00 2001 From: Vickie Karasic Date: Fri, 25 Oct 2024 13:09:49 -0400 Subject: [PATCH 3/6] fixing date and time formatting and adding a debug task --- playbooks/utils/veeam_backup_list_to_csvs.yml | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/playbooks/utils/veeam_backup_list_to_csvs.yml b/playbooks/utils/veeam_backup_list_to_csvs.yml index 6cbeb33bfd..1b08c90555 100644 --- a/playbooks/utils/veeam_backup_list_to_csvs.yml +++ b/playbooks/utils/veeam_backup_list_to_csvs.yml @@ -22,6 +22,10 @@ ansible.builtin.set_fact: unique_tags: "{{ vms_and_tags.virtual_machines | map(attribute='tags') | flatten | map(attribute='name') | unique }}" + - name: Debug for find all unique tags + ansible.builtin.debug: + var: vms_and_tags + - name: Create CSV file for tags copy: dest: "/tmp/tags_report.csv" @@ -38,7 +42,7 @@ - name: Append report date and time to tags CSV lineinfile: path: "/tmp/tags_report.csv" - line: "Report Date/Time {{ lookup('pipe', 'date +%m.%d.%Y %H:%M') }}" + line: "Report Date/Time {{ lookup('pipe', \"date '+%m.%d.%Y %H:%M'\") }}" delegate_to: localhost - name: Find VMs without tags @@ -61,7 +65,7 @@ - name: Append report date and time to untagged VMs CSV lineinfile: path: "/tmp/untagged_vms_report.csv" - line: "Report Date/Time {{ lookup('pipe', 'date +%m.%d.%Y %H:%M') }}" + line: "Report Date/Time {{ lookup('pipe', \"date '+%m.%d.%Y %H:%M'\") }}" delegate_to: localhost - name: Find VMs with tags @@ -90,5 +94,5 @@ - name: Append report date and time to tagged VMs CSV lineinfile: path: "/tmp/tagged_vms_report.csv" - line: "Report Date/Time {{ lookup('pipe', 'date +%m.%d.%Y %H:%M') }}" + line: "Report Date/Time {{ lookup('pipe', \"date '+%m.%d.%Y %H:%M'\") }}" delegate_to: localhost \ No newline at end of file From 4a1f24250e68cc90fbe4b58ead37f93c176f11b5 Mon Sep 17 00:00:00 2001 From: Vickie Karasic Date: Fri, 25 Oct 2024 13:14:41 -0400 Subject: [PATCH 4/6] oops wrong playbook --- playbooks/utils/veeam_backup_list.yml | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/playbooks/utils/veeam_backup_list.yml b/playbooks/utils/veeam_backup_list.yml index 22af88f3ee..eeea8505b2 100644 --- a/playbooks/utils/veeam_backup_list.yml +++ b/playbooks/utils/veeam_backup_list.yml @@ -22,6 +22,10 @@ ansible.builtin.set_fact: unique_tags: "{{ vms_and_tags.virtual_machines | map(attribute='tags') | flatten | map(attribute='name') | unique }}" + - name: Debug for find all unique tags + ansible.builtin.debug: + var: vms_and_tags + - name: View list of tags ansible.builtin.debug: var: unique_tags @@ -74,7 +78,7 @@ - name: Append report date and time to tags CSV lineinfile: path: "/tmp/tags_report.csv" - line: "Report Date/Time {{ lookup('pipe', 'date +%m.%d.%Y %H:%M') }}" + line: "Report Date/Time {{ lookup('pipe', \"date '+%m.%d.%Y %H:%M'\") }}" delegate_to: localhost - name: Find VMs without tags @@ -97,7 +101,7 @@ - name: Append report date and time to untagged VMs CSV lineinfile: path: "/tmp/untagged_vms_report.csv" - line: "Report Date/Time {{ lookup('pipe', 'date +%m.%d.%Y %H:%M') }}" + line: "Report Date/Time {{ lookup('pipe', \"date '+%m.%d.%Y %H:%M'\") }}" delegate_to: localhost - name: Find VMs with tags @@ -126,5 +130,5 @@ - name: Append report date and time to tagged VMs CSV lineinfile: path: "/tmp/tagged_vms_report.csv" - line: "Report Date/Time {{ lookup('pipe', 'date +%m.%d.%Y %H:%M') }}" + line: "Report Date/Time {{ lookup('pipe', \"date '+%m.%d.%Y %H:%M'\") }}" delegate_to: localhost From b17ddaea3d9597fb70a52bb1f4006754f4a14d27 Mon Sep 17 00:00:00 2001 From: Vickie Karasic Date: Fri, 25 Oct 2024 13:22:02 -0400 Subject: [PATCH 5/6] changing file destination to sandbox site from localhost --- playbooks/utils/veeam_backup_list.yml | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/playbooks/utils/veeam_backup_list.yml b/playbooks/utils/veeam_backup_list.yml index eeea8505b2..df589e4a02 100644 --- a/playbooks/utils/veeam_backup_list.yml +++ b/playbooks/utils/veeam_backup_list.yml @@ -66,20 +66,20 @@ copy: dest: "/tmp/tags_report.csv" content: "Tag\n" - delegate_to: localhost + delegate_to: sandbox-acozine.lib.princeton.edu - name: Append tags to CSV lineinfile: path: "/tmp/tags_report.csv" line: "{{ item }}" loop: "{{ unique_tags }}" - delegate_to: localhost + delegate_to: sandbox-acozine.lib.princeton.edu - name: Append report date and time to tags CSV lineinfile: path: "/tmp/tags_report.csv" line: "Report Date/Time {{ lookup('pipe', \"date '+%m.%d.%Y %H:%M'\") }}" - delegate_to: localhost + delegate_to: sandbox-acozine.lib.princeton.edu - name: Find VMs without tags ansible.builtin.set_fact: @@ -89,20 +89,20 @@ copy: dest: "/tmp/untagged_vms_report.csv" content: "VM Name\n" - delegate_to: localhost + delegate_to: sandbox-acozine.lib.princeton.edu - name: Append untagged VMs to CSV lineinfile: path: "/tmp/untagged_vms_report.csv" line: "{{ item }}" loop: "{{ untagged_vms }}" - delegate_to: localhost + delegate_to: sandbox-acozine.lib.princeton.edu - name: Append report date and time to untagged VMs CSV lineinfile: path: "/tmp/untagged_vms_report.csv" line: "Report Date/Time {{ lookup('pipe', \"date '+%m.%d.%Y %H:%M'\") }}" - delegate_to: localhost + delegate_to: sandbox-acozine.lib.princeton.edu - name: Find VMs with tags ansible.builtin.set_fact: @@ -118,17 +118,17 @@ copy: dest: "/tmp/tagged_vms_report.csv" content: "VM Name,Tag Name,Disk Size\n" - delegate_to: localhost + delegate_to: sandbox-acozine.lib.princeton.edu - name: Append tagged VMs to CSV lineinfile: path: "/tmp/tagged_vms_report.csv" line: "{{ item.VM_name }},{{ item.Tag_name }},{{ item.Disk_size }}" loop: "{{ slim_tagged_vms }}" - delegate_to: localhost + delegate_to: sandbox-acozine.lib.princeton.edu - name: Append report date and time to tagged VMs CSV lineinfile: path: "/tmp/tagged_vms_report.csv" line: "Report Date/Time {{ lookup('pipe', \"date '+%m.%d.%Y %H:%M'\") }}" - delegate_to: localhost + delegate_to: sandbox-acozine.lib.princeton.edu From 18944d9c78cb4dc74adfda82bfadc67f8f3da114 Mon Sep 17 00:00:00 2001 From: Vickie Karasic Date: Fri, 25 Oct 2024 13:34:06 -0400 Subject: [PATCH 6/6] changing the file destination to lib-sftp --- playbooks/utils/veeam_backup_list.yml | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/playbooks/utils/veeam_backup_list.yml b/playbooks/utils/veeam_backup_list.yml index df589e4a02..8c3249fb1d 100644 --- a/playbooks/utils/veeam_backup_list.yml +++ b/playbooks/utils/veeam_backup_list.yml @@ -66,20 +66,20 @@ copy: dest: "/tmp/tags_report.csv" content: "Tag\n" - delegate_to: sandbox-acozine.lib.princeton.edu + delegate_to: lib-sftp-staging1.princeton.edu - name: Append tags to CSV lineinfile: path: "/tmp/tags_report.csv" line: "{{ item }}" loop: "{{ unique_tags }}" - delegate_to: sandbox-acozine.lib.princeton.edu + delegate_to: lib-sftp-staging1.princeton.edu - name: Append report date and time to tags CSV lineinfile: path: "/tmp/tags_report.csv" line: "Report Date/Time {{ lookup('pipe', \"date '+%m.%d.%Y %H:%M'\") }}" - delegate_to: sandbox-acozine.lib.princeton.edu + delegate_to: lib-sftp-staging1.princeton.edu - name: Find VMs without tags ansible.builtin.set_fact: @@ -89,20 +89,20 @@ copy: dest: "/tmp/untagged_vms_report.csv" content: "VM Name\n" - delegate_to: sandbox-acozine.lib.princeton.edu + delegate_to: lib-sftp-staging1.princeton.edu - name: Append untagged VMs to CSV lineinfile: path: "/tmp/untagged_vms_report.csv" line: "{{ item }}" loop: "{{ untagged_vms }}" - delegate_to: sandbox-acozine.lib.princeton.edu + delegate_to: lib-sftp-staging1.princeton.edu - name: Append report date and time to untagged VMs CSV lineinfile: path: "/tmp/untagged_vms_report.csv" line: "Report Date/Time {{ lookup('pipe', \"date '+%m.%d.%Y %H:%M'\") }}" - delegate_to: sandbox-acozine.lib.princeton.edu + delegate_to: lib-sftp-staging1.princeton.edu - name: Find VMs with tags ansible.builtin.set_fact: @@ -118,17 +118,17 @@ copy: dest: "/tmp/tagged_vms_report.csv" content: "VM Name,Tag Name,Disk Size\n" - delegate_to: sandbox-acozine.lib.princeton.edu + delegate_to: lib-sftp-staging1.princeton.edu - name: Append tagged VMs to CSV lineinfile: path: "/tmp/tagged_vms_report.csv" line: "{{ item.VM_name }},{{ item.Tag_name }},{{ item.Disk_size }}" loop: "{{ slim_tagged_vms }}" - delegate_to: sandbox-acozine.lib.princeton.edu + delegate_to: lib-sftp-staging1.princeton.edu - name: Append report date and time to tagged VMs CSV lineinfile: path: "/tmp/tagged_vms_report.csv" line: "Report Date/Time {{ lookup('pipe', \"date '+%m.%d.%Y %H:%M'\") }}" - delegate_to: sandbox-acozine.lib.princeton.edu + delegate_to: lib-sftp-staging1.princeton.edu