Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottharwell/bd2302ae66498276e36fa23405f23ed6 to your computer and use it in GitHub Desktop.
Save scottharwell/bd2302ae66498276e36fa23405f23ed6 to your computer and use it in GitHub Desktop.
Generates an Ansible dynamic inventory of running virtual machines in Parallels.
#!/usr/bin/env python3
import json
import os
import re
def get_running_instances():
stream = os.popen('prlctl list -o ip --no-header')
output = stream.read()
replaced = re.sub(r"\s.*\n", "\n", output)
return replaced
# Constructs to the inventory JSON object
def create_inventory(instances):
host_key = "vagrant"
inventory = {
host_key: {
"hosts": [],
"vars": {
"ansible_ssh_user": "scott",
"ansible_ssh_private_key_file": "~/.ssh/id_ed25519"
}
},
"_meta": {
"hostvars": {}
}
}
for ip_addr in instances.splitlines():
if len(ip_addr) > 0:
inventory[host_key]["hosts"].append(ip_addr)
inventory["_meta"]["hostvars"][ip_addr] = {}
return inventory
instances = get_running_instances()
inventory = create_inventory(instances)
print(json.dumps(inventory))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment