Last active
October 16, 2021 13:36
-
-
Save scottharwell/bd2302ae66498276e36fa23405f23ed6 to your computer and use it in GitHub Desktop.
Revisions
-
scottharwell revised this gist
Oct 16, 2021 . No changes.There are no files selected for viewing
-
scottharwell created this gist
Oct 16, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,41 @@ #!/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))