Configure ESXi with Ansible

In my job I have many projects I do. One of them is setting up new VMware environments. Goal here is to configure a new esx\vcenter environment with Ansible. I was able to accomplish this with 2 playbooks and a group_vars file.

Enough of that, lets get into it.

The first playbook will configure DNS, Hostname,vswitches, portgroups and vmkernel. You will need to run this on a per host basis and it will prompt you for the ESXi host and IP address of the vMotion vmkernel port.

---
- vars_prompt:
    - name: "esxi_hostname"
      prompt: "Which ESX Host"
      private: no
    - name: "ip_address"
      prompt: "IP of vMotion vmkernal Port"
      private: no
  hosts: setup
  become: true
  become_user: root
  gather_facts: false
  tasks:
  - name: Configure DNS and Hostname
    vmware_dns_config:
      hostname: "{{ esxi_hostname }}"
      username: "{{ esxi_username }}"
      password: "{{ esxi_password }}"
      change_hostname_to: "{{ esxi_hostname }}"
      domainname: "{{ domainname }}"
      dns_servers:
          - "{{ dns_server1 }}"
          - "{{ dns_server2 }}"
      validate_certs: False
    delegate_to: localhost
  - name: Add vMotion Switch
    vmware_vswitch:
      hostname: "{{ esxi_hostname }}"
      username: "{{ esxi_username }}"
      password: "{{ esxi_password }}"
      esxi_hostname: "{{ esxi_hostname }}"
      switch: "{{ vswitch1 }}"
      nics:
        - "{{ vmnic_1 }}"
        - "{{ vmnic_4 }}"
      mtu: 1500
      validate_certs: False
      state: present
    delegate_to: localhost
  - name: Add Production Switch
    vmware_vswitch:
      hostname: "{{ esxi_hostname }}"
      username: "{{ esxi_username }}"
      password: "{{ esxi_password }}"
      esxi_hostname: "{{ esxi_hostname }}"
      switch: "{{ vswitch2 }}"
      nics:
        - "{{ vmnic_2 }}"
        - "{{ vmnic_5 }}"
      mtu: 1500
      validate_certs: False
      state: present
    delegate_to: localhost
  - name: Add to Management Switch
    vmware_vswitch:
      hostname: "{{ esxi_hostname }}"
      username: "{{ esxi_username }}"
      password: "{{ esxi_password }}"
      esxi_hostname: "{{ esxi_hostname }}"
      switch: "{{ vswitch0 }}"
      nics:
        - "{{ vmnic_3 }}"
      mtu: 1500
      validate_certs: False
      state: present
    delegate_to: localhost
  - name: Add vMotion Portgroup
    vmware_portgroup:
      hostname: "{{ esxi_hostname }}"
      username: "{{ esxi_username }}"
      password: "{{ esxi_password }}"
      hosts: "{{ esxi_hostname }}"
      switch: "{{ vswitch1 }}"
      portgroup: "{{ portgroup_name_vm }}"
      vlan_id: "{{ vlan_id_vm }}"
      state: present
      validate_certs: False
    delegate_to: localhost
  - name: Add Management Network VM Portgroup
    vmware_portgroup:
      hostname: "{{ esxi_hostname }}"
      username: "{{ esxi_username }}"
      password: "{{ esxi_password }}"
      hosts: "{{ esxi_hostname }}"
      switch: "{{ vswitch2 }}"
      portgroup: "{{ portgroup_name_pr }}"
      vlan_id: "{{ vlan_id_pr }}"
      state: present
      validate_certs: False
    delegate_to: localhost
  - name: Add vMotion vmkernel port with vMotion TCP/IP stack
    vmware_vmkernel:
      hostname: '{{ esxi_hostname }}'
      username: '{{ esxi_username }}'
      password: '{{ esxi_password }}'
      esxi_hostname: '{{ esxi_hostname }}'
      vswitch_name: "{{ vswitch1 }}"
      portgroup_name: "{{ portgroup_name_vm }}"
      network:
        type: 'static'
        ip_address: "{{ ip_address }}"
        subnet_mask: 255.255.255.0
        tcpip_stack: vmotion
      state: present
      validate_certs: False
    delegate_to: localhost

Next one will configure vcenter which will include creating of datacenter, cluster, setting of NTP and start the service

---
- hosts: setup
  become: true
  become_user: root
  gather_facts: false
  tasks:
  - name: Create Datacenter
    vmware_datacenter:
      hostname: "{{ vcenter_hostname }}"
      username: "{{ vcenter_username }}"
      password: "{{ vcenter_password }}"
      datacenter_name: "{{ datacenter_name }}"
      validate_certs: False
      state: present
    delegate_to: localhost
  - name: Create Cluster
    vmware_cluster:
      hostname: "{{ vcenter_hostname }}"
      username: "{{ vcenter_username }}"
      password: "{{ vcenter_password }}"
      datacenter_name: "{{ datacenter_name }}"
      cluster_name: "{{ cluster_name }}"
      validate_certs: False
      state: present
    delegate_to: localhost
  - name: Set NTP Servers
    vmware_host_ntp:
      hostname: "{{ vcenter_hostname }}"
      username: "{{ vcenter_username }}"
      password: "{{ vcenter_password }}"
      cluster_name: "{{ cluster_name }}"
      ntp_servers:
          - "{{ ntp_servers }}"
      validate_certs: False
      state: present
    delegate_to: localhost
  - name: Start ntpd Service
    vmware_host_service_manager:
      hostname: "{{ vcenter_hostname }}"
      username: "{{ vcenter_username }}"
      password: "{{ vcenter_password }}"
      cluster_name:
      service_name: ntpd
      service_policy: on
      state: present
      validate_certs: False
    delegate_to: localhost
  - name: Start ssh Service
    vmware_host_service_manager:
      hostname: "{{ vcenter_hostname }}"
      username: "{{ vcenter_username }}"
      password: "{{ vcenter_password }}"
      cluster_name: "{{ cluster_name }}"
      service_name: TSM-SSH
      service_policy: on
      state: present
      validate_certs: False
    delegate_to: localhost

This is the group_vars file I used.

I hope this helps and have fun with Ansible.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s