Managing VM Power State with Ansible

Introduction

Whether you’re scheduling routine maintenance, automating DR procedures, or managing lab environments, the ability to control VM power state is essential. With Ansible, you can automate VM power operations, start, stop, reboot, and validate, all from a single playbook.


My Personal Repository on GitHub

Nutanix Repository on GitHub


Diagram: VM Power Workflow


Use Case

  • Gracefully power down VMs for patching
  • Restart development environments daily
  • Stop unused test VMs to save resources

Sample Variables: group_vars/all.yml

vm_power_map:
web01: poweron
db01: poweroff
test01: restart

Playbook: vm_power_control.yml

- name: Control Nutanix VM power states
hosts: localhost
gather_facts: false
collections:
- nutanix.ncp
vars_files:
- nutanix_credentials.yml
- group_vars/all.yml
tasks:

- name: Control power state of VMs
loop: "{{ vm_power_map | dict2items }}"
loop_control:
label: "{{ item.key }}"
nutanix.ncp.vms:
name: "{{ item.key }}"
state: present
power_state: "{{ item.value }}"
cluster_name: "prod-cluster"

Supported Power States

  • poweron
  • poweroff
  • restart
  • pause (in some builds)

Run the Playbook

ansible-playbook vm_power_control.yml --ask-vault-pass -i inventory.yml

Optional Enhancements

  • Add tags to control groupings (e.g., stop all “test” VMs)
  • Validate state post-execution with register
  • Integrate with a schedule or CI job

Summary

Controlling VM power state with Ansible gives your team more reliable workflows, improved energy efficiency, and the ability to automate daily environment resets or DR responses. Use this playbook to enforce consistency across dev, test, and prod AHV clusters.

External Documentation:

Leave a Reply

Discover more from Digital Thought Disruption

Subscribe now to keep reading and get access to the full archive.

Continue reading