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_plugin does not work in Ansible's check mode #552

Closed
mizutani256 opened this issue Jan 12, 2023 · 2 comments · Fixed by #553
Closed

docker_plugin does not work in Ansible's check mode #552

mizutani256 opened this issue Jan 12, 2023 · 2 comments · Fixed by #553
Labels
bug Something isn't working docker-plain plain Docker (no swarm, no compose, no stack)

Comments

@mizutani256
Copy link

mizutani256 commented Jan 12, 2023

SUMMARY

If you run a docker_plugin task with states "present" or "enable" (maybe "disable" too) with ansible in check_mode (flag -C in command line), the task fails because it tries to query the docker API for information on the plugin when accessing the result property even though it is in check mode and the plugin is bound to be absent - thus causing the failure. I'm not sure what the proper behavior should be, but I believe the check run should not fail here.

The specific plugin I was trying to install is grafana/loki-docker-driver:latest, though I think that does not affect the issue.

ISSUE TYPE
  • Bug Report
COMPONENT NAME

docker_plugin

ANSIBLE VERSION
ansible 2.10.8
  config file = None
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.9.2 (default, Feb 28 2021, 17:03:44) [GCC 10.2.1 20210110]
COLLECTION VERSION
# /root/.ansible/collections/ansible_collections
Collection       Version
---------------- -------
community.docker 3.3.2  
# /usr/lib/python3/dist-packages/ansible_collections
Collection       Version
---------------- -------
community.docker 1.2.2 
CONFIGURATION

ansible-config dump --only-changed output was empty

OS / ENVIRONMENT

This is happening to me when running a GitLab CI script on a "python" image and using roughly this sequence of commands for setup:

echo 'deb http://ppa.launchpad.net/ansible/ansible/ubuntu trusty main' > /etc/apt/sources.list.d/ansible.list
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys # key omitted
apt-get update -y
apt-get install -y ansible
ansible-galaxy collection install community.docker
ansible-playbook deploy.yml -C # remaining arguments omitted

The target machine for the deploy runs an Ubuntu 22.04.

STEPS TO REPRODUCE

If I understood the module's code correctly, any attempt to install/enable a docker plugin via the docker_plugin module while running Ansible in check mode will fail. As an example, for me it failed using the following task:

- name: Install Grafana Loki's driver for capturing logs
  community.docker.docker_plugin:
    plugin_name: grafana/loki-docker-driver:latest
    state: enable
    alias: loki
EXPECTED RESULTS

I'm not exactly sure what the result property should return in check mode (would None be acceptabe?) but the playbook as a whole should not fail since, in check mode, it assumes the plugin was properly installed.

ACTUAL RESULTS

This is the failure output only from the docker_plugin task. If more information is needed feel free to ask. I'm showing only this part because I'm afraid of inadvertedly sharing sensible information on our development environment. Also, this was enough for me to find the problem in the module's source code after a while.

The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_community.docker.docker_plugin_payload_ag90pjpr/ansible_community.docker.docker_plugin_payload.zip/ansible_collections/community/docker/plugins/module_utils/_api/api/client.py", line 259, in _raise_for_status
    response.raise_for_status()
  File "/usr/lib/python3/dist-packages/requests/models.py", line 940, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 404 Client Error: Not Found for url: http+docker://localhost/v1.41/plugins/loki/json
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "/tmp/ansible_community.docker.docker_plugin_payload_ag90pjpr/ansible_community.docker.docker_plugin_payload.zip/ansible_collections/community/docker/plugins/modules/docker_plugin.py", line 375, in main
  File "/tmp/ansible_community.docker.docker_plugin_payload_ag90pjpr/ansible_community.docker.docker_plugin_payload.zip/ansible_collections/community/docker/plugins/modules/docker_plugin.py", line 353, in result
  File "/tmp/ansible_community.docker.docker_plugin_payload_ag90pjpr/ansible_community.docker.docker_plugin_payload.zip/ansible_collections/community/docker/plugins/module_utils/_api/api/client.py", line 532, in get_json
    return self._result(self._get(self._url(pathfmt, *args, versioned_api=True), **kwargs), json=True)
  File "/tmp/ansible_community.docker.docker_plugin_payload_ag90pjpr/ansible_community.docker.docker_plugin_payload.zip/ansible_collections/community/docker/plugins/module_utils/_api/api/client.py", line 266, in _result
    self._raise_for_status(response)
  File "/tmp/ansible_community.docker.docker_plugin_payload_ag90pjpr/ansible_community.docker.docker_plugin_payload.zip/ansible_collections/community/docker/plugins/module_utils/_api/api/client.py", line 261, in _raise_for_status
    raise_from(create_api_error_from_http_exception(e), e)
  File "/tmp/ansible_community.docker.docker_plugin_payload_ag90pjpr/ansible_community.docker.docker_plugin_payload.zip/ansible_collections/community/docker/plugins/module_utils/_api/errors.py", line 45, in create_api_error_from_http_exception
    raise_from(cls(e, response=response, explanation=explanation), e)
  File "<string>", line 3, in raise_from
ansible_collections.community.docker.plugins.module_utils._api.errors.NotFound: 404 Client Error for http+docker://localhost/v1.41/plugins/loki/json: Not Found ("plugin "loki" not found")
fatal: [swarm_manager]: FAILED! => {
    "changed": false,
    "invocation": {
        "module_args": {
            "alias": "loki",
            "api_version": "auto",
            "ca_cert": null,
            "client_cert": null,
            "client_key": null,
            "debug": false,
            "docker_host": "unix://var/run/docker.sock",
            "enable_timeout": 0,
            "force_remove": false,
            "plugin_name": "grafana/loki-docker-driver:latest",
            "plugin_options": {},
            "ssl_version": null,
            "state": "enable",
            "timeout": 60,
            "tls": false,
            "tls_hostname": null,
            "use_ssh_client": false,
            "validate_certs": false
        }
    },
    "msg": "An unexpected docker error occurred: 404 Client Error for http+docker://localhost/v1.41/plugins/loki/json: Not Found (\"plugin \"loki\" not found\")"
}
@felixfontein felixfontein added bug Something isn't working docker-plain plain Docker (no swarm, no compose, no stack) labels Jan 12, 2023
@felixfontein
Copy link
Collaborator

Thanks for the report! #553 should fix this.

@mizutani256
Copy link
Author

Great work! Thanks for the very quick fix!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working docker-plain plain Docker (no swarm, no compose, no stack)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants