Skip to content

Instantly share code, notes, and snippets.

@malterb
Last active February 3, 2017 18:45
Show Gist options
  • Save malterb/b26c5b57540ea2b20173e3a7dc0a5e9d to your computer and use it in GitHub Desktop.
Save malterb/b26c5b57540ea2b20173e3a7dc0a5e9d to your computer and use it in GitHub Desktop.

With azure cli 2.0:

How to get all private IPs of an azure virtual machine scale set (vmss)

# Date: Feb-03-2017
SCALE_SET=""
RG=""
az vmss nic list --resource-group $RG --vmss-name $SCALE_SET | jq -r '.[] | .ipConfigurations[] | .privateIpAddress'

Using the vmss IPs as a dynamic ansible inventory host file as group "agents"

#!/usr/bin/env node
const exec = require('child_process').exec;

const rg = '';
const vmss = '';
const group = 'agents'

// _meta is required as a speedup. otherwise it will restart the script every time variables are needed. Slowdown ~30x
const result = {'_meta':{'hostvars':{}}};

exec(`az vmss nic list --resource-group ${rg} --vmss-name ${vmss}`, function(err, stdout, stderr) {
  const json = JSON.parse(stdout);
  const hosts = json.map((e) => { return e.ipConfigurations[0].privateIpAddress })
  result[group] = {'hosts': hosts}
  console.log(JSON.stringify(result));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment