Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements extra_hosts for docker_image module #59540

Merged
merged 4 commits into from
Jul 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/docker_image_etc_hosts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- docker_image - added ``extra_hosts`` argument (https://github.com/ansible/ansible/issues/59233)
15 changes: 15 additions & 0 deletions lib/ansible/modules/cloud/docker/docker_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@
- Do not use cache when building an image.
type: bool
default: no
etc_hosts:
description:
- Extra hosts to add to C(/etc/hosts) in building containers, as a mapping of hostname to IP address.
type: dict
version_added: "2.9"
args:
description:
- Provide a dictionary of C(key:value) build arguments that map to Dockerfile ARG directive.
Expand Down Expand Up @@ -324,6 +329,7 @@
author:
- Pavel Antonov (@softzilla)
- Chris Houseknecht (@chouseknecht)
- Sorin Sbarnea (@ssbarnea)

'''

Expand Down Expand Up @@ -424,6 +430,7 @@
from distutils.version import LooseVersion

from ansible.module_utils.docker.common import (
clean_dict_booleans_for_docker_api,
docker_version,
AnsibleDockerClient,
DockerBaseClass,
Expand Down Expand Up @@ -469,6 +476,7 @@ def __init__(self, client, results):
self.load_path = parameters.get('load_path')
self.name = parameters.get('name')
self.network = build.get('network')
self.extra_hosts = clean_dict_booleans_for_docker_api(build.get('etc_hosts'))
self.nocache = build.get('nocache', False)
self.build_path = build.get('path')
self.pull = build.get('pull')
Expand Down Expand Up @@ -738,6 +746,8 @@ def build_image(self):
params['cache_from'] = self.cache_from
if self.network:
params['network_mode'] = self.network
if self.extra_hosts:
params['extra_hosts'] = self.extra_hosts
if self.use_config_proxy:
params['use_config_proxy'] = self.use_config_proxy
# Due to a bug in docker-py, it will crash if
Expand Down Expand Up @@ -813,6 +823,7 @@ def main():
args=dict(type='dict'),
use_config_proxy=dict(type='bool'),
target=dict(type='str'),
etc_hosts=dict(type='dict'),
)),
archive_path=dict(type='path'),
container_limits=dict(type='dict', options=dict(
Expand Down Expand Up @@ -859,11 +870,15 @@ def detect_build_target(client):
def detect_use_config_proxy(client):
return client.module.params['build'] and client.module.params['build'].get('use_config_proxy') is not None

def detect_etc_hosts(client):
return client.module.params['build'] and bool(client.module.params['build'].get('etc_hosts'))

option_minimal_versions = dict()
option_minimal_versions["build.cache_from"] = dict(docker_py_version='2.1.0', docker_api_version='1.25', detect_usage=detect_build_cache_from)
option_minimal_versions["build.network"] = dict(docker_py_version='2.4.0', docker_api_version='1.25', detect_usage=detect_build_network)
option_minimal_versions["build.target"] = dict(docker_py_version='2.4.0', detect_usage=detect_build_target)
option_minimal_versions["build.use_config_proxy"] = dict(docker_py_version='3.7.0', detect_usage=detect_use_config_proxy)
option_minimal_versions["build.etc_hosts"] = dict(docker_py_version='2.6.0', docker_api_version='1.27', detect_usage=detect_etc_hosts)

client = AnsibleDockerClient(
argument_spec=argument_spec,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM busybox
# This should fail building if docker cannot resolve some-custom-host
RUN ping -c1 some-custom-host
26 changes: 26 additions & 0 deletions test/integration/targets/docker_image/tasks/tests/options.yml
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,29 @@
that:
- dockerfile_2 is changed
- dockerfile_2.image.Config.WorkingDir == '/first'

####################################################################
## build.etc_hosts #################################################
####################################################################

- name: Build image with custom etc_hosts
docker_image:
name: "{{ iname }}"
build:
path: "{{ role_path }}/files"
dockefile: "EtcHostsDockerfile"
pull: no
etc_hosts:
some-custom-host: "127.0.0.1"
source: build
register: path_1

- name: cleanup
docker_image:
name: "{{ iname }}"
state: absent
force_absent: yes

- assert:
that:
- path_1 is changed