Ansible Deploy a virtual machine from a template
2 min read

Ansible Deploy a virtual machine from a template

Ansible Deploy a virtual machine from a template
Photo by Fotis Fotopoulos / Unsplash

Requirement

  • CentOS 7
  • vCenter Server > 6.5 with at least one ESXi server
  • Ansible 2.5 or later must be installed
  • Installing the latest Pyvmomi via pip is recommended

1) Create virtual machine for Ansible

2) install python3-pip

yum update
yum install python3-pip

3) install PyVmomi and Ansible

pip3 install pyvmomi
yum -y install epel-release
yum -y update
yum -y install ansible

4) Ansible Playbook

make dir for save playbook

mkdir -p /etc/ansible/playbook
cd /etc/ansible/playbook

create variable

vim vcenter-vars.yml


# Global Settings
vcenter_server: ip-vcenter
vcenter_user: user-vcenter
vcenter_pass: pass-vcenter
servers:
- clone-template

create script ansible

vim clone.yml

---
- name: Create a VM from a template
  hosts: localhost
  gather_facts: no
  vars_files:
  - vcenter_vars.yml
  vars:
  ansible_python_interpreter: "/usr/bin/env python3"
  tasks:
  - name: Clone VM from template
    vmware_guest:
      hostname: "{{ vcenter_server }}"
      username: "{{ vcenter_user }}"
      password: "{{ vcenter_pass }}"
      validate_certs: False
      datacenter: Datacenter Name
      esxi_hostname: ip-esxi
      resource_pool: resource-pool Name
      template: template-os
      folder: archive folder vm
      name: "{{ item }}"
      disk:
      - size_gb: 50
        type: thin
        datastore: Datastore Name
      hardware:
        memory_mb: 4096
        num_cpus: 2
      networks:
      - name: name network vm
      state: poweredon
      wait_for_ip_address: yes
    with_items: "{{ servers }}"

ทดสอบ clone โดยใช้คำสั่ง

ansible-playbook clone.yml

Check spacebar if error