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:

  • ncli – Prism Element CLI for cluster configuration, VMs, storage, health checks
  • acli – AHV management tool focused on virtual machines
  • ecli (less common) – For deeper host-level operations
  • genesis – System-level diagnostic CLI on CVMs

Why Automate with Bash?

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

  • Speeds up large-scale tasks (VM deployments, cluster expansions)
  • Improves repeatability and documentation
  • Enables automation via cron or CI/CD pipelines
  • Supports monitoring integrations (email, Slack, webhook alerts)

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

  • Use set -euo pipefail to avoid partial script runs
  • Redirect all output and errors to logs (>> logfile 2>&1)
  • Validate CLI version compatibility with Nutanix documentation
  • Test scripts in dev/test environments with dummy VMs
  • Store reusable commands in functions or external .sh libraries

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:

Leave a Reply

Discover more from Digital Thought Disruption

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

Continue reading