1

How can I use docker ansible modules to list all containers in a specific network? I would like to accomplish this without using ansible shell commands.

Is this possible?

I found this post which would work if I used shell commands. But again, I dont want to do that. How can I do with docker ansible modules?

Dave
  • 727
  • 1
  • 9
  • 20

1 Answers1

5

You can use the community.docker.docker_network_info module to inspect the network; the returned information includes a list of containers attached to the network.

For example, this playbook will display a list of containers attached to the network name in the docker_network variable:

- hosts: localhost
  gather_facts: false
  vars:
    docker_network: bridge
  collections:
    - community.docker
  tasks:
    - name: "get network info"
      docker_network_info:
        name: "{{ docker_network }}"
      register: net_info

    - name: "get container info"
      docker_container_info:
        name: "{{ item }}"
      register: container_info
      loop: "{{ net_info.network.Containers.keys() }}"

    - debug:
        msg: "{{ item }}"
      loop: "{{ container_info.results|json_query('[].container.Name') }}"
Zeitounator
  • 38,476
  • 7
  • 53
  • 66
larsks
  • 277,717
  • 41
  • 399
  • 399
  • Sorry for late reply on this, this works, however it only lists active containers. Is there a way to list all containers using the network, even if they are in an existed status? – Dave Feb 24 '23 at 15:33
  • Nm, there is no way to do that with these modules. I will accept this as the correct answer. Thanks! – Dave Feb 24 '23 at 16:14