Ansible Pilot

Start a VMware vSphere Virtual Machine - Ansible module vmware_guest_powerstate

How to automate the change of power state from Powered Off to Powered On of the virtual machine guest “myvm” using Ansible Playbook and vmware_guest_powerstate module.

May 17, 2022
Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons

How to Start a VMware vSphere Virtual Machine with Ansible?

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

Ansible Start a VMware vSphere Virtual Machine

Let’s talk about the Ansible module vmware_guest_powerstate. The full name is community.vmware.vmware_guest_powerstate, which means that is part of the collection of modules to interact with VMware, community-supported. Manages power states of virtual machines in vCenter.

Parameters

The following parameters are useful in order to Start a VMware vSphere Virtual Machine using the module vmware_guest_powerstate. First of all, we need to establish the connection with VMware vSphere or VMware vCenter using a plethora of self-explicative parameters: hostname, username, password, datacenter, and validate_certs. Once the connection is successfully established you could specify the desired power state, in this case, “powered-on”. You could also force the power state change using the force parameter, default disabled. You could also specify the reply to some answer that could arise while waiting for the task to complete. Some common uses are to allow a cdrom to be changed even if locked or to answer the question as to whether a VM was copied or moved.

The Best Resources For Ansible

Certifications

Video Course

Printed Book

eBooks

demo

How to Start a VMware vSphere Virtual Machine with Ansible. I’m going to show you how to start the Virtual Machine named “myvm” from the power state “Powered Off” to the power state “Powered On” using Ansible Playbook.

code

---
- name: start vm demo
  hosts: localhost
  become: false
  gather_facts: false
  collections:
    - community.vmware
  pre_tasks:
    - include_vars: vars.yml
  tasks:
    - name: power on
      vmware_guest_powerstate:
        hostname: "{{ vcenter_hostname }}"
        username: "{{ vcenter_username }}"
        password: "{{ vcenter_password }}"
        name: "{{ vm_name }}"
        validate_certs: "{{ vcenter_validate_certs }}"
        state: powered-on
---
vcenter_hostname: "vmware.example.com"
vcenter_datacenter: "vmwaredatacenter"
vcenter_validate_certs: false
vcenter_username: "[email protected]"
vcenter_password: "MySecretPassword123"
vm_name: "myvm"
localhost

execution

$ ansible-playbook vm_start.yml 
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit
localhost does not match 'all'
PLAY [start vm demo] ******************************************************************************
TASK [include_vars] *******************************************************************************
ok: [localhost]
TASK [power on] ***********************************************************************************
changed: [localhost]
PLAY RECAP ****************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

idempotency

$ ansible-playbook vm_start.yml 
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit
localhost does not match 'all'
PLAY [start vm demo] ******************************************************************************
TASK [include_vars] *******************************************************************************
ok: [localhost]
TASK [power on] ***********************************************************************************
ok: [localhost]
PLAY RECAP ****************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

before execution

ansible_module_vmware_guest_powerstate_start before execution

after execution

ansible_module_vmware_guest_powerstate_start after execution

code with ❤️ in GitHub

Recap

Now you know how to Start a VMware vSphere Virtual Machine with Ansible. Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons
Follow me

Subscribe not to miss any new releases