Skip to content

Commit

Permalink
Pass Docker daemon connection params from inventory to connection plu…
Browse files Browse the repository at this point in the history
…gin (#157)

* Move variable handling to doc fragment, and make them known to module_utils.

* Pass Daemon connection options to connection plugin.

* Add changelog fragment.

* Fix syntax error.

* Forgot 'options:'.
  • Loading branch information
felixfontein authored Jun 22, 2021
1 parent d0d5bdb commit 902bcc6
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 33 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/157-inventory-connection-options.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- "docker_containers inventory plugin - when ``connection_type=docker-api``, now pass Docker daemon connection options from inventory plugin to connection plugin. This can be disabled by setting ``configure_docker_daemon=false`` (https://github.com/ansible-collections/community.docker/pull/157)."
34 changes: 1 addition & 33 deletions plugins/connection/docker_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,41 +33,9 @@
- name: ansible_host
- name: ansible_docker_host
# The following are options from the docs fragment. We want to allow the user to
# specify them with Ansible variables.
docker_host:
vars:
- name: ansible_docker_docker_host
tls_hostname:
vars:
- name: ansible_docker_tls_hostname
api_version:
vars:
- name: ansible_docker_api_version
timeout:
vars:
- name: ansible_docker_timeout
ca_cert:
vars:
- name: ansible_docker_ca_cert
client_cert:
vars:
- name: ansible_docker_client_cert
client_key:
vars:
- name: ansible_docker_client_key
ssl_version:
vars:
- name: ansible_docker_ssl_version
tls:
vars:
- name: ansible_docker_tls
validate_certs:
vars:
- name: ansible_docker_validate_certs
extends_documentation_fragment:
- community.docker.docker
- community.docker.docker.var_names
- community.docker.docker.docker_py_1_documentation
'''

Expand Down
36 changes: 36 additions & 0 deletions plugins/doc_fragments/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,42 @@ class ModuleDocFragment(object):
and use C($DOCKER_CONFIG/config.json) otherwise.
'''

# For plugins: allow to define common options with Ansible variables

VAR_NAMES = r'''
options:
docker_host:
vars:
- name: ansible_docker_docker_host
tls_hostname:
vars:
- name: ansible_docker_tls_hostname
api_version:
vars:
- name: ansible_docker_api_version
timeout:
vars:
- name: ansible_docker_timeout
ca_cert:
vars:
- name: ansible_docker_ca_cert
client_cert:
vars:
- name: ansible_docker_client_cert
client_key:
vars:
- name: ansible_docker_client_key
ssl_version:
vars:
- name: ansible_docker_ssl_version
tls:
vars:
- name: ansible_docker_tls
validate_certs:
vars:
- name: ansible_docker_validate_certs
'''

# Additional, more specific stuff for minimal Docker SDK for Python version < 2.0

DOCKER_PY_1_DOCUMENTATION = r'''
Expand Down
19 changes: 19 additions & 0 deletions plugins/inventory/docker_containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,23 @@
R(docker connection plugin,ansible_collections.community.docker.docker_connection),
and C(docker-api) selects the
R(docker_api connection plugin,ansible_collections.community.docker.docker_api_connection).
- When C(docker-api) is used, all Docker daemon configuration values are passed from the inventory plugin
to the connection plugin. This can be controlled with I(configure_docker_daemon).
type: str
default: docker-api
choices:
- ssh
- docker-cli
- docker-api
configure_docker_daemon:
description:
- Whether to pass all Docker daemon configuration from the inventory plugin to the connection plugin.
- Only used when I(connection_type=docker-api).
type: bool
default: true
version_added: 1.8.0
verbose_output:
description:
- Toggle to (not) include all available inspection metadata.
Expand Down Expand Up @@ -138,6 +148,7 @@

from ansible_collections.community.docker.plugins.module_utils.common import (
RequestException,
DOCKER_COMMON_ARGS_VARS,
)
from ansible_collections.community.docker.plugins.plugin_utils.common import (
AnsibleDockerClient,
Expand Down Expand Up @@ -180,6 +191,13 @@ def _populate(self, client):
self.inventory.add_group('running')
self.inventory.add_group('stopped')

extra_facts = {}
if self.get_option('configure_docker_daemon'):
for option_name, var_name in DOCKER_COMMON_ARGS_VARS.items():
value = self.get_option(option_name)
if value is not None:
extra_facts[var_name] = value

for container in containers:
id = container.get('Id')
short_id = id[:13]
Expand Down Expand Up @@ -256,6 +274,7 @@ def _populate(self, client):
ansible_host=full_name,
ansible_connection='community.docker.docker_api',
))
facts.update(extra_facts)

full_facts.update(facts)
for key, value in inspect.items():
Expand Down
6 changes: 6 additions & 0 deletions plugins/module_utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ class RequestException(Exception):
debug=dict(type='bool', default=False)
)

DOCKER_COMMON_ARGS_VARS = dict([
[option_name, 'ansible_docker_%s' % option_name]
for option_name in DOCKER_COMMON_ARGS
if option_name != 'debug'
])

DOCKER_MUTUALLY_EXCLUSIVE = []

DOCKER_REQUIRED_TOGETHER = [
Expand Down

0 comments on commit 902bcc6

Please sign in to comment.