A few months back, I had some fun playing round with Terraform with VMC on AWS. It’s fair to say, I caught a bit of the old automation bug, so this time I thought I’d play around with Ansible. This is how I got myself started….
Rather my usual rambles, this is a simple, do this, do that and you will be able to control your VMC on AWS environment using Ansible. I’m not claiming this is the best way, or even the most secure way – but it worked perfectly well for me. Here goes! Its worth adding, I’m far from a linux or ubuntu guru, but I do like to play from time to time.
I got started with a Ubuntu 18.04 LTS install. I won’t go through in the ins and outs of installing the OS, because quite frankly its super easy. Anyway, we get started once you’re logged in with your user that you created during install
First up, switch to root, run a quick update, and install the common software package
sudo -s apt update apt install -y software-properties-common
Add the ansible repo and install ansible
add-apt-repository --yes --update ppa:ansible/ansible apt install -y ansible
Next up is python, we’ll need this for pyvmomi afterwards
apt install -y python-minimal python-pip pip install pyvmomi
Then the folks at VMware kindly created a lovely SDK for us to use
pip install --upgrade git+https://github.com/vmware/vsphere-automation-sdk-python.git
For good measure (and the community vmware stuff is regularly updated) I copied down the community.vmware repo from GitHub
ansible-galaxy collection install community.vmware pip install -r ~/.ansible/collections/ansible_collections/community/vmware/requirements.txt
Just for the record, and you can check yours too, you can run a command or two to see the versions you are running
Depending on when you did the install etc, you may get different versions
So you should be in a lovely space right now, where you can test ansible is working locally
ansible -m ping localhost
All this does, is ping the host you are on, but gives you the chance to see your first bit of ansible output
So, you’re up and running – well done. If you’ve got this far in under an hour, you’re waaaay faster than me!
There are tonnes of guides showing you how to connect to a local on premise vcenter, but I was curious and wanted to know whether it would work connecting to my VMC SDDC as I couldn’t find any decent documentation or blogs (my preferred learning tool).
For whatever reason (I’m sure someone will explain) I didn’t have the typical ansible directory structure created or the .cfg, so I created the directory structure in /etc
mkdir ansible cd ansible mkdir playbooks cd playbooks
Once in there, create your first .yml file and get going
One thing I have quickly learnt about yaml, is making sure my indentations are consistent. From what I understand, it doesn’t care what spacing you use, as long as you are consistent with it. I double spaced in this file – no idea if this is good practise or not 😀
# vmctest.yml - name: Clone VMC VM hosts: localhost gather_facts: no tasks: - name: Clone the template vmware_guest: hostname: "vcenter.sddc-xx-xx-xx-xxx.vmwarevmc.com" username: "[email protected]" password: "password" resource_pool: "Compute-ResourcePool" datacenter: "SDDC-Datacenter" folder: "/Workloads" cluster: "Cluster-1" networks: - name: "SDDC-Network" #Enter your own here hardware: num_cpus: 2 memory_mb: 4096 validate_certs: False name: "Cloned-Ansible-VM" template: "WinServer-2016-Template" datastore: "WorkloadDatastore" state: poweredon wait_for_ip_address: yes register: new_vm
If you are too excited (like I was) and got too gung ho, you’ll find you indented poorly, missed quotation marks, or spelt your own surname wrong (Its not like I’ve been writing it for 30-odd years or anything….)
Anyway, save the file by pressing CTRL+X, pressing Y to save and enter to save using that filename you used before. Now you’re ready to go – type the below and watch ansible do magic!
It will take a while, because what you’ve done is:
- Connected to your VMC vCenter
- Cloned an existing templated VM into the correct datastore
- Added it to a resource pool and folder
- Set the CPU, RAM and Network to be assigned to your VM
- Powered it on and given it an IP
Hopefully you see an output similar to this:
All that it is saying is that it has ran ok and that you’ve changed 1 element, your new VM. If you check your vCenter console, you should see your VM in the root of your Workloads folder with a name of “Cloned-Ansible-VM”.
I realise this is nothing particularly fancy, but to those wanting to get started in the automation world need to use a flavour of linux. If its not your background (I’m looking at those thousands of Wintel engineers out there), then it can be a bit of a struggle. Hopefully this helps someone get started