Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save scottharwell/bd2302ae66498276e36fa23405f23ed6 to your computer and use it in GitHub Desktop.
Save scottharwell/bd2302ae66498276e36fa23405f23ed6 to your computer and use it in GitHub Desktop.

Revisions

  1. scottharwell revised this gist Oct 16, 2021. No changes.
  2. scottharwell created this gist Oct 16, 2021.
    41 changes: 41 additions & 0 deletions Parallels Running Instance Dynamic Inventory.py
    Original 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))