community.docker.docker_context_info module – Retrieve information on Docker contexts for the current user

Note

This module is part of the community.docker collection (version 4.4.0).

It is not included in ansible-core. To check whether it is installed, run ansible-galaxy collection list.

To install it, use: ansible-galaxy collection install community.docker.

To use it in a playbook, specify: community.docker.docker_context_info.

Synopsis

  • Essentially returns the output of docker context ls --format json.

Parameters

Parameter

Comments

cli_context

string

The Docker CLI context to use.

If set, will ignore the DOCKER_HOST and DOCKER_CONTEXT environment variables and the user’s Docker config.

If not set, the module will follow Docker CLI’s precedence and uses DOCKER_HOST if set; if not, uses DOCKER_CONTEXT if set; if not, uses the current context from the Docker config; if not set, uses default.

only_current

boolean

If set to true, contexts will just contain the current context and none else.

If set to false, contexts will list all contexts.

Choices:

  • false ← (default)

  • true

Attributes

Attribute

Support

Description

check_mode

Support: full

This action does not modify state.

Can run in check_mode and return changed status prediction without modifying target.

diff_mode

Support: N/A

This action does not modify state.

Will return details on what has changed (or possibly needs changing in check_mode), when in diff mode.

idempotent

Support: full

This action does not modify state.

When run twice in a row outside check mode, with the same arguments, the second invocation indicates no change.

This assumes that the system controlled/queried by the module has not changed in a relevant way.

Examples

- name: Get infos on contexts
  community.docker.docker_context_info:
  register: result

- name: Show all contexts
  ansible.builtin.debug:
    msg: "{{ result.contexts }}"

- name: Get current context
  community.docker.docker_context_info:
    only_current: true
  register: docker_current_context

- name: Run community.docker modules with current context
  module_defaults:
    group/community.docker.docker: "{{ docker_current_context.contexts[0].config }}"
  block:
    - name: Task using the current context
      community.docker.docker_container:
        image: ubuntu:latest
        name: ubuntu
        state: running

Return Values

Common return values are documented here, the following are the fields unique to this module:

Key

Description

contexts

list / elements=dictionary

A list of all contexts (only_current=false) or only the current context (only_current=true).

Returned: success

config

dictionary

In case the context is for Docker, contains option values to configure the community.docker modules to use this context.

Returned: success

Sample: {}

ca_path

boolean

The CA certificate used to validate the Docker daemon’s certificate.

Returned: success, context is for Docker, TLS config is present, and CA cert is present

Sample: "/path/to/ca-cert.pem"

client_cert

boolean

The client certificate to authenticate with to the Docker daemon.

Returned: success, context is for Docker, TLS config is present, and client cert info is present

Sample: "/path/to/client-cert.pem"

client_key

boolean

The client certificate’s key to authenticate with to the Docker daemon.

Returned: success, context is for Docker, TLS config is present, and client cert info is present

Sample: "/path/to/client-key.pem"

docker_host

string

The Docker daemon to connect to.

Returned: success and context is for Docker

Sample: "unix:///var/run/docker.sock"

tls

boolean

Whether the Docker context should use an unvalidated TLS connection.

Returned: success and context is for Docker

Sample: false

validate_certs

boolean

Whether the Docker context should use a validated TLS connection.

Returned: success, context is for Docker, and TLS config is present

Sample: true

current

boolean

Whether this context is the current one.

Returned: success

Sample: true

description

boolean

The context’s description, if available.

Returned: success

Sample: "My context"

meta_path

string

The path to the context’s meta directory.

Not present for contexts[].name=default.

Returned: success

Sample: "/home/felix/.docker/contexts/meta/0123456789abcdef01234567890abcdef0123456789abcdef0123456789abcde"

name

boolean

The context’s name.

Returned: success

Sample: "default"

tls_path

string

The path to the context’s TLS config directory.

Not present for contexts[].name=default.

Returned: success

Sample: "/home/user/.docker/contexts/tls/0123456789abcdef01234567890abcdef0123456789abcdef0123456789abcde/"

Authors

  • Felix Fontein (@felixfontein)