community.general.docker_network (1.3.14) — module

Manage Docker networks

Authors: Ben Keith (@keitwb), Chris Houseknecht (@chouseknecht), Dave Bendit (@DBendit)

Install collection

Install with ansible-galaxy collection install community.general:==1.3.14


Add to requirements.yml

  collections:
    - name: community.general
      version: 1.3.14

Description

Create/remove Docker networks and connect containers to them.

Performs largely the same function as the "docker network" CLI subcommand.


Requirements

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Create a network
  community.general.docker_network:
    name: network_one
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Remove all but selected list of containers
  community.general.docker_network:
    name: network_one
    connected:
      - container_a
      - container_b
      - container_c
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Remove a single container
  community.general.docker_network:
    name: network_one
    connected: "{{ fulllist|difference(['container_a']) }}"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Add a container to a network, leaving existing containers connected
  community.general.docker_network:
    name: network_one
    connected:
      - container_a
    appends: yes
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Create a network with driver options
  community.general.docker_network:
    name: network_two
    driver_options:
      com.docker.network.bridge.name: net2
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Create a network with custom IPAM config
  community.general.docker_network:
    name: network_three
    ipam_config:
      - subnet: 172.3.27.0/24
        gateway: 172.3.27.2
        iprange: 172.3.27.0/26
        aux_addresses:
          host1: 172.3.27.3
          host2: 172.3.27.4
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Create a network with labels
  community.general.docker_network:
    name: network_four
    labels:
      key1: value1
      key2: value2
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Create a network with IPv6 IPAM config
  community.general.docker_network:
    name: network_ipv6_one
    enable_ipv6: yes
    ipam_config:
      - subnet: fdd1:ac8c:0557:7ce1::/64
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Create a network with IPv6 and custom IPv4 IPAM config
  community.general.docker_network:
    name: network_ipv6_two
    enable_ipv6: yes
    ipam_config:
      - subnet: 172.4.27.0/24
      - subnet: fdd1:ac8c:0557:7ce2::/64
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Delete a network, disconnecting all containers
  community.general.docker_network:
    name: network_one
    state: absent
    force: yes

Inputs

    
tls:
    default: false
    description:
    - Secure the connection to the API by using TLS without verifying the authenticity
      of the Docker host server. Note that if I(validate_certs) is set to C(yes) as well,
      it will take precedence.
    - If the value is not specified in the task, the value of environment variable C(DOCKER_TLS)
      will be used instead. If the environment variable is not set, the default value
      will be used.
    type: bool

name:
    aliases:
    - network_name
    description:
    - Name of the network to operate on.
    required: true
    type: str

debug:
    default: false
    description:
    - Debug mode
    type: bool

force:
    default: false
    description:
    - With state C(absent) forces disconnecting all containers from the network prior
      to deleting the network. With state C(present) will disconnect all containers, delete
      the network and re-create the network.
    - This option is required if you have changed the IPAM or driver options and want
      an existing network to be updated to use the new options.
    type: bool

scope:
    choices:
    - local
    - global
    - swarm
    description:
    - Specify the network's scope.
    type: str

state:
    choices:
    - absent
    - present
    default: present
    description:
    - C(absent) deletes the network. If a network has connected containers, it cannot
      be deleted. Use the I(force) option to disconnect all containers and delete the
      network.
    - C(present) creates the network, if it does not already exist with the specified
      parameters, and connects the list of containers provided via the connected parameter.
      Containers not on the list will be disconnected. An empty list will leave no containers
      connected to the network. Use the I(appends) option to leave existing containers
      connected. Use the I(force) options to force re-creation of the network.
    type: str

driver:
    default: bridge
    description:
    - Specify the type of network. Docker provides bridge and overlay drivers, but 3rd
      party drivers can also be used.
    type: str

labels:
    description:
    - Dictionary of labels.
    type: dict

appends:
    aliases:
    - incremental
    default: false
    description:
    - By default the connected list is canonical, meaning containers not on the list are
      removed from the network.
    - Use I(appends) to leave existing containers connected.
    type: bool

ca_cert:
    aliases:
    - tls_ca_cert
    - cacert_path
    description:
    - Use a CA certificate when performing server verification by providing the path to
      a CA certificate file.
    - If the value is not specified in the task and the environment variable C(DOCKER_CERT_PATH)
      is set, the file C(ca.pem) from the directory specified in the environment variable
      C(DOCKER_CERT_PATH) will be used.
    type: path

timeout:
    default: 60
    description:
    - The maximum amount of time in seconds to wait on a response from the API.
    - If the value is not specified in the task, the value of environment variable C(DOCKER_TIMEOUT)
      will be used instead. If the environment variable is not set, the default value
      will be used.
    type: int

internal:
    description:
    - Restrict external access to the network.
    type: bool

connected:
    aliases:
    - containers
    description:
    - List of container names or container IDs to connect to a network.
    - Please note that the module only makes sure that these containers are connected
      to the network, but does not care about connection options. If you rely on specific
      IP addresses etc., use the M(community.general.docker_container) module to ensure
      your containers are correctly connected to this network.
    elements: str
    type: list

attachable:
    description:
    - If enabled, and the network is in the global scope, non-service containers on worker
      nodes will be able to connect to the network.
    type: bool

client_key:
    aliases:
    - tls_client_key
    - key_path
    description:
    - Path to the client's TLS key file.
    - If the value is not specified in the task and the environment variable C(DOCKER_CERT_PATH)
      is set, the file C(key.pem) from the directory specified in the environment variable
      C(DOCKER_CERT_PATH) will be used.
    type: path

api_version:
    aliases:
    - docker_api_version
    default: auto
    description:
    - The version of the Docker API running on the Docker Host.
    - Defaults to the latest version of the API supported by Docker SDK for Python and
      the docker daemon.
    - If the value is not specified in the task, the value of environment variable C(DOCKER_API_VERSION)
      will be used instead. If the environment variable is not set, the default value
      will be used.
    type: str

client_cert:
    aliases:
    - tls_client_cert
    - cert_path
    description:
    - Path to the client's TLS certificate file.
    - If the value is not specified in the task and the environment variable C(DOCKER_CERT_PATH)
      is set, the file C(cert.pem) from the directory specified in the environment variable
      C(DOCKER_CERT_PATH) will be used.
    type: path

docker_host:
    aliases:
    - docker_url
    default: unix://var/run/docker.sock
    description:
    - The URL or Unix socket path used to connect to the Docker API. To connect to a remote
      host, provide the TCP connection string. For example, C(tcp://192.0.2.23:2376).
      If TLS is used to encrypt the connection, the module will automatically replace
      C(tcp) in the connection URL with C(https).
    - If the value is not specified in the task, the value of environment variable C(DOCKER_HOST)
      will be used instead. If the environment variable is not set, the default value
      will be used.
    type: str

enable_ipv6:
    description:
    - Enable IPv6 networking.
    type: bool

ipam_config:
    description:
    - List of IPAM config blocks. Consult L(Docker docs,https://docs.docker.com/compose/compose-file/compose-file-v2/#ipam)
      for valid options and values. Note that I(iprange) is spelled differently here (we
      use the notation from the Docker SDK for Python).
    elements: dict
    suboptions:
      aux_addresses:
        description:
        - Auxiliary IP addresses used by Network driver, as a mapping from hostname to
          IP.
        type: dict
      gateway:
        description:
        - IP gateway address.
        type: str
      iprange:
        description:
        - IP address range in CIDR notation.
        type: str
      subnet:
        description:
        - IP subset in CIDR notation.
        type: str
    type: list

ipam_driver:
    description:
    - Specify an IPAM driver.
    type: str

ssl_version:
    description:
    - Provide a valid SSL version number. Default value determined by ssl.py module.
    - If the value is not specified in the task, the value of environment variable C(DOCKER_SSL_VERSION)
      will be used instead.
    type: str

ipam_options:
    description:
    - Dictionary of IPAM options.
    - Deprecated in 2.8, will be removed in community.general 2.0.0. Use parameter I(ipam_config)
      instead. In Docker 1.10.0, IPAM options were introduced (see L(here,https://github.com/moby/moby/pull/17316)).
      This module parameter addresses the IPAM config not the newly introduced IPAM options.
      For the IPAM options, see the I(ipam_driver_options) parameter.
    suboptions:
      aux_addresses:
        description:
        - Auxiliary IP addresses used by Network driver, as a mapping from hostname to
          IP.
        type: dict
      gateway:
        description:
        - IP gateway address.
        type: str
      iprange:
        description:
        - IP address range in CIDR notation.
        type: str
      subnet:
        description:
        - IP subset in CIDR notation.
        type: str
    type: dict

tls_hostname:
    default: localhost
    description:
    - When verifying the authenticity of the Docker Host server, provide the expected
      name of the server.
    - If the value is not specified in the task, the value of environment variable C(DOCKER_TLS_HOSTNAME)
      will be used instead. If the environment variable is not set, the default value
      will be used.
    type: str

driver_options:
    description:
    - Dictionary of network settings. Consult docker docs for valid options and values.
    type: dict

validate_certs:
    aliases:
    - tls_verify
    default: false
    description:
    - Secure the connection to the API by using TLS and verifying the authenticity of
      the Docker host server.
    - If the value is not specified in the task, the value of environment variable C(DOCKER_TLS_VERIFY)
      will be used instead. If the environment variable is not set, the default value
      will be used.
    type: bool

ipam_driver_options:
    description:
    - Dictionary of IPAM driver options.
    type: dict

Outputs

network:
  description:
  - Network inspection results for the affected network.
  - Note that facts are part of the registered vars since Ansible 2.8. For compatibility
    reasons, the facts are also accessible directly as C(docker_network). Note that
    the returned fact will be removed in community.general 2.0.0.
  returned: success
  sample: {}
  type: dict