How to run Ansible on vCenter

This type of produce it’s definitely extraordinary and going to open your mind to new automation utilities that you can run in your environment, I am going to show you how to run ansible playbook on your vCenter, it may to be for ansible beginners who wants to know how to run a simple playbooks
Hopefully you already have installed ansible on your machine, if not, please go over this command of quick ansible installation:

sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get install python -y
sudo apt update
sudo apt install software-properties-common
sudo apt-add-repository --yes --update ppa:ansible/ansible
sudo apt install ansible

Requirements:
– Make sure you have fully access to your vCenter using the 443 and 22:
telnet to your vCenter from ansible machine.
– Make sure you are ruining the following procedure with root account.
– In my lab environment i am running Ubuntu 18.04
– In order to work with VMware modules in ansible, we have to install the pyVmomi python SDK:

sudo pip install pyvmomi

Open your Linux machine, and create new folder which will contains your ansible playbooks:

mkdir ansible

Go into the folder using:

cd ansible

Create ansible.cfg file:

nano ansible.cfg

– Host file contains the group and services that yo want to run playbooks, in different words your inventory file.
By default ansible host file located on  “/etc/ansible/hosts/” , we can change the location file by specifying the new location on cfg file.

Paste the following syntax:

[defaults]
inventory=./myhost
gathering=no

Save the file.

nano myhosts

basically based to ansible requirements we have to specify vCenter IP’s,  but in this playbook we already specifying  the vCenter’s IP Address, so we are adding this just because ansible  requires specifying any hosts file so you can just add your vCenter IP.

[vcenter]
192.168.31.41

And at this point we have to the create the tasks, for my demonstration I will use on the following example: “Set NTP servers for all ESXi Host in given Cluster“, which take from:
https://docs.ansible.com/ansible/2.5/modules/vmware_host_ntp_module.html

nano Setntponcluster.yml

Paste this:

---
- name: Set NTP servers for all ESXi Host in given Cluster
  hosts: vcenter
  tasks:
   - vmware_host_ntp:
      hostname: '{{ vcenter_hostname }}'
      username: '{{ vcenter_username }}'
      password: '{{ vcenter_password }}'
      cluster_name: '{{ cluster_name }}'
      validate_certs: False
      state: present
      ntp_servers:
          - time1.google.com
     delegate_to: localhost

Save the file,  as you can see the I’m using on ‘{{}}’’ which means variables, I have to set these variable in order to run this playbook successfully, you can also state your details without variable it will work as well.

Into the ansible folder create new folder using the following command:

mkdir group_vars

Change directory to the folder:

cd group_vars

We have to create a new “yaml” file exact per hosts’s group name, so let’s run:

nano vcenter.yml

Into the file, paste this:

---

 vcenter_hostname: vcenterip
 vcenter_username:'your vcenter users'
 vcenter_password: 'your vCenter password'
 cluster_name: rnd-cluster

Save the file and go one folder back to the ansible folder using “cd ..”

At this point the playbook is ready for use run it using this:

ansible-playbook Setntponcluster.yml

In a case you want to run this on several clusters you can use this.
Using (loop ) which ‘{{item }}” I call to several clusters, you have to remove the “cluster_name” on group_vars file and here you have to state it like this:

---

- name: Set NTP servers for all ESXi Host in given Cluster
  hosts: vcenter
  tasks:
   - vmware_host_ntp:
      hostname: '{{ vcenter_hostname }}'
      username: '{{ vcenter_username }}'
      password: '{{ vcenter_password }}'
      cluster_name: '{{ item }}'
      validate_certs: False
      state: present
      ntp_servers:
          - time1.google.com
     delegate_to: localhost
     loop:
        - rnd-cluster
        - it-cluster