Automate VM creation with Ansible (VMware, Bind, Zabbix)

Hello everybody

In my environment, I own VMware with vCenter, Bind as DNS server and Zabbix as a monitoring tool.

What I would like to do with Ansible is to create a virtual machine from an already predefined template, add an entry in DNS, and add the VM in Zabbix monitoring.

I know Ansible has modules for DNS, Vmware, and Zabbix, but has anyone done it yet?

One of the many questions I have is regarding the IP address of the new VM, how do I change the IP address of the new VM using Ansible and already add in Ansible inventory?

Thanks for all the help.

In my experience I’ve wanted to separate these kinda things out into a couple plays, since we’re dealing with logically different resources(hypervisor, dns, monitoring).

Full disclosure, I haven’t used any of the described modules, but I’ve used somewhat similar patterns with ec2, security groups, firewalld, etc.

  1. Provision a machine with the vmware module, and then use a register: guest statement to retain some of the variables returned from the provisioning in a variable called guest.

It looks like the vmware_guest_module has some return values, though the documentation doesn’t get into what sort of fields they contain:

https://docs.ansible.com/ansible/latest/modules/vmware_guest_module.html#return-values

  1. Using the nsupdate module(https://docs.ansible.com/ansible/2.3/nsupdate_module.html), you will likely want to pass in the ip address that is hopefully contained within the guest variable retained in the last step, and then associate it to the name that you have in mind.

  2. It looks like theres a handful of zabbix modules to choose from here: https://docs.ansible.com/ansible/latest/modules/list_of_monitoring_modules.html#zabbix

However, I’ve never used zabbix — if there’s a way that you can tell zabbix “monitor service-name on address: 10.0.0.1” that would be the way I would recommend using the abstractions that the module provides.

Note: if you plan to do this for more than one guest, it may make sense to consider abstracting this process into a role, so that you can specify the name to provision the machines with as part of your configuration data.

An admittedly rough idea of the top of my head for some role data that provisions a guest at 10.0.3.5 called ’newnode.example.org’ on the nameserver at 10.0.1.5, using a vmware host at 10.0.2.5.

Hello Will Weber,
Thank you for your answer, certainly “helps more than confuses”
I still have questions about the IP address of the VM after being created by Ansible.
Do you have any idea how to change the IP address of the VM and later add this new host to the Ansible inventory file?

You can define the IP address of a VM being created from the template at the time of creation and then you can dynamically create an inventory group during the play execution that you can use for further processing. Or you can use a template to create additional inventory items based on VM facts being gathered or some other mechanism that you could then use as an additional inventory item. I have some examples that I would be glad to share if interested.

For example, you could define a vms.yml file with the VMs you want to manage as in https://gist.github.com/mrlesmithjr/2a573a82bf4ce944316bffcacca80899

An example of a Jinja2 template to manage your inventory https://gist.github.com/mrlesmithjr/3c149124f2894c572939bf41c6bc5ad5

And then your Ansible tasks to manage those VMs might look like https://gist.github.com/mrlesmithjr/3203c04299b40ae5f07dac70f3c20bc6

The key here to assigning the IP address at creation time comes from the vmware_guest module and the networks parameter.