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_host_info: Filter containers by multiple labels #18

Closed
yajo opened this issue Jun 3, 2020 · 9 comments · Fixed by #160
Closed

docker_host_info: Filter containers by multiple labels #18

yajo opened this issue Jun 3, 2020 · 9 comments · Fixed by #160
Labels
docker-plain plain Docker (no swarm, no compose, no stack)

Comments

@yajo
Copy link
Contributor

yajo commented Jun 3, 2020

SUMMARY

Cannot filter for multiple labels.

ISSUE TYPE
  • Feature Idea
COMPONENT NAME

docker_host_info

ADDITIONAL INFORMATION

This cannot work because we can't define the same key twice in a dict:

- name: find running docker containers
  register: docker_info
  docker_host_info:
    containers: yes
    containers_filters:
      label: com.docker.compose.project=myproject # THIS GETS IGNORED
      label: com.docker.compose.service=backup

Possible solutions:

  1. Allow a list in containers_filters:

    - name: find running docker containers
      register: docker_info
      docker_host_info:
        containers: yes
        containers_filters:
          - label=com.docker.compose.project=myproject
          - label=com.docker.compose.service=backup
  2. Allow a dict in containers_filters.label filter:

    - name: find running docker containers
      register: docker_info
      docker_host_info:
        containers: yes
        containers_filters:
          label:
            com.docker.compose.project: myproject
            com.docker.compose.service: backup

I have not tested, but I guess the same applies to images_filters, networks_filters and volumes_filters.

@ansibullbot
Copy link

Files identified in the description:

If these files are inaccurate, please update the component name section of the description or use the !component bot command.

click here for bot help

@felixfontein felixfontein changed the title Filter containers by multiple labels docker_host_info: Filter containers by multiple labels Jun 3, 2020
@felixfontein
Copy link
Collaborator

This is the interface provided by Docker itself: it wants a dictionary of filters. If we provide something more sophisticated, we probably have to parse and process this by ourselves.

@yajo
Copy link
Contributor Author

yajo commented Jun 5, 2020

But you can actually do that filtering with CLI, right?

@felixfontein
Copy link
Collaborator

No idea. Feel free to figure out how the CLI does it.

@yajo
Copy link
Contributor Author

yajo commented Jun 5, 2020

Definitely the CLI can do it:

2020-06-05_09-30

I think you use docker-py behind the scenes, right? Its docs say you can pass a list to the label fiilter. That would easily map like this to ansible:

- name: find running docker containers
  register: docker_info
  docker_host_info:
    containers: yes
    containers_filters:
      label:
        - com.docker.compose.project=myproject
        - com.docker.compose.service=backup

@felixfontein
Copy link
Collaborator

I never claimed the CLI can't do it. I was just stating that I don't know whether it can do it (and honestly, I don't care if it can or not).

Feel free to create a PR to implement this.

@felixfontein felixfontein transferred this issue from ansible-collections/community.general Nov 1, 2020
@felixfontein felixfontein added the docker-plain plain Docker (no swarm, no compose, no stack) label Jan 2, 2021
@Ajpantuso
Copy link
Collaborator

@felixfontein, the OP referred to the higher-level API, but the API used by this module does in fact permit a list of labels.
I could do something like the following to allow lists to remain intact with minimal impact to the other modules. (With a doc update as well)

Update this to

def clean_dict_booleans_for_docker_api(data, allow_sequences=False):
    '''
    Go doesn't like Python booleans 'True' or 'False', while Ansible is just
    fine with them in YAML. As such, they need to be converted in cases where
    we pass dictionaries to the Docker API (e.g. docker_network's
    driver_options and docker_prune's filters).
    '''
    result = dict()
    if data is not None:
        for k, v in data.items():
            if allow_sequences and is_sequence(v):
                pass
            elif v is True:
                v = 'true'
            elif v is False:
                v = 'false'
            else:
                v = str(v)
            result[str(k)] = v
    return result

@felixfontein
Copy link
Collaborator

@Ajpantuso it's a bit more complicated, since the sequence's values have to (potentially) be adjusted as well. Besides that, it should work.

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

Successfully merging a pull request may close this issue.

4 participants