aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorB Stack <bgstack15@gmail.com>2018-01-20 10:05:15 -0500
committerB Stack <bgstack15@gmail.com>2018-01-20 10:05:15 -0500
commit45e3dab50830f496ccb073ec0ff7083137f7e7b7 (patch)
tree0cf66e48224213321fb194c6e284d93f96b2434a
downloadvcenter_matrix-45e3dab50830f496ccb073ec0ff7083137f7e7b7.tar.gz
vcenter_matrix-45e3dab50830f496ccb073ec0ff7083137f7e7b7.tar.bz2
vcenter_matrix-45e3dab50830f496ccb073ec0ff7083137f7e7b7.zip
initial commit
-rw-r--r--.gitignore2
-rw-r--r--README.md17
-rwxr-xr-xgenerate.sh61
-rw-r--r--generate.yml57
-rw-r--r--vcenter_matrix.csv.example6
-rw-r--r--vcenter_matrix.yml.example7
-rw-r--r--vcenters.txt.example2
7 files changed, 152 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..b411296
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+vcenter_matrix.yml
+vcenter_matrix.csv
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..0209363
--- /dev/null
+++ b/README.md
@@ -0,0 +1,17 @@
+# Vcenter_matrix
+*Shell script that uses and aids ansible*
+Use vcenter_matrix to generate yaml and csv lists of all the virtual guests in a set of vcenter hosts.
+
+This script prompts for the username and password to the vcenters. Then it iterates through the list of vcenters in vcenters.txt and fetches the guest name and uuid of just the Linux guests.
+
+### Usage
+
+ cd /etc/ansible/shell/vcenter_matrix
+ vi vcenters.txt # add in your vcenter hostnames
+ ./generate.sh
+
+# Reference
+## Weblinks
+1. File descript 10 https://unix.stackexchange.com/questions/107800/using-while-loop-to-ssh-to-multiple-servers
+2. Web problem with vcenter api is because of escaped characters in variables https://github.com/ansible/ansible/issues/32477
+
diff --git a/generate.sh b/generate.sh
new file mode 100755
index 0000000..1729717
--- /dev/null
+++ b/generate.sh
@@ -0,0 +1,61 @@
+#!/bin/sh
+# File: generate.sh
+# Location: /etc/ansible/shell/vcenter_matrix/
+# Author: bgstack15
+# Startdate: 2017-12-14
+# Title: Script that Generates the vcenter matrix
+# Purpose: Generate a list of all virtual guests and their uuids in the provided vcenters
+# History:
+# Usage: Populate vcenters.txt with the list of vcenter hosts to check. The script will use the same credential for all the vcenters.
+# Document:
+# Reference:
+# file descript 10 https://unix.stackexchange.com/questions/107800/using-while-loop-to-ssh-to-multiple-servers
+# web problem with vcenter api is because of escaped characters in variables https://github.com/ansible/ansible/issues/32477
+# Improve:
+# Accept parameters or environment variables for the input text file or even the credentials
+# Document:
+
+# Variables
+infile=/etc/ansible/shell/vcenter_matrix/vcenters.txt
+userfile=/etc/ansible/shell/vcenter_matrix/username.yml
+tmpdir="$( mktemp -d )"
+pwfile="$( TMPDIR="${tmpdir}" mktemp )"
+outputfile_yml=/etc/ansible/shell/vcenter_matrix/vcenter_matrix.yml
+outputfile_csv=/etc/ansible/shell/vcenter_matrix/vcenter_matrix.csv
+
+# Prompt for some variables
+printf "%s" 'vc_username (DOMAIN\\username): '
+read -r vc_username
+printf "%s" 'vc_password: '
+read -s vc_password
+printf '\n'
+
+# Functions
+clean_generate() {
+ /bin/rm -fr "${tmpdir:-NOTHINGTODELETE}" 1>/dev/null 2&1
+}
+trap "clean_generate ; trap '' {0..20} ; exit 0 ;" 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
+
+# Prepare vault file
+echo -e "vc_username: ${vc_username}" > "${userfile}"
+echo "vc_password: ${vc_password}" >> "${userfile}"
+echo "$( pwmake 300 )" > "${pwfile}"
+ansible-vault encrypt "${userfile}" --vault-password-file "${pwfile}" 2>&1 | grep -viE 'encryption successful'
+unset vc_username vc_password
+
+# initialize files
+/bin/rm -f "${outputfile_yml}" "${outputfile_csv}"
+touch "${outputfile_yml}" ; chmod 0664 "${outputfile_yml}" ; chgrp ansible "${outputfile_yml}"
+touch "${outputfile_csv}" ; chmod 0664 "${outputfile_csv}" ; chgrp ansible "${outputfile_csv}"
+echo "vcenter_matrix:" > "${outputfile_yml}"
+
+# iterate over each vcenter
+__func() {
+local tempfile="$( TMPDIR="${tmpdir}" mktemp )"
+sed -r -e 's/\#.*$//;' -e '/^\s*$/d;' -e 's/\s*//g;' < "${infile}" > "${tempfile}"
+while read -u10 line ;
+do
+ echo "" | ansible-playbook -l $( hostname -s ) /etc/ansible/shell/vcenter_matrix/generate.yml -e "vc_hostname=${line}" -e "outputfile_yml=${outputfile_yml}" -e "outputfile_csv=${outputfile_csv}" --vault-password-file "${pwfile}" -v
+done 10< "${tempfile}"
+}
+time __func
diff --git a/generate.yml b/generate.yml
new file mode 100644
index 0000000..f306c2f
--- /dev/null
+++ b/generate.yml
@@ -0,0 +1,57 @@
+---
+# File: /etc/ansible/shell/vcenter_matrix/vcenter_matrix.yml
+# Location: /etc/ansible/shell/vcenter_matrix/
+# Author: bgstack15
+# Startdate: 2017-12-14
+# Title: Playbook
+# Purpose: Generate a csv of vcenter,VM,uuid
+# History:
+# 2017-12-15 Added exclusion for Windows
+# Usage: used by generate.sh
+# Reference:
+# Improve:
+# Document:
+
+- hosts: all
+ vars_files:
+ - /etc/ansible/shell/vcenter_matrix/username.yml
+ tasks:
+
+ - debug:
+ msg: "Now checking vcenter {{ vc_hostname }}."
+
+ - vmware_vm_facts:
+ username: "{{ vc_username }}"
+ hostname: "{{ vc_hostname }}"
+ password: "{{ vc_password }}"
+ validate_certs: no
+ delegate_to: localhost
+ register: vmfacts
+
+ - debug:
+ msg: "{{ vmfacts }}"
+
+# - pause:
+# prompt: "Please check the facts above."
+
+ - name: generate yml file
+ lineinfile:
+ dest: "{{ outputfile_yml }}"
+ line: '- { vcenter: "{{ vc_hostname }}", hostname: "{{ item.key }}", uuid: "{{ item.value.uuid }}" }'
+ insertafter: EOF
+ create: yes
+ delegate_to: localhost
+ with_dict: "{{ vmfacts.virtual_machines }}"
+ when:
+ - '"Windows" not in item.value.guest_fullname'
+
+ - name: generate csv file
+ lineinfile:
+ dest: "{{ outputfile_csv }}"
+ line: "{{ vc_hostname }},{{ item.key }},{{ item.value.uuid }}"
+ insertafter: EOF
+ create: yes
+ delegate_to: localhost
+ with_dict: "{{ vmfacts.virtual_machines }}"
+ when:
+ - '"Windows" not in item.value.guest_fullname'
diff --git a/vcenter_matrix.csv.example b/vcenter_matrix.csv.example
new file mode 100644
index 0000000..373b823
--- /dev/null
+++ b/vcenter_matrix.csv.example
@@ -0,0 +1,6 @@
+vcnorth,prod1,42208510-d3d6-348a-9cb2-8f8ef832a731
+vcnorth,prod2,42101708-7564-645a-5c75-cb54d81a28fd
+vcnorth,prod3,4210f2ab-7154-1bd3-be43-89cb59237bc2
+vcsouth,test1,4210bbd5-393c-2128-6a37-db96a68b66a3
+vcsouth,test2,42102e87-b2e4-7cf2-a984-6af10815ff8d
+vcsouth,test3,4210f983-77a0-21a4-f977-97e0beebcc20
diff --git a/vcenter_matrix.yml.example b/vcenter_matrix.yml.example
new file mode 100644
index 0000000..a2c87b9
--- /dev/null
+++ b/vcenter_matrix.yml.example
@@ -0,0 +1,7 @@
+vcenter_matrix:
+- { vcenter: "vcnorth", hostname: "prod1", uuid: "42208510-d3d6-348a-9cb2-8f8ef832a731" }
+- { vcenter: "vcnorth", hostname: "prod2", uuid: "42101708-7564-645a-5c75-cb54d81a28fd" }
+- { vcenter: "vcnorth", hostname: "prod3", uuid: "4210f2ab-7154-1bd3-be43-89cb59237bc2" }
+- { vcenter: "vcsouth", hostname: "test1", uuid: "4210bbd5-393c-2128-6a37-db96a68b66a3" }
+- { vcenter: "vcsouth", hostname: "test2", uuid: "42102e87-b2e4-7cf2-a984-6af10815ff8d" }
+- { vcenter: "vcsouth", hostname: "test3", uuid: "4210f983-77a0-21a4-f977-97e0beebcc20" }
diff --git a/vcenters.txt.example b/vcenters.txt.example
new file mode 100644
index 0000000..ff5d6bf
--- /dev/null
+++ b/vcenters.txt.example
@@ -0,0 +1,2 @@
+vcnorth
+vcsouth
bgstack15