Getting all your VMs with Affinity rules
So! Today i wanted to list all the VMs in our cluster that had any host affinity rules. So, just for future reference here is a oneliner that you can run from any PC CVM
First we take a look at the nuclei command vm_host_affinity_legacy_policy.list
It gives a JSON output like this
"Total Entities : 27"
"Length : 27"
"Offset : 0"
"Entities :"
{
"message": {
"entities": [
{
"config": {
"host_reference_list": [
{
"kind": "host",
"name": "AVH-HOST-A",
"uuid": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx"
},
{
"kind": "host",
"name": "AVH-HOST-B",
"uuid": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx"
},
{
"kind": "host",
"name": "AVH-HOST-C",
"uuid": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx"
},
{
"kind": "host",
"name": "AVH-HOST-D",
"uuid": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx"
}
],
"vm_reference": {
"kind": "vm",
"name": "VirtualMachine01",
"uuid": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx"
}
},
"metadata": {
"uuid": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx"
}
},
As you can se, the response is in JSON format. And to read trough all the entities in this format can be challenging. So we ned to format the output. We're only intressted in the message.entities.config.vm_reference.name value, and display that value for each entry in the list.
Below Oneliner will provide us with the output that we're looking for.
nuclei vm_host_affinity_legacy_policy.list | grep -A 5 '"vm_reference":' | grep '"name":' | awk -F'"' '{print $4}'
The response will look something like this:
I hope this can help someone out there :) thanks for reading!