diff --git a/changelogs/fragments/52425-docker_swarm_service-add_working_dir_option.yml b/changelogs/fragments/52425-docker_swarm_service-add_working_dir_option.yml new file mode 100644 index 00000000000..4291931ac6e --- /dev/null +++ b/changelogs/fragments/52425-docker_swarm_service-add_working_dir_option.yml @@ -0,0 +1,2 @@ +minor_changes: + - "docker_swarm_service - Added support for ``working_dir`` parameter." diff --git a/lib/ansible/modules/cloud/docker/docker_swarm_service.py b/lib/ansible/modules/cloud/docker/docker_swarm_service.py index d73d91a1812..c8d33682122 100644 --- a/lib/ansible/modules/cloud/docker/docker_swarm_service.py +++ b/lib/ansible/modules/cloud/docker/docker_swarm_service.py @@ -446,6 +446,12 @@ options: - Before Ansible 2.8, the default value for this option was C(root). - The default has been removed so that the user defined in the image is used if no user is specified here. - Corresponds to the C(--user) option of C(docker service create). + working_dir: + type: str + description: + - Path to the working directory. + - Corresponds to the C(--workdir) option of C(docker service create). + version_added: "2.8" extends_documentation_fragment: - docker - docker.docker_py_2_documentation @@ -743,6 +749,7 @@ class DockerService(DockerBaseClass): self.update_monitor = None self.update_max_failure_ratio = None self.update_order = None + self.working_dir = None def get_facts(self): return { @@ -786,7 +793,8 @@ class DockerService(DockerBaseClass): 'update_failure_action': self.update_failure_action, 'update_monitor': self.update_monitor, 'update_max_failure_ratio': self.update_max_failure_ratio, - 'update_order': self.update_order + 'update_order': self.update_order, + 'working_dir': self.working_dir, } @staticmethod @@ -823,6 +831,7 @@ class DockerService(DockerBaseClass): s.update_max_failure_ratio = ap['update_max_failure_ratio'] s.update_order = ap['update_order'] s.user = ap['user'] + s.working_dir = ap['working_dir'] s.command = ap['command'] if isinstance(s.command, string_types): @@ -1012,6 +1021,8 @@ class DockerService(DockerBaseClass): differences.add('hostname', parameter=self.hostname, active=os.hostname) if self.tty is not None and self.tty != os.tty: differences.add('tty', parameter=self.tty, active=os.tty) + if self.working_dir is not None and self.working_dir != os.working_dir: + differences.add('working_dir', parameter=self.working_dir, active=os.working_dir) if self.force_update: force_update = True return not differences.empty or force_update, differences, needs_rebuild, force_update @@ -1138,9 +1149,11 @@ class DockerService(DockerBaseClass): container_spec_args['tty'] = self.tty if self.groups is not None: container_spec_args['groups'] = self.groups + if self.working_dir is not None: + container_spec_args['workdir'] = self.working_dir if secrets is not None: container_spec_args['secrets'] = secrets - if self.mounts is not None: + if mounts is not None: container_spec_args['mounts'] = mounts if dns_config is not None: container_spec_args['dns_config'] = dns_config @@ -1328,6 +1341,7 @@ class DockerServiceManager(object): ds.args = task_template_data['ContainerSpec'].get('Args') ds.groups = task_template_data['ContainerSpec'].get('Groups') ds.stop_signal = task_template_data['ContainerSpec'].get('StopSignal') + ds.working_dir = task_template_data['ContainerSpec'].get('Dir') healthcheck_data = task_template_data['ContainerSpec'].get('Healthcheck') if healthcheck_data: @@ -1703,7 +1717,8 @@ def main(): update_monitor=dict(type='int'), update_max_failure_ratio=dict(type='float'), update_order=dict(type='str'), - user=dict(type='str') + user=dict(type='str'), + working_dir=dict(type='str'), ) option_minimal_versions = dict( diff --git a/test/integration/targets/docker_swarm_service/tasks/tests/options.yml b/test/integration/targets/docker_swarm_service/tasks/tests/options.yml index 1c44e0302c8..e43d22c89a4 100644 --- a/test/integration/targets/docker_swarm_service/tasks/tests/options.yml +++ b/test/integration/targets/docker_swarm_service/tasks/tests/options.yml @@ -2524,6 +2524,43 @@ - user_2 is not changed - user_3 is changed +#################################################################### +## working_dir ##################################################### +#################################################################### + +- name: working_dir + docker_swarm_service: + name: "{{ service_name }}" + image: alpine:3.8 + working_dir: /tmp + register: working_dir_1 + +- name: working_dir (idempotency) + docker_swarm_service: + name: "{{ service_name }}" + image: alpine:3.8 + working_dir: /tmp + register: working_dir_2 + +- name: working_dir (change) + docker_swarm_service: + name: "{{ service_name }}" + image: alpine:3.8 + working_dir: / + register: working_dir_3 + +- name: cleanup + docker_swarm_service: + name: "{{ service_name }}" + state: absent + diff: no + +- assert: + that: + - working_dir_1 is changed + - working_dir_2 is not changed + - working_dir_3 is changed + #################################################################### #################################################################### #################################################################### diff --git a/test/integration/targets/docker_swarm_service/vars/main.yml b/test/integration/targets/docker_swarm_service/vars/main.yml index cdde68fce88..8c92675b9e5 100644 --- a/test/integration/targets/docker_swarm_service/vars/main.yml +++ b/test/integration/targets/docker_swarm_service/vars/main.yml @@ -44,3 +44,4 @@ service_expected_output: update_monitor: null update_order: null update_parallelism: 1 + working_dir: null