diff --git a/changelogs/fragments/50883-docker_network-labels-support.yaml b/changelogs/fragments/50883-docker_network-labels-support.yaml new file mode 100644 index 00000000000..7ff235664bd --- /dev/null +++ b/changelogs/fragments/50883-docker_network-labels-support.yaml @@ -0,0 +1,2 @@ +minor_changes: + - docker_network - added support for specifying labels diff --git a/lib/ansible/modules/cloud/docker/docker_network.py b/lib/ansible/modules/cloud/docker/docker_network.py index 50dfc06dbad..ca63d87c264 100644 --- a/lib/ansible/modules/cloud/docker/docker_network.py +++ b/lib/ansible/modules/cloud/docker/docker_network.py @@ -132,6 +132,14 @@ options: default: null required: false + labels: + version_added: 2.8 + description: + - Dictionary of labels. + type: dict + default: null + required: false + scope: version_added: 2.8 description: @@ -216,6 +224,13 @@ EXAMPLES = ''' host1: 172.3.27.3 host2: 172.3.27.4 +- name: Create a network with labels + docker_network: + name: network_four + labels: + key1: value1 + key2: value2 + - name: Create a network with IPv6 IPAM config docker_network: name: network_ipv6_one @@ -283,6 +298,7 @@ class TaskParameters(DockerBaseClass): self.appends = None self.force = None self.internal = None + self.labels = None self.debug = None self.enable_ipv6 = None self.scope = None @@ -437,6 +453,17 @@ class DockerNetworkManager(object): differences.add('attachable', parameter=self.parameters.attachable, active=net.get('Attachable')) + if self.parameters.labels: + if not net.get('Labels'): + differences.add('labels', + parameter=self.parameters.labels, + active=net.get('Labels')) + else: + for key, value in self.parameters.labels.items(): + if not (key in net['Labels']) or value != net['Labels'][key]: + differences.add('labels.%s' % key, + parameter=value, + active=net['Labels'].get(key)) return not differences.empty, differences @@ -475,6 +502,8 @@ class DockerNetworkManager(object): params['scope'] = self.parameters.scope if self.parameters.attachable is not None: params['attachable'] = self.parameters.attachable + if self.parameters.labels: + params['labels'] = self.parameters.labels if not self.check_mode: resp = self.client.create_network(self.parameters.network_name, **params) @@ -586,6 +615,7 @@ def main(): )), enable_ipv6=dict(type='bool'), internal=dict(type='bool'), + labels=dict(type='dict', default={}), debug=dict(type='bool', default=False), scope=dict(type='str', choices=['local', 'global', 'swarm']), attachable=dict(type='bool'), @@ -598,6 +628,7 @@ def main(): option_minimal_versions = dict( scope=dict(docker_py_version='2.6.0', docker_api_version='1.30'), attachable=dict(docker_py_version='2.0.0', docker_api_version='1.26'), + labels=dict(docker_api_version='1.23'), ) client = AnsibleDockerClient( diff --git a/test/integration/targets/docker_network/tasks/tests/options.yml b/test/integration/targets/docker_network/tasks/tests/options.yml index 19248e6d6e0..566f1d26c19 100644 --- a/test/integration/targets/docker_network/tasks/tests/options.yml +++ b/test/integration/targets/docker_network/tasks/tests/options.yml @@ -189,3 +189,51 @@ - attachable_1 is failed - "('version is ' ~ docker_py_version ~'. Minimum version required is 2.0.0') in attachable_1.msg" when: docker_py_version is version('2.0.0', '<') + +#################################################################### +## labels ########################################################## +#################################################################### + +- name: labels + docker_network: + name: "{{ nname_1 }}" + labels: + ansible.test.1: hello + ansible.test.2: world + register: labels_1 + +- name: labels (idempotency) + docker_network: + name: "{{ nname_1 }}" + labels: + ansible.test.2: world + ansible.test.1: hello + register: labels_2 + +- name: labels (less labels) + docker_network: + name: "{{ nname_1 }}" + labels: + ansible.test.1: hello + register: labels_3 + +- name: labels (more labels) + docker_network: + name: "{{ nname_1 }}" + labels: + ansible.test.1: hello + ansible.test.3: ansible + register: labels_4 + +- name: cleanup + docker_network: + name: "{{ nname_1 }}" + state: absent + force: yes + +- assert: + that: + - labels_1 is changed + - labels_2 is not changed + - labels_3 is not changed + - labels_4 is changed