Ansible 2.9x Restart vSphere VMs

Mindwatering Incorporated

Author: Tripp W Black

Created: 10/13 at 09:51 PM

 

Category:
Linux
RH AAP

Task restart vSphere VMs from Ansible 2.9x - Community Version

venv setup:
1. Setup custom virtual environment folder:
$ cd /var/lib/awx/venv/
$ sudo -i
# umask0022
# mkdir custom-venvvcenter
# chmod 0755 ./custom-venvvcenter

2. Change owner:
Note: Did not work. We had to skip this step.
# chown awx:awx ./custom-venvcenter

3. Fence venv:
# source ./custom-venvvcenter/bin/activate
...
# deactivate
(To get out of the fence)

3. Add packages to venv needed for vSphere vCenter:
# cd custom-venvvcenter
# pwd
# ./bin/pip install --upgrade pip
# ./bin/pip install psutil
# ./bin/pip install wheel
# ./bin/pip install -U "ansible==2.9.*"
# ./bin/pip install requests
# ./bin/pip install pyVim
# ./bin/pip install pyVmomi

4. Install the IO HTTP package, to utilize the certified galaxy collection:
# ./bin/pip install aiohttp

5. (Optional) Install tag support:
# ./bin/pip install --upgrade git+https://github.com/vmware/vsphere-automation-sdk-python.git



Inventory File:
/vars/inventory/static_inventory/vmdevhosts.yml
all:
hosts:
children:
devapphosts:
app1vm.mindwatering.net:
app2vm.mindwatering.net:
app3vm.mindwatering.net:
devdbhosts:
db1vm.mindwatering.net:
db2vm.mindwatering.net:
vcenterdev:
vcenterdev.mindwatering.net


Account Role File:
/vars/accounts/devaccounts.yml
---
vsphererestapi:
vcenter:
userid:
ldaploginid
userpwd:
nottherealpassword
userdomain:
mindwatering.net


Playbook Scripts:

tasks/poweroffvm.yml
---
# power-off a VM

- name: vSphere Shutdown VM
hosts: localhost
connection: local
gather_facts: no
vars:
vcenternm: {{vcenterfqdn}}
vcenterid: {{vcenterid}}
vcenterpwd: {{vcenterpwd}}
vmname: {{vmname}}
tasks:
- name: Shutdown VM, {{vmname}}, and wait
vmware_guest_powerstate:
hostname: "{{vcenternm}}"
username: "{{vcenterid}}"
password: "{{vcenterpwd}}"
name: "{{vmname}}"
validate_certs: False
state: shutdown-guest
state_change_timeout: 2000
register: wait
ignore_errors: yes

- name: Force Poweroff VM, {{vmname}}, if Shutdown Timeout
vmware_guest_powerstate:
hostname: "{{vcenternm}}"
username: "{{vcenterid}}"
password: "{{vcenterpwd}}"
name: "{{vmname}}"
validate_certs: False
state: shutdown-guest
state_change_timeout: 2000
register: poweroff
when: wait.msg is search('Timeout')


tasks/poweronvm.yml
---
# power-on a VM

- name: vSphere Start VM
hosts: localhost
connection: local
gather_facts: no
vars:
vcenternm: {{vcenterfqdn}}
vcenterid: {{vcenterid}}
vcenterpwd: {{vcenterpwd}}
vmname: {{vmname}}
tasks:
- name: Power-on VM, {{vmname}}
vmware_guest_powerstate:
hostname: "{{vcenternm}}"
username: "{{vcenterid}}"
password: "{{vcenterpwd}}"
name: "{{vmname}}"
validate_certs: False
state: powered-on


restart_devvms.yml (main folder)
---
# Restart Dev Environment VMs in order desired
# shutdown order:
# - web appliances
# - db appliance
# restart order:
# - db appliance
# - web appliances

- name: Set-up vars
hosts: localhost
become: true
gather_facts: false
connection: local

include_vars:
file: vars/inventory/static_inventory/vmdevhosts.yml
name: vmdevapphosts

file: vars/inventory/static_inventory/vmdevhosts.yml
name: vmdevdbhosts

file: vars/inventory/static_inventory/vmdevhosts.yml
name: vcenterdev

file: vars/accounts/devaccounts.yml
name: userid

file: vars/accounts/devaccounts.yml
name: userdomain

vars_prompt:
- name vcenterpwd
prompt: Enter the vCenter user id pwd

- name: Shutdown App VM(s)
tasks:
- include_tasks: tasks/poweroffvm.yml
vars:
vmname: {{item}}
loop: "{{vmdevapphosts}}"

- name: Shutdown DB VM(s)
tasks:
- include_tasks: tasks/poweroffvm.yml
vars:
vmname: {{item}}
loop: "{{vmdevdbhosts}}"

- name: Power-on DB VM(s)
tasks:
- include_tasks: tasks/poweronvm.yml
vars:
vmname: {{item}}
loop: "{{vmdevdbhosts}}"

- name: Power-on App VM(s)
tasks:
- include_tasks: tasks/poweronvm.yml
vars:
vmname: {{item}}
loop: "{{vmdevapphosts}}"






previous page