AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Deploy a VMware vSphere Virtual Machine from a Template - Ansible module vmware_guest

By Luca Berton · Published 2024-01-01 · Category: troubleshooting

How to automate the deployment of a virtual machine guest from “mytemplate” template using Ansible Playbook and vmware_guest module.

Deploy a VMware vSphere Virtual Machine from a Template - Ansible module vmware_guest

How to Deploy a VMware vSphere Virtual Machine from a Template with Ansible?

I'm going to show you a live Playbook and some simple Ansible code. I'm Luca Berton and welcome to today's episode of Ansible Pilot

See also: How to Add a Disk to a VMware VM Using Ansible Playbook

Ansible Deploy a VMware vSphere Virtual Machine from a Template

community.vmware.vmware_guest • Manages virtual machines in vCenter

Let's talk about the Ansible module vmware_guest. The full name is community.vmware.vmware_guest, which means that is part of the collection of modules to interact with VMware, community-supported. It's a module pretty stable and out for years. It manages virtual machines in vCenter.

Parameters

The module vmware_guest has a very long list of parameters to customize all your needs to create a VMware vSphere Virtual Machine. Please refer to the manual for the full list.

See also: Configure Ansible Dynamic Inventory for VMware in Simple Steps

Links

community.vmware.vmware_guest

## Playbook How to Deploy a VMware vSphere Virtual Machine from a Template with Ansible. I'm going to show you how to deploy a Virtual Machine named "myvm" from a template "mytemplate" without any customization.

code

• vm_deploy_template.yml
---
- name: deploy vm from template Playbook
  hosts: localhost
  become: false
  gather_facts: false
  collections:
    - community.vmware
  pre_tasks:
    - include_vars: vars.yml
tasks:
    - name: create folder
      vcenter_folder:
        hostname: "{{ vcenter_hostname }}"
        username: "{{ vcenter_username }}"
        password: "{{ vcenter_password }}"
        validate_certs: "{{ vcenter_validate_certs }}"
        datacenter_name: "{{ vcenter_datacenter }}"
        folder_name: "{{ vcenter_destination_folder }}"
        folder_type: vm
        state: present
    - name: clone VM
      vmware_guest:
        hostname: "{{ vcenter_hostname }}"
        username: "{{ vcenter_username }}"
        password: "{{ vcenter_password }}"
        validate_certs: "{{ vcenter_validate_certs }}"
        datacenter: "{{ vcenter_datacenter }}"
        cluster: "{{ vcenter_cluster }}"
        name: "{{ vm_name }}"
        folder: "{{ vcenter_destination_folder }}"
        template: "{{ vm_template }}"
• vars.yml
---
vcenter_hostname: "vmware.example.com"
vcenter_datacenter: "vmwaredatacenter"
vcenter_validate_certs: false
vcenter_username: "username@vsphere.local"
vcenter_password: "MySecretPassword123"
vm_name: "myvm"
vcenter_destination_folder: "myvm"
vm_state: "poweroff"
vm_template: "mytemplate"
• inventory
localhost

execution

$ ansible-playbook -i inventory vm_deploy_template.yml
PLAY [deploy vm from template Playbook] ***************************************************************
TASK [include_vars] *******************************************************************************
ok: [localhost]
TASK [create folder] ******************************************************************************
changed: [localhost]
TASK [clone VM] ***********************************************************************************
changed: [localhost]
PLAY RECAP ****************************************************************************************
localhost                  : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

idempotency

$ ansible-playbook -i inventory vm_deploy_template.yml
PLAY [deploy vm from template Playbook] ***************************************************************
TASK [include_vars] *******************************************************************************
ok: [localhost]
TASK [create folder] ******************************************************************************
ok: [localhost]
TASK [clone VM] ***********************************************************************************
ok: [localhost]
PLAY RECAP ****************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

after execution

ansible_module_vmware_guest after execution

code with ❤️ in GitHub

Conclusion

Now you know how to Deploy a VMware vSphere Virtual Machine from a Template with Ansible.

See also: Configuring Ansible for VMware: Complete Setup Guide & Playbook

Related Articles

Jinja2 filters in Ansible templatesprivilege escalation with Ansible becomebuilding an Ansible inventory

Category: troubleshooting

Watch the video: Deploy a VMware vSphere Virtual Machine from a Template - Ansible module vmware_guest — Video Tutorial

Browse all Ansible tutorials · AnsiblePilot Home