Dynamic VM Network Assignment Using Ansible

Introduction

Virtual machine networking in Nutanix AHV is defined by subnet assignments, bridges (e.g., br0), and VLAN tags. Ansible lets you assign VM NICs dynamically during or after deployment, ensuring proper segmentation for dev, test, and prod workloads. This guide provides a flexible playbook for assigning networks to Nutanix VMs.


My Personal Repository on GitHub

Nutanix Repository on GitHub


Diagram: Dynamic Network Assignment Flow


Use Case

  • Assign multiple VMs to different subnets
  • Attach NICs post-deployment
  • Automate network provisioning during CI/CD VM creation

Sample Variables File: group_vars/all.yml

vm_network_map:
web01: vlan10
db01: vlan20
monitor01: vlan30

Sample Playbook: assign_networks.yml

- name: Assign networks to Nutanix VMs
hosts: localhost
gather_facts: false
collections:
- nutanix.ncp
vars_files:
- nutanix_credentials.yml
tasks:

- name: Attach NIC to VM
loop: "{{ vm_network_map.keys() | list }}"
loop_control:
loop_var: vm_name
nutanix.ncp.vms:
name: "{{ vm_name }}"
state: present
cluster_name: "prod-cluster"
vm_nics:
- subnet_name: "{{ vm_network_map[vm_name] }}"

Optional: Remove and Replace NICs

To replace NICs, use state: absent with the current list, then re-attach with state: present.


Run the Playbook

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

Use with a Larger Deployment Flow

This playbook can be inserted into VM deployment roles or CI/CD pipelines where subnet selection is dynamic based on group, app role, or site.


Summary

Network assignment on Nutanix AHV is flexible and declarative with Ansible. This playbook gives you the ability to assign NICs programmatically, streamlining VM provisioning across dev, QA, and prod environments.

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