From 902bcc6193dd94268c7326ee661362edaf0265a6 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Tue, 22 Jun 2021 06:29:56 +0200 Subject: [PATCH] Pass Docker daemon connection params from inventory to connection plugin (#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:'. --- .../157-inventory-connection-options.yml | 2 ++ plugins/connection/docker_api.py | 34 +----------------- plugins/doc_fragments/docker.py | 36 +++++++++++++++++++ plugins/inventory/docker_containers.py | 19 ++++++++++ plugins/module_utils/common.py | 6 ++++ 5 files changed, 64 insertions(+), 33 deletions(-) create mode 100644 changelogs/fragments/157-inventory-connection-options.yml diff --git a/changelogs/fragments/157-inventory-connection-options.yml b/changelogs/fragments/157-inventory-connection-options.yml new file mode 100644 index 00000000..26d6d968 --- /dev/null +++ b/changelogs/fragments/157-inventory-connection-options.yml @@ -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)." diff --git a/plugins/connection/docker_api.py b/plugins/connection/docker_api.py index d1cccf81..13ba07b8 100644 --- a/plugins/connection/docker_api.py +++ b/plugins/connection/docker_api.py @@ -33,41 +33,9 @@ options: - 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 ''' diff --git a/plugins/doc_fragments/docker.py b/plugins/doc_fragments/docker.py index d10bb78b..162b3a33 100644 --- a/plugins/doc_fragments/docker.py +++ b/plugins/doc_fragments/docker.py @@ -116,6 +116,42 @@ notes: 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''' diff --git a/plugins/inventory/docker_containers.py b/plugins/inventory/docker_containers.py index dc7eb22a..7dbf88b9 100644 --- a/plugins/inventory/docker_containers.py +++ b/plugins/inventory/docker_containers.py @@ -44,6 +44,8 @@ options: 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: @@ -51,6 +53,14 @@ options: - 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. @@ -138,6 +148,7 @@ from ansible.plugins.inventory import BaseInventoryPlugin, Constructable 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, @@ -180,6 +191,13 @@ class InventoryModule(BaseInventoryPlugin, Constructable): 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] @@ -256,6 +274,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable): ansible_host=full_name, ansible_connection='community.docker.docker_api', )) + facts.update(extra_facts) full_facts.update(facts) for key, value in inspect.items(): diff --git a/plugins/module_utils/common.py b/plugins/module_utils/common.py index fbe2ba28..7e9f4a2e 100644 --- a/plugins/module_utils/common.py +++ b/plugins/module_utils/common.py @@ -96,6 +96,12 @@ DOCKER_COMMON_ARGS = dict( 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 = [