Use Ansible and Cobbler to create a VMware Linux guest

This Ansible playbook will help you automate the creation a Linux virtual machine in your VMware datacenter. My goal was to create something relatively simple which would demonstrate the Ansible + vsphere-guest tools quickly.

After creating the VM, we will get the MAC address info to add into to  Cobbler to do a netinstall of the operating system for the newly built virtual machine. This technique should allow you to auto-provision new VMs.

 

--- # Create a new VM on an ESX server
- hosts: localhost
  connection: local
  vars:
    vcenter_hostname_fqdn: "datacenter1.exampledomain.com"
    cluster: "Clustername"
    datastore: 'Datastore1'
    esxi_hostname: 'esxhost.exampledomain.com'
    datacenter_name: "Big Datacenter West US"
    vmnetwork: 'SERVERS'
     # Due to a bug, you must always put /Resources in front of your pool name
    resource_pool_name: "/Resources/Linux"
    guestname: "vmtest1"
  vars_prompt:
    - name: "username"
      prompt: "Enter username with access to vCenter such as DOMAIN\username"
      private: no
    - name: "password"
      prompt: "Enter password"
  tasks:
    - vsphere_guest:
        vcenter_hostname: "{{ vcenter_hostname_fqdn }}"
        username: "{{ username }}"
        password: "{{ password }}"
        guest: "{{ guestname }}"
        state: powered_on
        cluster: "{{ cluster }}"
        resource_pool: "{{ resource_pool_name }}"
        vm_extra_config:
          vcpu.hotadd: yes
          mem.hostadd: yes
          notes: This test VM is autocreated by an Ansible script
        vm_disk:
          disk1:
          size_gb: 30
          type: thin
          datastore: "{{ datastore }}"
        vm_nic:
         nic1:
           type: vmxnet3
           network: "{{ vmnetwork }}"
           network_type: dvs  # or standard
       vm_hardware:
         memory_mb: 2048
         num_cpus: 2
         osid: centos64Guest # had to leave this 64 even though I tested 6.5
         scsi: paravirtual
       vm_cdrom:
         type: "iso"
         iso_path: "ISOs/CentOS-6.5-x86_64-bin-DVD1.iso"
       esxi:
         datacenter: "{{ datacenter_name }}"
         hostname: "{{ esxi_hostname }}"
     register: newvm

# Now create a new connection to query facts from the new VM  
   - vsphere_guest:
       vcenter_hostname: "{{ vcenter_hostname_fqdn }}"
       username: "{{ username }}"
       password: "{{ password }}"
       guest: "{{ guestname }}"
       vmware_guest_facts: yes
     register: vmguest_facts

   - name: Display macaddress to use with 'cobbler system create'
     debug: msg="Mac address is "{{ hw_eth0.macaddress }}"
     register: macaddr_new

 - include: ansible2cobbler.yml

Now we can grab the new VM’s MAC address and add it into a new Cobbler system.

 

--- # ansible2cobbler.yml: Add new VM into Cobbler netinstall
- hosts: localhost
  connection: local
  gather_facts: no
  vars:
    profile_name: "CENTOS65-x86_64" 
    ip_address: "192.168.1.10"
    gateway_ip: "192.168.1.1"
    netmask_ip: "255.255.255.0"
    kickstart_file: "/var/lib/cobbler/kickstarts/kickstart.ks"
    name_servers: "192.168.1.15"
    ansible_hostname: vmtest1
    macaddress: "{{ hw_eth0.macaddress }}"
  tasks:
    - name: Check if system exists in Cobbler
      shell: "/usr/bin/cobbler system list | /usr/bin/grep {{ ansible_hostname }}"
      delegate_to: localhost
      ignore_errors: yes
      register: systemexist
      
    - name: Show system exist debug
      debug: msg={{ systemexist }}

    - name: Delete system in Cobbler
      command: "/usr/bin/cobbler system remove --name={{ ansible_hostname }}"
      when: systemexist
      notify: resync_cobbler
      ignore_errors: yes
      register: systemdelete

    - name: Create a system in Cobbler
      command: /usr/bin/cobbler system add --name={{ ansible_hostname }} \
--profile={{ profile_name }} --interface=eth0 --mac-address={{ macaddress }} \
--netmask={{ netmask_ip }} --ip-address={{ ip_address }} --gateway={{ gateway_ip }}\
 --netboot-enabled=1 --name-servers={{ name_servers }} --hostname={{ ansible_hostname }} \
--kickstart={{ kickstart_file }}
      ignore_errors: yes
      when: systemexist|failed
      notify: resync_cobbler
      register: systemcreate
      

  handlers:
    - name: resync_cobbler
      command: cobbler sync
Advertisements