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: changed always true when profiles are present #191

Closed
hummeltech opened this issue Aug 19, 2021 · 7 comments · Fixed by #192
Closed

docker_compose: changed always true when profiles are present #191

hummeltech opened this issue Aug 19, 2021 · 7 comments · Fixed by #192

Comments

@hummeltech
Copy link

hummeltech commented Aug 19, 2021

SUMMARY

When using a docker-compose definition containing a service with a profiles specification, the value of changed is always true.

ISSUE TYPE
  • Bug Report
COMPONENT NAME

plugins/modules/docker_compose.py

ANSIBLE VERSION
$ ansible --version
ansible [core 2.11.3] 
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['~/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.9/site-packages/ansible
  ansible collection location = ~/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.9.6 (default, Jun 30 2021, 10:22:16) [GCC 11.1.0]
  jinja version = 3.0.1
  libyaml = True
COLLECTION VERSION
$ ansible-galaxy collection list community.docker

# /usr/lib/python3.9/site-packages/ansible_collections
Collection       Version
---------------- -------
community.docker 1.9.0
CONFIGURATION
$ ansible-config dump --only-changed
OS / ENVIRONMENT
STEPS TO REPRODUCE

Run the example playbook below two times:

---
- name: Error when using profiles
  hosts: localhost
  gather_facts: no
  tasks:
    - community.docker.docker_compose:
        debug: true
        project_name: example
        definition:
          version: '2'
          services:
            db:
              image: postgres:latest
              environment:
                POSTGRES_PASSWORD: example
            web:
              image: httpd:latest
              depends_on:
                - db
            p_web:
              image: httpd:latest
              depends_on:
                - db
              profiles:
                - p
      register: example_output

    - ansible.builtin.debug:
        var: example_output.actions

    - ansible.builtin.assert:
        that:
          - not example_output.changed

    - ansible.builtin.assert:
        that:
          - example_output.services.db.example_db_1.state.running
          - example_output.services.web.example_web_1.state.running
          - not example_output.services.p_web
EXPECTED RESULTS

There should be no changes after the first run of the playbook above.

ACTUAL RESULTS

It is always changed.

changed: [localhost] => {
    "actions": [
        {
            "create": [],
            "service": "p_web"
        }
    ],
    "changed": true,
    "invocation": {
        "module_args": {
            "api_version": "auto",
            "build": false,
            "ca_cert": null,
            "client_cert": null,
            "client_key": null,
            "debug": true,
            "definition": {
                "services": {
                    "db": {
                        "environment": {
                            "POSTGRES_PASSWORD": "example"
                        },
                        "image": "postgres:latest"
                    },
                    "p_web": {
                        "depends_on": [
                            "db"
                        ],
                        "image": "httpd:latest",
                        "profiles": [
                            "p"
                        ]
                    },
                    "web": {
                        "depends_on": [
                            "db"
                        ],
                        "image": "httpd:latest"
                    }
                },
                "version": "2"
            },
            "dependencies": true,
            "docker_host": "unix://var/run/docker.sock",
            "env_file": null,
            "files": null,
            "hostname_check": false,
            "nocache": false,
            "profiles": null,
            "project_name": "example",
            "project_src": null,
            "pull": false,
            "recreate": "smart",
            "remove_images": null,
            "remove_orphans": false,
            "remove_volumes": false,
            "restarted": false,
            "scale": null,
            "services": null,
            "ssl_version": null,
            "state": "present",
            "stopped": false,
            "timeout": 10,
            "tls": false,
            "tls_hostname": null,
            "use_ssh_client": false,
            "validate_certs": false
        }
    },
    "services": {
        "db": {
            "example_db_1": {
                "cmd": [
                    "postgres"
                ],
                "image": "postgres:latest",
                "labels": {
                    "com.docker.compose.config-hash": "1ab83594a4d5cc2746e95d0ca3ad3954d0bf47163384b5cb9386e73088c2e413",
                    "com.docker.compose.container-number": "1",
                    "com.docker.compose.oneoff": "False",
                    "com.docker.compose.project": "example",
                    "com.docker.compose.project.config_files": "/tmp/ansibleaj7s34_n/docker-compose.yml",
                    "com.docker.compose.project.working_dir": "/tmp/ansibleaj7s34_n",
                    "com.docker.compose.service": "db",
                    "com.docker.compose.version": "1.29.2"
                },
                "networks": {
                    "example_default": {
                        "IPAddress": "172.28.0.2",
                        "IPPrefixLen": 16,
                        "aliases": [
                            "db",
                            "c645eeb3a4a7"
                        ],
                        "globalIPv6": "",
                        "globalIPv6PrefixLen": 0,
                        "links": null,
                        "macAddress": "02:42:ac:1c:00:02"
                    }
                },
                "state": {
                    "running": true,
                    "status": "running"
                }
            }
        },
        "p_web": {},
        "web": {
            "example_web_1": {
                "cmd": [
                    "httpd-foreground"
                ],
                "image": "httpd:latest",
                "labels": {
                    "com.docker.compose.config-hash": "e8031049ec6c1362eeccde82c6cd569f8b7fb3e459434380885684a528b7fe37",
                    "com.docker.compose.container-number": "1",
                    "com.docker.compose.oneoff": "False",
                    "com.docker.compose.project": "example",
                    "com.docker.compose.project.config_files": "/tmp/ansiblew7q1cip3/docker-compose.yml",
                    "com.docker.compose.project.working_dir": "/tmp/ansiblew7q1cip3",
                    "com.docker.compose.service": "web",
                    "com.docker.compose.version": "1.29.2"
                },
                "networks": {
                    "example_default": {
                        "IPAddress": "172.28.0.3",
                        "IPPrefixLen": 16,
                        "aliases": [
                            "3324579e6f0c",
                            "web"
                        ],
                        "globalIPv6": "",
                        "globalIPv6PrefixLen": 0,
                        "links": null,
                        "macAddress": "02:42:ac:1c:00:03"
                    }
                },
                "state": {
                    "running": true,
                    "status": "running"
                }
            }
        }
    }
}
@hummeltech
Copy link
Author

This is where I believe result['changed'] is being set to True when using the example playbook:
https://github.com/ansible-collections/community.docker/blob/1.9.0/plugins/modules/docker_compose.py#L793-L803

I suspect it just needs to skip any service specifications not matching any specified profiles arguments.

@Ajpantuso
Copy link
Collaborator

Correct, I can pass the module's profiles list to this method to determine if the debug and changed logic should be skipped for the affected service.

@hummeltech
Copy link
Author

Nice idea, let me know if I can be of any assistance, or if you'd like me to work on creating a fix. Thanks

@Ajpantuso
Copy link
Collaborator

Nice idea, let me know if I can be of any assistance, or if you'd like me to work on creating a fix. Thanks

Can you try testing #192 locally? Very few of the ansible-test containers have docker-compose >=1.28.0 so any additional testing would be nice.

@hummeltech
Copy link
Author

Sure, I am testing it now.

@hummeltech
Copy link
Author

Yes, so far, so good, it seems to be resolved in both the above example and my actual use-case.

@hummeltech
Copy link
Author

I tried a definition without any profiles specifications as well without issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants