Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

Using Ansible for Managing VMware vSphere Infrastructure

On this second part article of after How to install Ansible on Linux for vSphere configuration, I have written here 2 Ansible playbook files (play tasks created) to configure VMWare ESXi servers. Language for the ansible playbook files is YAML/YML format. When using Ansible to use on VMware vSphere, pyVmomi python module is required (already installed and shown in earlier article). Ansible uses vSphere API to interaction and configuration.

The first file secrets.yml is a Inventory file, first 3 dash is start of the file, I am mentioning vCenter IP, username and password. and they are self explanatory. 
In the second file playbook.yml, I have used secrets.yml, defined tasks to connect vCenter server and configuration of infrastructure. below is the break down of the playbook.yml script.

Line 01: 3 dashes are the start of file.
Line 02: All the commands/tasks will be executed from localhost (ansible host).
Line 03 to 08: I am including secrets.yml, all the information mentioned in the file will be used as variable properties, main variable name is secret created.
Line 10 to 19: This information is used to login to vCenter server, variable names will be ie: secret.vcenter, secret.username, secret.password used throughout the playbooks. It will register the output as variable login, it contains authentication info. To perform this tasks uri module is used, it connects and interact with webservices of vCenter server API. (No direct ESXi can be used as it doesn't have APIs connect).
Line 21 to 28: Again it is using uri module. Using cached API/cookies login information in login variable, fetch the ESXi servers list information from vCenter Server and store it in by registering vchosts variable.
Line 30 to 44: This is last part to perform tasks on all the ESXi servers. vmware_host_config_manager module is used here, and configuring ESXi advanced configuration. Just for more information it is using loop for each ESXi using with_items, each ESXi server is mentioned as item.name.

You can download VMWare vSphere yaml Ansible scripts here, yml scripts are also available on github.com/kunaludapi.

secrets.yml

1
2
3
4
---
vcenter:  192.168.0.1
username: administrator@vsphere.local
password: 123456

playbook.yml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
---
- hosts: localhost
  
  tasks:
    - name: Include Secret Environment Items
      include_vars:
        file: secrets.yml
        name: secret

    - name: vCenter Login
      uri:
        url: "https://{{secret.vcenter}}/rest/com/vmware/cis/session"
        force_basic_auth: yes
        method: POST
        user: "{{secret.username}}"
        password: "{{secret.password}}"
        status_code: 200
        validate_certs: no
      register: login

    - name: Get hosts from vCenter
      uri:
        url: "https://{{secret.vcenter}}/rest/vcenter/host"
        force_basic_auth: yes
        validate_certs: no
        headers:
          Cookie: "{{login.set_cookie}}"
      register: vchosts

    - name: Set ESXi shell time out
      vmware_host_config_manager:
        hostname: "{{secret.vcenter}}"
        username: "{{secret.username}}"
        password: "{{secret.password}}"
        esxi_hostname: "{{item.name}}"
        options:
          'UserVars.ESXiShellTimeOut': 1800
          'NFS.MaxVolumes': 256
          'NFS.HeartbeatMaxFailures': 10
          'NFS.HeartbeatTimeout': 5
          'NFS.HeartbeatFrequency': 12
          'Net.TcpipHeapSize': 32
        validate_certs: no
      with_items: "{{vchosts.json.value}}"

Both of the files are kept on same folder and to execute/run this playbook use below command.

ansible-playbook playbook.yml

Playbook will start gathering facts (collecting information), and shows which tasks are ok, what is changed or failed. From below screenshot it shows the end result is successful.

vmware vsphere ansible ansible-playbook playbook.yml yaml play task gathering facts localhost esxi shell time out play recap unreachable changed failed skipped rescued ignored.png

I can confirmed on the vCenter > Esxi > Advanced System Settings, settings are applied successfully.

vmware vsphere vcenter esxi advanced system settings devops ansible yml yaml gathering facts automation configuration ansible-playbook playbook module.png

Useful articles
How to install Docker on Linux
Cannot connect to the Docker daemon at unix:var run docker.sock. Is the docker daemon running
Docker Error response from daemon i\o timeout internet proxy
How to install Ansible on Linux for vSphere configuration
How to Setup Passwordless SSH Login on Windows
configure remote ssh extension on visual studio code
VS code remote ssh could not establish to host, connecting was canceled

Go Back



Comment

Blog Search

Page Views

11274857

Follow me on Blogarama