Ansible vSphere Set-up Notes

Mindwatering Incorporated

Author: Tripp W Black

Created: 06/24/2021 at 10:28 AM

 

Category:
Linux
Other

Ansible VMware REST API
Prereqisites:
- Ansible => 2.9.10
- Python => 3.6
- AIOHTTP (virtualenv)


Setup via Pip:
$ pip install aiohttp
(CentOS/Fedora - $ sudo dnf install python3-aiohttp)

Setup via yum:
Enable the extended epel library
$ sudo yum install epel-release
<y> to install the one package
$ sudo yum install ansible
<y> to install the ~20 packages


$ ansible-galaxy collection install vmware.vmware_rest


Notes:
MORefID key is used to identify Datacenter(s), folder(s), and VMs
We don't have to follow the tree. For example, we can get the cluster, without using the datacenter MORefID.
Docs are at:
$ ansible-doc -t module vmware.vmware_rest.vcenter_cluster_info

To test from a local Ansible machine:
$ export VMWARE_HOST=myvsphere.mindwatering.net
$ export VMWARE_USER = myadmin@vsphere.local
$ export VMWARE_PASSWORD = mypwd
$ export VMWARE_VALIDATE_CERTS = no
$ ansible-playbook myplaybook.yml

$ vi listDatacenters.yml
- name: List of Datacenters
vmware.vmware_rest.vcenter_datacenter_info:
register: mw_datacenters

$ ansible-playbook listDatacenters.yml
Output:
{
"value": [
{
"name": "mw_dc",
"datacenter": "datacenter-1234"
}
],
"changed": false
}





____________________________

How to set-up a custom "venv" virtual environment for a project:

There are two paths, the community "packages" or official repos.
This example uses the community vSphere option.

Community Version:
1. Set-up folder:
cd /var/lib/awx/venv/
$ sudo -i
# umask 0022
# mkdir custom-venv
# chmod 0775 ./custom-venv

Skip next line from ansible docs - not working in 3.6, with pip --install. So we do at the end, instead.
<! -- # chown awx:awx ./custom-venv -->

2. Create the virtualenv (virtual environment) w/in the new folder just created:
# python3 -m venv /var/lib/awx/venv/custom-venv

Below the instructions deviate. Some say to fence a source here. docs.ansible.com does not. However, we source to limit the subsequent actions w/in the venv.
Notes:
- Type ./custom-venv/bin/deactivate to get out of the fence.
- Customize the version of Ansible installed using the syntax below, or you can install a specific version, as needed.
- If you are setting up a venv like another, perform a ./bin/pip freeze > requirements.txt to get all the modules installed in the source. Use the file as the source for the installation of the second environment. Warning, sources go away quickly with pip. Expect dependency issues and lost productivity for complicated virtualenv folders.

# source ./custom-venv/bin/activate
# cd custom-venv
# pwd
/var/lib/awx/venv/custom-venv
# ./bin/pip install --upgrade pip
<wait>
# ./bin/pip install psutil
# ./bin/pip install wheel
# ./bin/pip install -U "ansible==3.6*"
# ./bin/pip install requests
# ./bin/pip install pyVim

If doing the community collection:
# ./bin/pip install pyVmomi

If doing the galaxy collection:
# ./bin/pip install aiohttp

If we need to do tags:
# ./bin/pip install --upgrade git+https://github.com/vmware/vsphere-automation-sdk-python.git


3. We see issues with awx not being able to see/consume modules, so we make awx:awx the owner now:
# chown -R awx:awx ./custom-venv

4. Deactivate:
# deactivate






previous page