Docker Network module labels support (#50883)

* Added support for labels for Docker Network

* Fixed missing comma in Docker Network module

* Specified minimal versions for dependencies and added changelog

* Fixes based on suggestions after code review

* Added integration tests
pull/51152/head
Jan Sušnik 6 years ago committed by ansibot
parent e3f61f4480
commit 70a39d358b

@ -0,0 +1,2 @@
minor_changes:
- docker_network - added support for specifying labels

@ -132,6 +132,14 @@ options:
default: null default: null
required: false required: false
labels:
version_added: 2.8
description:
- Dictionary of labels.
type: dict
default: null
required: false
scope: scope:
version_added: 2.8 version_added: 2.8
description: description:
@ -216,6 +224,13 @@ EXAMPLES = '''
host1: 172.3.27.3 host1: 172.3.27.3
host2: 172.3.27.4 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 - name: Create a network with IPv6 IPAM config
docker_network: docker_network:
name: network_ipv6_one name: network_ipv6_one
@ -283,6 +298,7 @@ class TaskParameters(DockerBaseClass):
self.appends = None self.appends = None
self.force = None self.force = None
self.internal = None self.internal = None
self.labels = None
self.debug = None self.debug = None
self.enable_ipv6 = None self.enable_ipv6 = None
self.scope = None self.scope = None
@ -437,6 +453,17 @@ class DockerNetworkManager(object):
differences.add('attachable', differences.add('attachable',
parameter=self.parameters.attachable, parameter=self.parameters.attachable,
active=net.get('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 return not differences.empty, differences
@ -475,6 +502,8 @@ class DockerNetworkManager(object):
params['scope'] = self.parameters.scope params['scope'] = self.parameters.scope
if self.parameters.attachable is not None: if self.parameters.attachable is not None:
params['attachable'] = self.parameters.attachable params['attachable'] = self.parameters.attachable
if self.parameters.labels:
params['labels'] = self.parameters.labels
if not self.check_mode: if not self.check_mode:
resp = self.client.create_network(self.parameters.network_name, **params) resp = self.client.create_network(self.parameters.network_name, **params)
@ -586,6 +615,7 @@ def main():
)), )),
enable_ipv6=dict(type='bool'), enable_ipv6=dict(type='bool'),
internal=dict(type='bool'), internal=dict(type='bool'),
labels=dict(type='dict', default={}),
debug=dict(type='bool', default=False), debug=dict(type='bool', default=False),
scope=dict(type='str', choices=['local', 'global', 'swarm']), scope=dict(type='str', choices=['local', 'global', 'swarm']),
attachable=dict(type='bool'), attachable=dict(type='bool'),
@ -598,6 +628,7 @@ def main():
option_minimal_versions = dict( option_minimal_versions = dict(
scope=dict(docker_py_version='2.6.0', docker_api_version='1.30'), 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'), attachable=dict(docker_py_version='2.0.0', docker_api_version='1.26'),
labels=dict(docker_api_version='1.23'),
) )
client = AnsibleDockerClient( client = AnsibleDockerClient(

@ -189,3 +189,51 @@
- attachable_1 is failed - attachable_1 is failed
- "('version is ' ~ docker_py_version ~'. Minimum version required is 2.0.0') in attachable_1.msg" - "('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', '<') 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

Loading…
Cancel
Save