-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdocker-machine.py
executable file
·88 lines (65 loc) · 1.93 KB
/
docker-machine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python
"""
Ansible dynamic inventory that gets the docker-machine host and it's
variables into the inventory to the 'docker-host' group.
Example Usage:
> eval "$(docker-machine env MACHINE_NAME)"
$ ansible-playbook playbook.yml -i docker-machine.py
Debug:
python docker-machine.py
"""
import os
import argparse
import subprocess
try:
import json
except ImportError:
import simplejson as json
"""
Gets information about a docker machine.
docker-machine host
"""
def getDockerMachineInfo(machineName, format):
return subprocess.check_output([
"docker-machine",
"inspect",
"-f",
format,
machineName ]).strip()
"""
Gets all available docker machines.
Returning the list of available docker machines
"""
def getAllDockerMachines(machineName, format):
return subprocess.check_output([
"docker-machine",
"ls",
"-q" ]).strip()
"""
Get the docker machine inventory group.
Returns the docker host group
"""
def getAnsibleDockerInventoryGroup(dmName):
return {
"hosts": ["localhost"],
"vars": {
# at the moment docker-machine is always using this port
"docker_host_url": "tcp://{}:2376".format(
getDockerMachineInfo(dmName, "{{.Driver.IPAddress}}")),
"docker_host_cert_path": getDockerMachineInfo(dmName, "{{.HostOptions.AuthOptions.StorePath}}"),
"ansible_connection": "local"
}
}
# get the current docker machine name
docker_machine_name = os.environ['DOCKER_MACHINE_NAME']
if docker_machine_name is None:
raise Exception('Missing DOCKER_MACHINE_NAME environment variable')
# add the docker-machine to the inventory
json_data = {
"docker-host": getAnsibleDockerInventoryGroup(docker_machine_name),
"local": {
"hosts": ["localhost"],
"vars": { "ansible_connection": "local" }
}
}
print json.dumps(json_data, indent=4)