Stop a VMware vSphere Virtual Machine - Ansible module vmware_guest_powerstate — Video Tutorial
How to automate the gracefully use guest shutdown and forcefully power off to change the power state from Powered On to Powered Off of the virtual machine guest “myvm” using Ansible Playbook and vmware_guest_powerstate module.
Watch Video
Watch "Stop a VMware vSphere Virtual Machine - Ansible module vmware_guest_powerstate" on YouTube
What You'll Learn
- How to Stop a VMware vSphere Virtual Machine with Ansible?
- Ansible Stop a VMware vSphere Virtual Machine
- Parameters
- Links
- code
- execution
- idempotency
- before execution
- after execution
- Conclusion
Full Tutorial Content
How to Stop a VMware vSphere Virtual Machine 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.
Ansible Stop a VMware vSphere Virtual Machine
- `community.vmware.vmware_guest_powerstate`
- Manages power states of virtual machines in vCenter
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.
It manages power states of virtual machines in vCenter.
Parameters
- hostname string / username string / password string / datacenter string / validate_certs boolean - connection details
- state string - present / powered-off / powered-on / reboot-guest / restarted / shutdown-guest / suspended
- force boolean - no/yes
- answer string - A list of questions to answer, should one or more arise while waiting for the task to be complete.
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 "shutdown-guest" to gracefully ask the guest operating system to shutdown or "powered-off" to turn off the virtual machine guest.
You could also force the power state to change using the `force` parameter, default disabled.
Links
- [community.vmware.vmware_guest_powerstate](https://docs.ansible.com/ansible/latest/collections/community/vmware/vmware_guest_powerstate_module.html)
## Playbook
How to Stop a VMware vSphere Virtual Machine with Ansible.
I'm going to show you how to Stop the Virtual Machine named "myvm" from the power state "Powered On" to the power state "Powered Off" using Ansible Playbook. I'm going to try first the "shutdown-guest" operation to gracefully ask the guest operating system to shut down than the "powered-off" to forcefully turn off the virtual machine guest.
code
- vm_stop.yml
```yaml
---
- name: stop vm Playbook
hosts: localhost
become: false
gather_facts: false
collections:
- community.vmware
pre_tasks:
- include_vars: vars.yml
tasks:
- name: guest shutdown
vmware_guest_powerstate:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
validate_certs: "{{ vcenter_validate_certs }}"
name: "{{ vm_name }}"
state: shutdown-guest
state_change_timeout: 120
register: shutdown
ignore_errors: true
- name: poweroff
vmware_guest_powerstate:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{
About This Tutorial
- Author: Luca Berton
- Difficulty: Beginner
- Read time: 5 min
- Category: troubleshooting
Read the full written article: Stop a VMware vSphere Virtual Machine - Ansible module vmware_guest_powerstate
Topics Covered
Related Video Tutorials
- How to Add a Disk to a VMware VM Using Ansible Playbook — Learn how to add a disk to a VMware VM using an Ansible playbook. This guide includes the YAML configuration, variables, and execution steps for easy automation.
- Configure Ansible Dynamic Inventory for VMware in Simple Steps — Discover how to configure Ansible Dynamic Inventory for VMware to automate and manage virtual machines efficiently. Step-by-step guide with Playbook included.
- Take a VMware Virtual Machine Snapshot - Ansible module vmware_guest_snapshot — How to automate taking snapshots named “Ansible Managed Snapshot” to a VMware Virtual Machine “myvm” Ansible Playbook and vmware_guest_snapshot module.
- 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.
- Configuring Ansible for VMware: Complete Setup Guide & Playbook — Learn how to set up Ansible for VMware with pyVmomi and the community.vmware collection. Follow our guide to configure and run your first VMware playbook.
- Configure a Python Virtual Environment for Ansible VMware - ansible collection community.vmware — Learn how to set up a Python Virtual Environment for Ansible VMware, ensuring seamless integration and management of your VMware infrastructure with Ansible community.vmware collection.