-
-
Save dexterbt1/c3e78827add7c8db1c6410cbc955c83d to your computer and use it in GitHub Desktop.
Ansible dynamic inventory plugin for Docker Machine
This file contains 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 characters
#!/usr/bin/env python | |
""" | |
Example Usage: | |
$ ansible -i machine.py machinename -m ping | |
""" | |
import argparse | |
import subprocess | |
try: | |
import json | |
except ImportError: | |
import simplejson as json | |
def dm(*args): | |
return subprocess.check_output(["docker-machine"] + list(args)).strip() | |
def dminspect(fmt, mcn): | |
return dm("inspect", "-f", fmt, mcn) | |
def get_host_and_vars(m): | |
hosts = [dminspect("{{.Driver.IPAddress}}", m)] | |
ssh_vars = { | |
"ansible_ssh_user": dminspect("{{.Driver.SSHUser}}", m), | |
"ansible_ssh_port": dminspect("{{.Driver.SSHPort}}", m), | |
"ansible_ssh_private_key_file": "{}/{}".format(dminspect("{{.StorePath}}", m), "id_rsa") | |
} | |
data = {"hosts": hosts, "vars": ssh_vars} | |
return data | |
class DockerMachineInventory(object): | |
def __init__(self): | |
self.inventory = {} # Ansible Inventory | |
parser = argparse.ArgumentParser(description='Produce an Ansible Inventory file based on Docker Machine status') | |
parser.add_argument('--list', action='store_true', help='List all active Droplets as Ansible inventory (default: True)') | |
self.args = parser.parse_args() | |
machines = dm("ls", "-q").splitlines() | |
json_data = {m: get_host_and_vars(m) for m in machines} | |
print json.dumps(json_data) | |
DockerMachineInventory() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment