create Virtual Machines in VMware with ansible

Hi

Mainly I am a system administrator. To live freely, I work  project based and I give trainings. In my current project, I have some additional roles which include managing a vmware environment.

I must create new VMs in vsphere 6.5 environment fast. I have ansible under by belt. so here how I have done this.

Scenario: I have 5 VMs to create. My config files are the following;

  • testVM.csv: this includes VM properties such as datastore, Network config etc. My each VM has 3 NICs.. If you need less, then you can trim these items in the csv file
  • makeVM.yml: it creates VMs according to testVM.csv file. you will need Pyvmomi python library. you can install it with pip
  • creds.txt: vcenter credentials are here
  • hosts file: this must include only one IP. otherwise ansible tries to create a VM for each host in this file..


csv file format:
VMname;cpuCount;MemorySize;diskSize;dataStoreName;PortGroupName_NIC1;IP_NIC1;Netmask_NIC1;defGW_NIC1;PortGroupName_NIC2;IP_NIC2;Netmask_NIC1;defGW_NIC2;PortGroupName_NIC3;IP_NIC3;Netmask_NIC3;defGW_NIC3;dns1;dns2

selcuk@yoda:/myproject/ansible$ head testVM.csv 
testvm1;2;8192;100;gumus01;VLAN-262;10.13.13.51;255.255.255.0;10.13.13.1;VLAN-263;10.13.14.51;255.255.255.0;10.13.14.1;VLAN-264;10.13.15.51;255.255.255.0;10.13.15.1;10.13.12.1;10.13.12.2;
testvm2;2;8192;100;gumus01;VLAN-262;10.13.13.52;255.255.255.0;10.13.13.1;VLAN-263;10.13.14.52;255.255.255.0;10.13.14.1;VLAN-264;10.13.15.52;255.255.255.0;10.13.15.1;10.13.12.1;10.13.12.2;
testvm3;2;8192;100;gumus01;VLAN-262;10.13.13.53;255.255.255.0;10.13.13.1;VLAN-263;10.13.14.53;255.255.255.0;10.13.14.1;VLAN-264;10.13.15.53;255.255.255.0;10.13.15.1;10.13.12.1;10.13.12.2;
testvm4;2;8192;100;gumus01;VLAN-262;10.13.13.54;255.255.255.0;10.13.13.1;VLAN-263;10.13.14.54;255.255.255.0;10.13.14.1;VLAN-264;10.13.15.54;255.255.255.0;10.13.15.1;10.13.12.1;10.13.12.2;
testvm5;2;8192;100;gumus01;VLAN-262;10.13.13.55;255.255.255.0;10.13.13.1;VLAN-263;10.13.14.55;255.255.255.0;10.13.14.1;VLAN-264;10.13.15.55;255.255.255.0;10.13.15.1;10.13.12.1;10.13.12.2;
;;;;;;;;;;;;;;;;;;;



selcuk@yoda:/myproject/ansible$ cat creds.txt 
---
 username: administrator@vsphere.local
 password: vcenterPW


selcuk@yoda:/myproject/ansible$ cat makeVM.yml 

--- 
- hosts: all 
  connection: local 
  gather_facts: false 
  vars_files:
  - /myproject/ansible/creds.txt 



  tasks: 
  - name: Create a VM from a template 
    vmware_guest: 
      hostname: vcenterIP 
      username: "{{username}}" 
      password: "{{password}}" 
      validate_certs: no 
      datacenter: vcenterDC 
      folder: testvm 
      cluster: cluster1 
      name: "{{item}}" 
      state: poweredon 
      guest_id: centos7_64Guest 
      template: Centos74 
      disk: 
      - size_gb: "{{ lookup('csvfile', '{{item}} file=testVM.csv delimiter=; col=3') }}" 
        type: thick 
        datastore: "{{ lookup('csvfile', '{{item}} file=testVM.csv delimiter=; col=4') }}"
      hardware: 
        memory_mb: "{{ lookup('csvfile', '{{item}} file=testVM.csv delimiter=; col=2') }}"
        num_cpus: "{{ lookup('csvfile', '{{item}} file=testVM.csv delimiter=; col=1') }}" 
      networks: 
      - name: "{{ lookup('csvfile', '{{item}} file=testVM.csv delimiter=; col=5') }}" 
        ip: "{{ lookup('csvfile', '{{item}} file=testVM.csv delimiter=; col=6') }}"
        netmask: "{{ lookup('csvfile', '{{item}} file=testVM.csv delimiter=; col=7') }}"
        gateway: "{{ lookup('csvfile', '{{item}} file=testVM.csv delimiter=; col=8') }}"
      - name: "{{ lookup('csvfile', '{{item}} file=testVM.csv delimiter=; col=9') }}" 
        ip: "{{ lookup('csvfile', '{{item}} file=testVM.csv delimiter=; col=10') }}"
        netmask: "{{ lookup('csvfile', '{{item}} file=testVM.csv delimiter=; col=11') }}"
      - name: "{{ lookup('csvfile', '{{item}} file=testVM.csv delimiter=; col=13') }}"
        ip: "{{ lookup('csvfile', '{{item}} file=testVM.csv delimiter=; col=14') }}"
        netmask: "{{ lookup('csvfile', '{{item}} file=testVM.csv delimiter=; col=15') }}"
      customization: 
        domain: yourdomain.com 
        dns_servers: 
        - 10.11.12.13 
        - 10.11.12.14

    with_items:
    - testvm1
    - testvm2
    - testvm3
    - testvm4
    - testvm5



selcuk@yoda:/myproject/ansible$ cat hosts
192.168.1.36


Now to create VMs just type

selcuk@yoda:/myproject/ansible$ ansible-playbook -i hosts makeVM.yml 


it took 5 mins to create 5 VMs.. Not bad...


PS: I am using ansible 2.5 . my PYvmomi version:

selcuk@yoda:/myproject/ansible$ pip show Pyvmomi
Name: pyvmomi
Version: 6.5.0.2017.5.post1
Summary: VMware vSphere Python SDK
Home-page: https://github.com/vmware/pyvmomi
Author: VMware, Inc.
Author-email: jhu@vmware.com
License: License :: OSI Approved :: Apache Software License
Location: /usr/local/lib/python2.7/dist-packages
Requires: requests, six



you can install python dependency   with this
 pip install pyvmomi




Yorumlar

Bu blogdaki popüler yayınlar

Yüksek Hizmet Sürekliliği (High Availability)