Skip to content

Instantly share code, notes, and snippets.

@chgeuer
Last active February 6, 2019 16:30
Show Gist options
  • Save chgeuer/b366a80d682ebb70cb14d222594d6715 to your computer and use it in GitHub Desktop.
Save chgeuer/b366a80d682ebb70cb14d222594d6715 to your computer and use it in GitHub Desktop.

BOSH CF repro - Assign new IP to booting VM

  • Create a NIC with start_ip
  • Start VM with that NIC
  • While VM is in Creating state, change private (primary) IP to alternative_ip

Base setup

export rg_name=switchip
export vm_name=somevm
export vnet_name=switchipvnet
export subnet_name=subnet
export location=westeurope

export start_ip=10.0.0.10
export alternative_ip=10.0.0.200

az group create --name switchip --location westeurope

az network vnet create \
    --resource-group "${rg_name}" \
    --name "${vnet_name}" \
    --subnet-name "${subnet_name}" \
    --address-prefix 10.0.0.0/16 \
    --subnet-prefix 10.0.0.0/24

Synchronously create a NIC

export nic_id_primary=$(az network nic create \
    --resource-group "${rg_name}" \
    --name "nic_${vm_name}" \
    --vnet-name "${vnet_name}" --subnet "${subnet_name}" \
    --location "${location}" \
    --private-ip-address "${start_ip}" \
    | jq -r ".NewNIC.id")

Async trigger VM creation (--no-wait)

az vm create \
    --resource-group "${rg_name}" --name "${vm_name}" \
    --location "${location}" \
    --image SLES --size Standard_DS1_v2 \
    --admin-username chgeuer --ssh-key-value "$(cat /mnt/c/Users/chgeuer/.ssh/dcos.openssh.public)" \
    --no-wait \
    --nics "${nic_id_primary}"

Check provisioning state

az vm show \
    --resource-group "${rg_name}" --name "${vm_name}" \
    | jq -r ".provisioningState"

Resulted in Creating

Tweak NIC IP

az network nic update \
    --ids "${nic_id_primary}" \
    --set ipConfigurations[0].privateIpAddress="${alternative_ip}"

#
# Check if original IP id available again
#
az network vnet check-ip-address \
    --resource-group "${rg_name}" \
    --name "${vnet_name}" \
    --ip-address "${start_ip}"

Check provisioning state

az vm show \
    --resource-group "${rg_name}" --name "${vm_name}" \
    | jq -r ".provisioningState"

Resulted in Creating

Show private IP

az network nic show --ids "$(az vm show \
    --resource-group "${rg_name}" --name "${vm_name}" \
    | jq -r ".networkProfile.networkInterfaces[].id")" \
    | jq -r ".ipConfigurations[].privateIpAddress"

Resulted in 10.0.0.31

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment