Create all VLANs from one cluster to a New Cluster
I'am on the train traveling from my home town to Stockhom here in sweden to attend Nutanix Cloud Day tomorrow. I got some spare time on the train, and figured i would use it to the best. Currently we're in the process of setting up our third nutanix cluster in our datacenters, we're trying to convrert some Dell 740xd nodes that where running Hyper-V and S2D (storage spaces direct).
And for the convenience of future migration between the existing clusters that we have and the new Dell cluster, we want all VLANs to be present on all our 3 clusters.
Mainly we use Prism Central to configure new vlans, but in this scenario we want to mirror the vlans from one cluster, to our soon freshly baked cluster :)
So i wondered, how can i script this to save time?
This is what i came up with:
- First, connect to a cvm in your "source cluster".
- Run the acli net.list command with the output of json-pretty with som grep and sed formatting. puting it to a file called network_list.txt
acli -o json-pretty net.list | grep -E '"id"|"name"' | sed 'N;s/\n/ /;s/ *"id": \([0-9]*\), *"name": "\(.*\)"/{\n "id": \1,\n "name": "\2"\n},/;s/},,/},/' > /home/nutanix/network_list.txt
- This should leave you with an output file called network_list.txt similar to this:
{
"id": 100,
"name": "VLAN 100"
},
{
"id": 200,
"name": "VLAN 100"
},
{
"id": 300,
"name": "VLAN 100"
},
{
"id": 400,
"name": "VLAN 100"
},
{
"id": 500,
"name": "VLAN 100"
},
- Next, we copy the file to our local linux machine (in my case we have a ssh bastion host used for operations like this). The post wont cover the copy process, but my favorite is scp :)
- Once we have the network_list.txt file on our local system we can start creating the acli net.create commands using a simple script with AWK.
See my example script below. Since we have swedish caracters in our VLAN names, i will use UTF-8 in the output file, change this to match your needs :)
#!/bin/bash
input_file="network_list.txt"
output_file="output.txt"
awk -F '[:,]' '/"id":/ {id=$2; gsub(/^[[:space:]]+|[[:space:]]+$/, "", id)} /"name":/ {gsub(/"/, "", $2); name=$2; gsub(/^[[:space:]]+|[[:space:]]+$/, "", name)} /^\{/{print "acli net.create name=\"" name "\" vlan=" id}' "$input_file" |
iconv -f UTF-8 -t ISO-8859-1 > "$output_file"
- Then we just run the script, i called the script 'script.sh' on our jump-box.
sh script.sh
- Now you should have a output file looking like this:
acli net.create name="VLAN 100" vlan=100
acli net.create name="VLAN 200" vlan=200
acli net.create name="VLAN 300" vlan=300
acli net.create name="VLAN 400" vlan=400
acli net.create name="VLAN 500" vlan=500
- Now connect with ssh to a CVM in the destination cluster and copy paste all the commands in the file. You can copy paste them all at the same time and acli will handle it for you, one at the time.
- Aaaaand you're done!
Thanks for reading. And use this method with caution. Thanks to Open AI and ChatGPT for helping me with the correct AWK statements in the script :)
Until next time. Have a great one!