2014-04-01 20:54:37 +02:00
|
|
|
#!/usr/bin/python
|
2014-01-22 13:04:19 +01:00
|
|
|
#
|
|
|
|
|
|
|
|
# (c) 2014, Pavel Antonov <antonov@adwz.ru>
|
|
|
|
#
|
2014-02-11 17:10:47 +01:00
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
2014-01-22 13:04:19 +01:00
|
|
|
# This module is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This software is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this software. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
2014-02-11 17:10:47 +01:00
|
|
|
module: docker_image
|
2015-06-15 20:41:22 +02:00
|
|
|
author: "Pavel Antonov (@softzilla)"
|
2014-02-11 17:10:47 +01:00
|
|
|
version_added: "1.5"
|
|
|
|
short_description: manage docker images
|
2014-01-22 13:04:19 +01:00
|
|
|
description:
|
2014-02-11 17:10:47 +01:00
|
|
|
- Create, check and remove docker images
|
2014-01-22 13:04:19 +01:00
|
|
|
options:
|
2014-02-11 17:10:47 +01:00
|
|
|
path:
|
2014-01-22 13:04:19 +01:00
|
|
|
description:
|
2014-02-11 17:10:47 +01:00
|
|
|
- Path to directory with Dockerfile
|
2014-01-22 13:04:19 +01:00
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
aliases: []
|
2015-05-18 09:58:31 +02:00
|
|
|
dockerfile:
|
|
|
|
description:
|
|
|
|
- Dockerfile to use
|
|
|
|
required: false
|
|
|
|
default: Dockerfile
|
|
|
|
version_added: "2.0"
|
2014-02-11 17:10:47 +01:00
|
|
|
name:
|
2014-01-22 13:04:19 +01:00
|
|
|
description:
|
2014-02-11 17:10:47 +01:00
|
|
|
- Image name to work with
|
|
|
|
required: true
|
2014-01-22 13:04:19 +01:00
|
|
|
default: null
|
|
|
|
aliases: []
|
2014-02-11 17:10:47 +01:00
|
|
|
tag:
|
2014-01-22 13:04:19 +01:00
|
|
|
description:
|
2014-02-11 17:10:47 +01:00
|
|
|
- Image tag to work with
|
2014-01-22 13:04:19 +01:00
|
|
|
required: false
|
2014-05-09 04:04:55 +02:00
|
|
|
default: "latest"
|
2014-01-22 13:04:19 +01:00
|
|
|
aliases: []
|
2014-02-11 17:10:47 +01:00
|
|
|
nocache:
|
2014-01-22 13:04:19 +01:00
|
|
|
description:
|
2014-02-11 17:10:47 +01:00
|
|
|
- Do not use cache with building
|
2014-01-22 13:04:19 +01:00
|
|
|
required: false
|
2014-02-11 17:10:47 +01:00
|
|
|
default: false
|
2014-01-22 13:04:19 +01:00
|
|
|
aliases: []
|
|
|
|
docker_url:
|
|
|
|
description:
|
|
|
|
- URL of docker host to issue commands to
|
|
|
|
required: false
|
2015-07-02 02:15:23 +02:00
|
|
|
default: ${DOCKER_HOST} or unix://var/run/docker.sock
|
2014-01-22 13:04:19 +01:00
|
|
|
aliases: []
|
2015-07-02 02:15:23 +02:00
|
|
|
use_tls:
|
|
|
|
description:
|
|
|
|
- Whether to use tls to connect to the docker server. "no" means not to
|
|
|
|
use tls (and ignore any other tls related parameters). "encrypt" means
|
|
|
|
to use tls to encrypt the connection to the server. "verify" means to
|
|
|
|
also verify that the server's certificate is valid for the server
|
|
|
|
(this both verifies the certificate against the CA and that the
|
|
|
|
certificate was issued for that host. If this is unspecified, tls will
|
|
|
|
only be used if one of the other tls options require it.
|
|
|
|
choices: [ "no", "encrypt", "verify" ]
|
|
|
|
version_added: "1.9"
|
|
|
|
tls_client_cert:
|
|
|
|
description:
|
|
|
|
- Path to the PEM-encoded certificate used to authenticate docker client.
|
|
|
|
If specified tls_client_key must be valid
|
|
|
|
default: ${DOCKER_CERT_PATH}/cert.pem
|
|
|
|
version_added: "1.9"
|
|
|
|
tls_client_key:
|
|
|
|
description:
|
|
|
|
- Path to the PEM-encoded key used to authenticate docker client. If
|
|
|
|
specified tls_client_cert must be valid
|
|
|
|
default: ${DOCKER_CERT_PATH}/key.pem
|
|
|
|
version_added: "1.9"
|
|
|
|
tls_ca_cert:
|
|
|
|
description:
|
|
|
|
- Path to a PEM-encoded certificate authority to secure the Docker connection.
|
|
|
|
This has no effect if use_tls is encrypt.
|
|
|
|
default: ${DOCKER_CERT_PATH}/ca.pem
|
|
|
|
version_added: "1.9"
|
|
|
|
tls_hostname:
|
|
|
|
description:
|
|
|
|
- A hostname to check matches what's supplied in the docker server's
|
|
|
|
certificate. If unspecified, the hostname is taken from the docker_url.
|
|
|
|
default: Taken from docker_url
|
|
|
|
version_added: "1.9"
|
2015-05-23 22:17:30 +02:00
|
|
|
docker_api_version:
|
|
|
|
description:
|
|
|
|
- Remote API version to use. This defaults to the current default as
|
|
|
|
specified by docker-py.
|
|
|
|
default: docker-py default remote API version
|
|
|
|
version_added: "2.0"
|
2014-01-22 13:04:19 +01:00
|
|
|
state:
|
|
|
|
description:
|
2014-02-11 17:10:47 +01:00
|
|
|
- Set the state of the image
|
2014-01-22 13:04:19 +01:00
|
|
|
required: false
|
|
|
|
default: present
|
2014-02-11 17:10:47 +01:00
|
|
|
choices: [ "present", "absent", "build" ]
|
2014-01-22 13:04:19 +01:00
|
|
|
aliases: []
|
2014-02-11 17:10:47 +01:00
|
|
|
timeout:
|
2014-01-22 13:04:19 +01:00
|
|
|
description:
|
2014-02-11 17:10:47 +01:00
|
|
|
- Set image operation timeout
|
2014-01-22 13:04:19 +01:00
|
|
|
required: false
|
2014-02-11 17:10:47 +01:00
|
|
|
default: 600
|
2014-01-22 13:04:19 +01:00
|
|
|
aliases: []
|
2015-05-11 21:15:53 +02:00
|
|
|
requirements:
|
|
|
|
- "python >= 2.6"
|
|
|
|
- "docker-py"
|
|
|
|
- "requests"
|
2014-01-22 13:04:19 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2014-02-11 17:10:47 +01:00
|
|
|
Build docker image if required. Path should contains Dockerfile to build image:
|
2014-01-22 13:04:19 +01:00
|
|
|
|
|
|
|
- hosts: web
|
|
|
|
sudo: yes
|
|
|
|
tasks:
|
2014-02-11 17:10:47 +01:00
|
|
|
- name: check or build image
|
|
|
|
docker_image: path="/path/to/build/dir" name="my/app" state=present
|
2014-01-22 13:04:19 +01:00
|
|
|
|
2014-02-11 17:10:47 +01:00
|
|
|
Build new version of image:
|
2014-01-22 13:04:19 +01:00
|
|
|
|
|
|
|
- hosts: web
|
|
|
|
sudo: yes
|
|
|
|
tasks:
|
2014-02-11 17:10:47 +01:00
|
|
|
- name: check or build image
|
|
|
|
docker_image: path="/path/to/build/dir" name="my/app" state=build
|
2014-01-22 13:04:19 +01:00
|
|
|
|
2014-02-11 17:10:47 +01:00
|
|
|
Remove image from local docker storage:
|
2014-01-22 13:04:19 +01:00
|
|
|
|
|
|
|
- hosts: web
|
|
|
|
sudo: yes
|
|
|
|
tasks:
|
2014-06-14 00:15:29 +02:00
|
|
|
- name: remove image
|
2014-02-11 17:10:47 +01:00
|
|
|
docker_image: name="my/app" state=absent
|
2014-01-22 13:04:19 +01:00
|
|
|
|
|
|
|
'''
|
|
|
|
|
2015-05-11 21:15:53 +02:00
|
|
|
import re
|
2015-06-30 08:25:28 +02:00
|
|
|
import os
|
2015-05-11 21:15:53 +02:00
|
|
|
from urlparse import urlparse
|
|
|
|
|
2014-01-22 13:04:19 +01:00
|
|
|
try:
|
2014-02-26 21:27:39 +01:00
|
|
|
import json
|
2015-05-11 21:15:53 +02:00
|
|
|
except ImportError:
|
|
|
|
import simplejson as json
|
|
|
|
|
|
|
|
try:
|
2014-01-22 13:04:19 +01:00
|
|
|
from requests.exceptions import *
|
2015-05-11 21:15:53 +02:00
|
|
|
HAS_REQUESTS = True
|
|
|
|
except ImportError:
|
|
|
|
HAS_REQUESTS = False
|
2014-01-22 13:04:19 +01:00
|
|
|
|
2014-06-25 08:52:00 +02:00
|
|
|
try:
|
2015-05-11 21:15:53 +02:00
|
|
|
import docker.client
|
|
|
|
HAS_DOCKER_CLIENT = True
|
2014-06-25 08:52:00 +02:00
|
|
|
except ImportError:
|
2015-05-11 21:15:53 +02:00
|
|
|
HAS_DOCKER_CLIENT = False
|
|
|
|
|
2015-08-03 17:44:48 +02:00
|
|
|
DEFAULT_DOCKER_API_VERSION = None
|
2015-05-11 21:15:53 +02:00
|
|
|
if HAS_DOCKER_CLIENT:
|
|
|
|
try:
|
|
|
|
from docker.errors import APIError as DockerAPIError
|
|
|
|
except ImportError:
|
|
|
|
from docker.client import APIError as DockerAPIError
|
2014-06-25 08:52:00 +02:00
|
|
|
|
2015-05-23 22:17:30 +02:00
|
|
|
try:
|
|
|
|
# docker-py 1.2+
|
|
|
|
import docker.constants
|
|
|
|
DEFAULT_DOCKER_API_VERSION = docker.constants.DEFAULT_DOCKER_API_VERSION
|
|
|
|
except (ImportError, AttributeError):
|
|
|
|
# docker-py less than 1.2
|
|
|
|
DEFAULT_DOCKER_API_VERSION = docker.client.DEFAULT_DOCKER_API_VERSION
|
|
|
|
|
2014-01-22 13:04:19 +01:00
|
|
|
class DockerImageManager:
|
|
|
|
|
|
|
|
def __init__(self, module):
|
|
|
|
self.module = module
|
|
|
|
self.path = self.module.params.get('path')
|
2015-05-18 09:58:31 +02:00
|
|
|
self.dockerfile = self.module.params.get('dockerfile')
|
2014-01-22 13:04:19 +01:00
|
|
|
self.name = self.module.params.get('name')
|
|
|
|
self.tag = self.module.params.get('tag')
|
|
|
|
self.nocache = self.module.params.get('nocache')
|
2015-06-30 08:25:28 +02:00
|
|
|
|
|
|
|
# Connect to the docker server using any configured host and TLS settings.
|
|
|
|
|
|
|
|
env_host = os.getenv('DOCKER_HOST')
|
|
|
|
env_docker_verify = os.getenv('DOCKER_TLS_VERIFY')
|
|
|
|
env_cert_path = os.getenv('DOCKER_CERT_PATH')
|
|
|
|
env_docker_hostname = os.getenv('DOCKER_TLS_HOSTNAME')
|
|
|
|
|
|
|
|
docker_url = module.params.get('docker_url')
|
|
|
|
if not docker_url:
|
|
|
|
if env_host:
|
|
|
|
docker_url = env_host
|
|
|
|
else:
|
|
|
|
docker_url = 'unix://var/run/docker.sock'
|
|
|
|
|
|
|
|
docker_api_version = module.params.get('docker_api_version')
|
|
|
|
|
|
|
|
tls_client_cert = module.params.get('tls_client_cert', None)
|
|
|
|
if not tls_client_cert and env_cert_path:
|
|
|
|
tls_client_cert = os.path.join(env_cert_path, 'cert.pem')
|
|
|
|
|
|
|
|
tls_client_key = module.params.get('tls_client_key', None)
|
|
|
|
if not tls_client_key and env_cert_path:
|
|
|
|
tls_client_key = os.path.join(env_cert_path, 'key.pem')
|
|
|
|
|
|
|
|
tls_ca_cert = module.params.get('tls_ca_cert')
|
|
|
|
if not tls_ca_cert and env_cert_path:
|
|
|
|
tls_ca_cert = os.path.join(env_cert_path, 'ca.pem')
|
|
|
|
|
|
|
|
tls_hostname = module.params.get('tls_hostname')
|
|
|
|
if tls_hostname is None:
|
|
|
|
if env_docker_hostname:
|
|
|
|
tls_hostname = env_docker_hostname
|
|
|
|
else:
|
|
|
|
parsed_url = urlparse(docker_url)
|
|
|
|
if ':' in parsed_url.netloc:
|
|
|
|
tls_hostname = parsed_url.netloc[:parsed_url.netloc.rindex(':')]
|
|
|
|
else:
|
|
|
|
tls_hostname = parsed_url
|
|
|
|
if not tls_hostname:
|
|
|
|
tls_hostname = True
|
|
|
|
|
|
|
|
# use_tls can be one of four values:
|
|
|
|
# no: Do not use tls
|
|
|
|
# encrypt: Use tls. We may do client auth. We will not verify the server
|
|
|
|
# verify: Use tls. We may do client auth. We will verify the server
|
|
|
|
# None: Only use tls if the parameters for client auth were specified
|
|
|
|
# or tls_ca_cert (which requests verifying the server with
|
|
|
|
# a specific ca certificate)
|
|
|
|
use_tls = module.params.get('use_tls')
|
|
|
|
if use_tls is None and env_docker_verify is not None:
|
|
|
|
use_tls = 'verify'
|
|
|
|
|
|
|
|
tls_config = None
|
|
|
|
if use_tls != 'no':
|
|
|
|
params = {}
|
|
|
|
|
|
|
|
# Setup client auth
|
|
|
|
if tls_client_cert and tls_client_key:
|
|
|
|
params['client_cert'] = (tls_client_cert, tls_client_key)
|
|
|
|
|
|
|
|
# We're allowed to verify the connection to the server
|
|
|
|
if use_tls == 'verify' or (use_tls is None and tls_ca_cert):
|
|
|
|
if tls_ca_cert:
|
|
|
|
params['ca_cert'] = tls_ca_cert
|
|
|
|
params['verify'] = True
|
|
|
|
params['assert_hostname'] = tls_hostname
|
|
|
|
else:
|
|
|
|
params['verify'] = True
|
|
|
|
params['assert_hostname'] = tls_hostname
|
|
|
|
elif use_tls == 'encrypt':
|
|
|
|
params['verify'] = False
|
|
|
|
|
|
|
|
if params:
|
|
|
|
# See https://github.com/docker/docker-py/blob/d39da11/docker/utils/utils.py#L279-L296
|
|
|
|
docker_url = docker_url.replace('tcp://', 'https://')
|
|
|
|
tls_config = docker.tls.TLSConfig(**params)
|
|
|
|
|
2015-05-23 22:17:30 +02:00
|
|
|
self.client = docker.Client(
|
2015-07-07 15:44:27 +02:00
|
|
|
base_url=docker_url,
|
2015-05-23 22:17:30 +02:00
|
|
|
version=module.params.get('docker_api_version'),
|
2015-06-30 08:25:28 +02:00
|
|
|
timeout=module.params.get('timeout'),
|
|
|
|
tls=tls_config)
|
|
|
|
|
2014-01-22 13:04:19 +01:00
|
|
|
self.changed = False
|
2014-02-26 21:27:39 +01:00
|
|
|
self.log = []
|
|
|
|
self.error_msg = None
|
|
|
|
|
|
|
|
def get_log(self, as_string=True):
|
|
|
|
return "".join(self.log) if as_string else self.log
|
2014-01-22 13:04:19 +01:00
|
|
|
|
|
|
|
def build(self):
|
2015-05-18 09:58:31 +02:00
|
|
|
stream = self.client.build(self.path, dockerfile=self.dockerfile, tag=':'.join([self.name, self.tag]), nocache=self.nocache, rm=True, stream=True)
|
2014-02-26 21:27:39 +01:00
|
|
|
success_search = r'Successfully built ([0-9a-f]+)'
|
|
|
|
image_id = None
|
2014-01-22 13:04:19 +01:00
|
|
|
self.changed = True
|
|
|
|
|
2014-02-26 21:27:39 +01:00
|
|
|
for chunk in stream:
|
2014-03-30 21:27:29 +02:00
|
|
|
if not chunk:
|
|
|
|
continue
|
|
|
|
|
2014-05-12 20:51:55 +02:00
|
|
|
try:
|
|
|
|
chunk_json = json.loads(chunk)
|
|
|
|
except ValueError:
|
|
|
|
continue
|
2014-02-26 21:27:39 +01:00
|
|
|
|
|
|
|
if 'error' in chunk_json:
|
|
|
|
self.error_msg = chunk_json['error']
|
|
|
|
return None
|
|
|
|
|
|
|
|
if 'stream' in chunk_json:
|
|
|
|
output = chunk_json['stream']
|
|
|
|
self.log.append(output)
|
|
|
|
match = re.search(success_search, output)
|
|
|
|
if match:
|
|
|
|
image_id = match.group(1)
|
|
|
|
|
2014-05-12 20:51:55 +02:00
|
|
|
# Just in case we skipped evaluating the JSON returned from build
|
|
|
|
# during every iteration, add an error if the image_id was never
|
|
|
|
# populated
|
|
|
|
if not image_id:
|
|
|
|
self.error_msg = 'Unknown error encountered'
|
|
|
|
|
2014-02-26 21:27:39 +01:00
|
|
|
return image_id
|
2014-01-22 13:04:19 +01:00
|
|
|
|
|
|
|
def has_changed(self):
|
|
|
|
return self.changed
|
|
|
|
|
|
|
|
def get_images(self):
|
|
|
|
filtered_images = []
|
|
|
|
images = self.client.images()
|
|
|
|
for i in images:
|
2014-02-26 21:27:39 +01:00
|
|
|
# Docker-py version >= 0.3 (Docker API >= 1.8)
|
|
|
|
if 'RepoTags' in i:
|
2014-04-18 16:14:12 +02:00
|
|
|
repotag = ':'.join([self.name, self.tag])
|
2014-02-26 21:27:39 +01:00
|
|
|
if not self.name or repotag in i['RepoTags']:
|
|
|
|
filtered_images.append(i)
|
|
|
|
# Docker-py version < 0.3 (Docker API < 1.8)
|
|
|
|
elif (not self.name or self.name == i['Repository']) and (not self.tag or self.tag == i['Tag']):
|
2014-01-22 13:04:19 +01:00
|
|
|
filtered_images.append(i)
|
|
|
|
return filtered_images
|
|
|
|
|
|
|
|
def remove_images(self):
|
|
|
|
images = self.get_images()
|
|
|
|
for i in images:
|
|
|
|
try:
|
|
|
|
self.client.remove_image(i['Id'])
|
|
|
|
self.changed = True
|
2014-06-25 08:52:00 +02:00
|
|
|
except DockerAPIError as e:
|
2014-01-22 13:04:19 +01:00
|
|
|
# image can be removed by docker if not used
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec = dict(
|
2015-05-23 22:17:30 +02:00
|
|
|
path = dict(required=False, default=None),
|
|
|
|
dockerfile = dict(required=False, default="Dockerfile"),
|
|
|
|
name = dict(required=True),
|
|
|
|
tag = dict(required=False, default="latest"),
|
|
|
|
nocache = dict(default=False, type='bool'),
|
|
|
|
state = dict(default='present', choices=['absent', 'present', 'build']),
|
2015-06-30 08:25:28 +02:00
|
|
|
use_tls = dict(default=None, choices=['no', 'encrypt', 'verify']),
|
|
|
|
tls_client_cert = dict(required=False, default=None, type='str'),
|
|
|
|
tls_client_key = dict(required=False, default=None, type='str'),
|
|
|
|
tls_ca_cert = dict(required=False, default=None, type='str'),
|
|
|
|
tls_hostname = dict(required=False, type='str', default=None),
|
|
|
|
docker_url = dict(),
|
2015-05-23 22:17:30 +02:00
|
|
|
docker_api_version = dict(required=False,
|
|
|
|
default=DEFAULT_DOCKER_API_VERSION,
|
|
|
|
type='str'),
|
|
|
|
timeout = dict(default=600, type='int'),
|
2014-01-22 13:04:19 +01:00
|
|
|
)
|
|
|
|
)
|
2015-05-11 21:15:53 +02:00
|
|
|
if not HAS_DOCKER_CLIENT:
|
|
|
|
module.fail_json(msg='docker-py is needed for this module')
|
|
|
|
if not HAS_REQUESTS:
|
|
|
|
module.fail_json(msg='requests is needed for this module')
|
2014-01-22 13:04:19 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
manager = DockerImageManager(module)
|
|
|
|
state = module.params.get('state')
|
|
|
|
failed = False
|
|
|
|
image_id = None
|
|
|
|
msg = ''
|
2014-02-26 21:27:39 +01:00
|
|
|
do_build = False
|
2014-01-22 13:04:19 +01:00
|
|
|
|
|
|
|
# build image if not exists
|
|
|
|
if state == "present":
|
|
|
|
images = manager.get_images()
|
|
|
|
if len(images) == 0:
|
2014-02-26 21:27:39 +01:00
|
|
|
do_build = True
|
|
|
|
# build image
|
|
|
|
elif state == "build":
|
|
|
|
do_build = True
|
2014-01-22 13:04:19 +01:00
|
|
|
# remove image or images
|
|
|
|
elif state == "absent":
|
|
|
|
manager.remove_images()
|
|
|
|
|
2014-02-26 21:27:39 +01:00
|
|
|
if do_build:
|
|
|
|
image_id = manager.build()
|
|
|
|
if image_id:
|
2014-06-25 15:00:01 +02:00
|
|
|
msg = "Image built: %s" % image_id
|
2014-02-26 21:27:39 +01:00
|
|
|
else:
|
2014-01-22 13:04:19 +01:00
|
|
|
failed = True
|
2014-02-26 21:27:39 +01:00
|
|
|
msg = "Error: %s\nLog:%s" % (manager.error_msg, manager.get_log())
|
2014-01-22 13:04:19 +01:00
|
|
|
|
|
|
|
module.exit_json(failed=failed, changed=manager.has_changed(), msg=msg, image_id=image_id)
|
|
|
|
|
2015-07-02 01:41:17 +02:00
|
|
|
except SSLError as e:
|
2015-07-02 17:45:29 +02:00
|
|
|
if get_platform() == "Darwin":
|
2015-07-02 01:41:17 +02:00
|
|
|
# Ensure that the environment variables has been set
|
|
|
|
if "DOCKER_HOST" not in os.environ:
|
|
|
|
environment_error = '''
|
|
|
|
It looks like you have not set your docker environment
|
|
|
|
variables. Please ensure that you have set the requested
|
|
|
|
variables as instructed when running boot2docker up. If
|
|
|
|
they are set in .bash_profile you will need to symlink
|
|
|
|
it to .bashrc.
|
|
|
|
'''
|
|
|
|
module.exit_json(failed=True, chaged=manager.has_changed(), msg="SSLError: " + str(e) + environment_error)
|
|
|
|
# If the above is true it's likely the hostname does not match
|
|
|
|
else:
|
|
|
|
environment_error = '''
|
2015-07-07 21:37:36 +02:00
|
|
|
You may need to ignore hostname missmatches by setting
|
|
|
|
tls_hostname=boot2docker in your role. If this does not
|
|
|
|
resolve the issue please open an issue at
|
|
|
|
ansible/ansible-modules-core and ping michaeljs1990
|
2015-07-02 01:41:17 +02:00
|
|
|
'''
|
|
|
|
module.exit_json(failed=True, chaged=manager.has_changed(), msg="SSLError: " + str(e) + environment_error)
|
|
|
|
# General error for non darwin users
|
|
|
|
else:
|
|
|
|
module.exit_json(failed=True, chaged=manager.has_changed(), msg="SSLError: " + str(e))
|
|
|
|
|
2015-07-07 21:36:10 +02:00
|
|
|
except ConnectionError as e:
|
|
|
|
if get_platform() == "Darwin" and "DOCKER_HOST" not in os.environ:
|
|
|
|
# Ensure that the environment variables has been set
|
|
|
|
environment_error = '''
|
|
|
|
It looks like you have not set your docker environment
|
|
|
|
variables. Please ensure that you have set the requested
|
|
|
|
variables as instructed when running boot2docker up. If
|
|
|
|
they are set in .bash_profile you will need to symlink
|
|
|
|
it to .bashrc.
|
|
|
|
'''
|
|
|
|
module.exit_json(failed=True, chaged=manager.has_changed(), msg="ConnectionError: " + str(e) + environment_error)
|
|
|
|
|
|
|
|
module.exit_json(failed=True, chaged=manager.has_changed(), msg="ConnectionError: " + str(e))
|
|
|
|
|
2014-06-25 08:52:00 +02:00
|
|
|
except DockerAPIError as e:
|
2014-01-22 13:04:19 +01:00
|
|
|
module.exit_json(failed=True, changed=manager.has_changed(), msg="Docker API error: " + e.explanation)
|
|
|
|
|
|
|
|
except RequestException as e:
|
|
|
|
module.exit_json(failed=True, changed=manager.has_changed(), msg=repr(e))
|
2015-05-11 21:15:53 +02:00
|
|
|
|
2014-01-22 13:04:19 +01:00
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
2015-05-11 21:15:53 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|