Skip to content

Commit f2d22b1

Browse files
committedMay 9, 2016
Big Bang
0 parents  commit f2d22b1

File tree

5 files changed

+181
-0
lines changed

5 files changed

+181
-0
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

‎LICENSE

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
(The MIT License)
2+
3+
Copyright (c) 2009-2014 Oscar Brito <aetheon@gmail.com>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
'Software'), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

‎README.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
[![Divhide](http://blog.divhide.com/assets/images/divhide_128px.png)](http://divhide.com/)
3+
4+
# README
5+
6+
This contains an example of an ansible playbook that starts up a docker container using
7+
docker-machine and uses an ansible docker connection to do the machine setup.
8+
9+
```
10+
11+
# initialize docker-machine
12+
docker-machine start local
13+
eval "$(docker-machine env local)"
14+
15+
# run the playbook using the dynamic inventory
16+
ansible-playbook -i docker-machine.py playbook.yml
17+
18+
# check docker running containers
19+
docker ps
20+
21+
# connect to running instance
22+
docker exec -it XXXXXXX bash
23+
24+
```
25+
26+
# Author
27+
28+
Oscar Brito

‎docker-machine.py

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

‎playbook.yml

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
3+
# run an empty centos 7 container
4+
- hosts: docker-host
5+
6+
vars:
7+
# workaround error "docker-py is needed for this module"
8+
ansible_python_interpreter: "/usr/bin/env python"
9+
10+
tasks:
11+
12+
- name: build docker image
13+
docker:
14+
image: "centos:7"
15+
name: "divhide-test"
16+
state: "reloaded"
17+
docker_url: "{{ docker_host_url }}"
18+
use_tls: "encrypt"
19+
command: sleep 5m
20+
environment:
21+
DOCKER_CERT_PATH: "{{ docker_host_cert_path }}"
22+
# register the running container
23+
register: docker_result
24+
25+
- name: add new docker connection {{ docker_result.ansible_facts.docker_containers[0].Config.Hostname }} to docker-container inventory group
26+
add_host:
27+
name: "{{ docker_result.ansible_facts.docker_containers[0].Config.Hostname }}"
28+
groups: "docker-container"
29+
ansible_connection: docker
30+
ansible_ssh_user: "root"
31+
ansible_become_user: "root"
32+
ansible_become: "yes"
33+
34+
35+
# setup the new machine using ansible instead of docker
36+
- hosts: docker-container
37+
tasks:
38+
39+
- name: install vi
40+
yum:
41+
name: vi
42+
state: present

0 commit comments

Comments
 (0)
Please sign in to comment.