Site icon Digital Thought Disruption

Deep Dive into Nutanix CLI and Bash Automation

Introduction

Nutanix’s CLI tools provide the same administrative power as the Prism GUI, but in a faster and more scriptable format. With Bash, infrastructure teams can streamline repetitive tasks, enforce policy compliance, and improve operational speed. This deep dive explores the primary CLI options from Nutanix, and shows how to build intelligent Bash scripts to drive cluster operations.


My Personal Repository on GitHub

Nutanix Repository on GitHub


Understanding the Nutanix CLI Stack

Nutanix offers multiple command-line interfaces:


Why Automate with Bash?

Bash brings control structures, string parsing, logic, and job scheduling to the otherwise manual CLI experience. This combination:


Diagram: Bash and Nutanix CLI Integration


Sample Automation: Logging Cluster Version and VM Inventory

#!/usr/bin/env bash

set -euo pipefail

log="/var/log/nutanix-cli-demo.log"
echo "[$(date)] Starting script" >> "$log"

echo "Cluster version:" >> "$log"
ncli cluster version >> "$log"

echo "----------------------------" >> "$log"
echo "VM Inventory:" >> "$log"
ncli vm list | awk 'NR > 1' >> "$log"

echo "[$(date)] Script complete" >> "$log"

Advanced Bash + Nutanix CLI Patterns

Loop Through All VMs and Check Status

#!/bin/bash

for vm in $(ncli vm list name | awk 'NR > 2 {print $1}'); do
state=$(ncli vm get name="$vm" | grep -i "Power State" | awk '{print $3}')
echo "$vm is currently $state"
done

Conditional Action: Restart a Failed VM

#!/bin/bash

vm="test-app01"
status=$(ncli vm get name="$vm" | grep -i "Power State" | awk '{print $3}')

if [[ "$status" != "on" ]]; then
echo "Starting $vm..."
ncli vm power-on name="$vm"
fi

Tips for Better CLI Scripting


Summary

Using Bash to automate Nutanix CLI commands can transform how you manage clusters. Whether it’s for health monitoring, capacity planning, or VM lifecycle management, CLI-driven workflows scale with your environment. Start small, build reusable functions, and document everything.

External Links:

Exit mobile version