Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docker_compose - Fix idempotency when services have no enabled profiles #192

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
bugfixes:
- docker_compose - fixed incorrect ``changed`` status for services with ``profiles`` defined, but none enabled
(https://github.com/ansible-collections/community.docker/pull/192).
10 changes: 10 additions & 0 deletions plugins/modules/docker_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,8 @@ def cmd_up(self):
# In case the only action is starting, and the user requested
# that the service should be stopped, ignore this service.
continue
if not self._service_profile_enabled(service):
continue
if plan.action != 'noop':
result['changed'] = True
result_action = dict(service=service.name)
Expand Down Expand Up @@ -1010,6 +1012,14 @@ def cmd_build(self):
))
return result

def _service_profile_enabled(self, service):
"""Returns `True` if the service has no profiles defined or has a profile which is among
the profiles passed to the `docker compose up` command. Otherwise returns `False`.
"""
if LooseVersion(compose_version) < LooseVersion('1.28.0'):
return True
return service.enabled_for_profiles(self.profiles or [])

def cmd_down(self):
result = dict(
changed=False,
Expand Down
26 changes: 14 additions & 12 deletions tests/integration/targets/docker_compose/tasks/tests/options.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
- profile_all
stop_grace_period: 1s
test_cases:
- test_name: no services enabled
- test_name: enable 1
profiles_value:
- profile_1
Expand Down Expand Up @@ -61,7 +62,7 @@
docker_compose:
project_name: "{{ pname }}"
definition: "{{ test_service | from_yaml }}"
profiles: "{{ test_case.profiles_value }}"
profiles: "{{ test_case.profiles_value | default(omit) }}"
stopped: "{{ test_case.stopped_value | default(omit) }}"
state: present
register: profiles_outputs
Expand All @@ -77,20 +78,21 @@

- assert:
that:
- profiles_outputs.results[0].services[cname_1][cname_1_name].state.running
- profiles_outputs.results[0].services[cname_2] == {}
- not profiles_outputs.results[1].services[cname_1][cname_1_name].state.running
- profiles_outputs.results[0] is not changed
- profiles_outputs.results[1].services[cname_1][cname_1_name].state.running
- profiles_outputs.results[1].services[cname_2] == {}
- not profiles_outputs.results[2].services[cname_1][cname_1_name].state.running
- profiles_outputs.results[2].services[cname_2][cname_2_name].state.running
- profiles_outputs.results[2].services[cname_2] == {}
- not profiles_outputs.results[3].services[cname_1][cname_1_name].state.running
- not profiles_outputs.results[3].services[cname_2][cname_2_name].state.running
- profiles_outputs.results[4].services[cname_1][cname_1_name].state.running
- profiles_outputs.results[4].services[cname_2][cname_2_name].state.running
- not profiles_outputs.results[5].services[cname_1][cname_1_name].state.running
- not profiles_outputs.results[5].services[cname_2][cname_2_name].state.running
- profiles_outputs.results[6].services[cname_1][cname_1_name].state.running
- profiles_outputs.results[6].services[cname_2][cname_2_name].state.running
- profiles_outputs.results[3].services[cname_2][cname_2_name].state.running
- not profiles_outputs.results[4].services[cname_1][cname_1_name].state.running
- not profiles_outputs.results[4].services[cname_2][cname_2_name].state.running
- profiles_outputs.results[5].services[cname_1][cname_1_name].state.running
- profiles_outputs.results[5].services[cname_2][cname_2_name].state.running
- not profiles_outputs.results[6].services[cname_1][cname_1_name].state.running
- not profiles_outputs.results[6].services[cname_2][cname_2_name].state.running
- profiles_outputs.results[7].services[cname_1][cname_1_name].state.running
- profiles_outputs.results[7].services[cname_2][cname_2_name].state.running
vars:
cname_1_name: "{{ pname + '_' + cname_1 + '_1' }}"
cname_2_name: "{{ pname + '_' + cname_2 + '_1' }}"
Expand Down