Site icon Digital Thought Disruption

Nutanix CLI and Bash Scripting Best Practices

Introduction

Bash and Nutanix CLI (ncli, acli) scripting is a powerful combo, but power demands responsibility. From production-level safety to logging and parameter handling, this post outlines essential best practices to help you create clean, robust, and maintainable automation scripts for Nutanix environments.


My Personal Repository on GitHub

Nutanix Repository on GitHub


Diagram: Safe Scripting Layers


Always Use Strict Mode

#!/usr/bin/env bash
set -euo pipefail

Validate Inputs

Avoid unexpected behavior from missing or malformed inputs.

if [[ -z "${1:-}" ]]; then
echo "Usage: $0 <vm-name>"
exit 1
fi

Secure Credential Handling


Log Everything

Centralize logs per script for traceability:

log="/var/log/nutanix_$(basename "$0" .sh).log"
echo "[$(date)] Starting script..." >> "$log"

Handle ncli/acli Errors Gracefully

Wrap critical commands:

if ! ncli cluster get-summary >> "$log" 2>&1; then
echo "Error fetching cluster summary" >> "$log"
exit 1
fi

Organize Scripts into Functions

Structure for reuse and clarity:

create_vm() {
local vm="$1"
acli vm.create "$vm" num_vcpus=2 memory=2048
}

Clean Up Temporary Files

Always remove scratch data or temp credentials:

trap 'rm -f /tmp/tmpfile.$$' EXIT

Notify on Errors

Email, Slack, or webhook alerts:

mailx -s "Nutanix Script Failed" admin@example.com < "$log"

Test in Lab First

Always validate scripts against a test cluster before running in production.


Schedule Sanely

Stagger cron jobs to avoid contention:

0 4 * * * /usr/local/bin/nutanix_vm_backup.sh
15 4 * * * /usr/local/bin/nutanix_vm_audit.sh

Summary

Following these Bash and Nutanix CLI best practices ensures your scripts are safe, maintainable, and enterprise-ready. Build automation like you’d build infrastructure: with reliability, observability, and recoverability in mind.

External Documentation:

Exit mobile version