I am relatively new to bash and I need to create 127 VNET security rules for an Azure MySQL server instance using the Azure CLI, which needs a rule name and associated subnet ID. The rule name is the subnet name. I can read the subnet name and ID into arrays and can see the arrays populated with
mapfile -t vnetRULEname < <(az network vnet subnet list -g resourcegroup --vnet-name vnet --query "[].{name:name}" -o table)
mapfile -t vnetRULEid < <(az network vnet subnet list -g resourcegroup
--vnet-name vnet --query "[].{objectID:id}" -o table)
I then want to run the following command so it creates the 127 rules using each name and ID in the arrays to create the rules.
az mysql server vnet-rule create -n <rule name from vnetRULEname> -g resourcegroup -s servername --subnet <subnet ID from vnetRULEid>
Would it be better to read both the subnet name and ID values into the same array?
Whats the best way to do this in a bash script and how do i tell it to ignore the column headers called Name and ID and the subnet called GatewaySubnet?
Sample of output (subnet names)
Name
-------------
GatewaySubnet
app-host-001
app-host-002
app-host-003
app-host-004
app-host-005
Sample of output (subnet ID's)
ObjectID
-----------------------------------------------------------------------------
/subscriptions/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/resourceGroups/resourcegroup/providers/Microsoft.Network/virtualNetworks/vnet/subnets/GatewaySubnet
/subscriptions/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/resourceGroups/resourcegroup/providers/Microsoft.Network/virtualNetworks/vnet/subnets/app-host-001
/subscriptions/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/resourceGroups/resourcegroup/providers/Microsoft.Network/virtualNetworks/vnet/subnets/app-host-002
/subscriptions/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/resourceGroups/resourcegroup/providers/Microsoft.Network/virtualNetworks/vnet/subnets/app-host-003
/subscriptions/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/resourceGroups/resourcegroup/providers/Microsoft.Network/virtualNetworks/vnet/subnets/app-host-004
/subscriptions/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/resourceGroups/resourcegroup/providers/Microsoft.Network/virtualNetworks/vnet/subnets/app-host-005
Running
#!/bin/bash
mapfile -t vnetRULEname < <(az network vnet subnet list -g resourcegroup --vnet-name vnet --query "[].{name:name}" -o table)
mapfile -t vnetRULEid < <(az network vnet subnet list -g resourcegroup --vnet-name vnet --query "[].{objectID:id}" -o table)
echo "These are vnetRULEname: ${vnetRULEname[@]}"
echo "These are vnetRULEids : ${vnetRULEid[@]}"
Displays the contents of both arrays on screen as i would expect to see. But if i run
mapfile -t vnetRULEname < <(az network vnet subnet list -g resourcegroup --vnet-name vnet --query "[].{name:name}" -o table)
mapfile -t vnetRULEid < <(az network vnet subnet list -g resourcegroup --vnet-name vnet --query "[].{objectID:id}" -o table)
#echo "These are vnetRULEname: ${vnetRULEname[@]}"
#echo "These are vnetRULEids : ${vnetRULEid[@]}"
sizeofarrays=${#arr[@]}
for (( i=0 ; i < sizeofarrays ; i++ ))
do
az mysql server vnet-rule create --name "${vnetRULEname[$i]}" --resource-group resourcegroup --server server --subnet "${vnetRULEid[$i]}"
done
echo ${arr[@]} ## print all the array
echo ${#arr[@]} ## print its size
I get
0
0
Many thanks in advance,
Andrew
Name ------------- GatewaySubnet app-host-001 app-host-002 app-host-003 app-host-004 – Naughty-Old-Jarvis Jul 31 '18 at 14:29