Menu

Virtual Geek

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

Ansible for VMwary Using vmware_vm_inventory dynamic inventory plugin

Since I started using Ansible for VMware management, Daily I am exploring new thing on vSphere Ansible, and stumbled upon interesting dynamic inventory plugin named vmware_vm_inventory from url https://docs.ansible.com/ansible/latest/plugins/inventory/vmware_vm_inventory.html (for more about parameter check url). The usage of the same is very easy. This plugin helps to get virtual machines as inventory hosts from VMware environment. after pulling the information the inventory_hostname is always the 'Name' and UUID of the virtual machine. 

Related Articles
How to install Ansible on Linux for vSphere configuration
Using Ansible for Managing VMware vsphere Infrastructure

To start with vmware_vm_inventory, I created a configuration ansible.cfg file with below text to enable it as plugin. All the required files I will be keeping in single directory.

[inventory]
enable_plugins = vmware_vm_inventory

ansible.cfg testing plugin for vm vmware_vm_inventory ansible automation programming devops yml yaml scripting enable_plugins inventory ini.png

Next create yml file which must be ending with .vmware.yml or .vmware.yaml as a YAML configuration file. If you have tags defined for VMs in your environment define it as true, Ansible with result vmlist with tags as groups, I don't have tags and I commented it.

plugin: vmware_vm_inventory
strict: False
hostname: 192.168.34.20
username: administrator@vsphere.local
password: Computer@1
validate_certs: False
#with_tags: True

I will just test the configuration script with below command to check script is able to connect vCenter and fetch the vms list correctly. After running below command it was showing VMs list with there configuration successfully.

ansible-inventory --list -i vm.vmware.yml

vm.vmware.yml ansible.cfg plugin vmware_vm_inventory administrator ansible vmware vsphere vcenter esxi vm virtual machine inventory ansible-inventory --list yaml automation.png

Next I tested another command to retrieve the list in graph form.

ansible-inventory --graph -i vm.vmware.yml/

Information gathering is successful.

testing plugin vm vmware inventory ansible ansible-inventory --graph vm.vmware.yml yaml devops automation declarative programming.png

Above all the commands where one-liner ad-hoc. I want to use this plugin in the playbook. Below is the playbook I am using, once it is connected to vCenter successfully, all the information is gathered as facts into global default variable vars. And once information is gathered I can use the same variable to process further for automation.

ansible-playbook get_vms.yml -i vm.vmware.yml

---
  - name: Get vms list
    hosts: localhost
    gather_facts: no

    tasks:
      #- name: gather list
        #set_fact:
          #vmlist: "{{ lookup('env','vcenter_host') }}"

      - debug:
          var: vars.groups.all

get_vms  ansible ansible-playbook vmware with vsphere vcenter ansible api connection automation devops inventory py script vmware_vm_inventory python dynamic static inventory.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

11360625

Follow me on Blogarama