summaryrefslogtreecommitdiffstats
path: root/ansible/library/docker_list_containers.py
blob: d8a789c6bf5354e8ad5f95678ea12d396d186b6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/python

ANSIBLE_METADATA = {
    'metadata_version': '1.1',
    'status': ['preview'],
    'supported_by': 'community'
}

DOCUMENTATION = '''
---
module: docker_list_containers

short_description: "List running docker containers"

description:
    - "Lists all running containers or those with matching label"

options:
    label_name:
        description:
            - container label name to match
        required: false
    label_value:
        description:
            - container label value to match
        required: false

author:
    - Bartek Grzybowski (b.grzybowski@partner.samsung.com)
'''

EXAMPLES = '''
# List all running containers
- name: List containers
  docker_list_containers:

# List all running containers matching label
- name: List containers
  docker_list_containers:
    label_name: 'io.rancher.project.name'
    label_value: 'kubernetes'
'''

RETURN = '''
containers:
    description: List of running containers matching module criteria
    type: list
    returned: always
    sample: [
        "rancher-agent",
        "rancher-server",
        "kubernetes-node-1",
        "infrastructure-server"
    ]
'''

from ansible.module_utils.docker_common import AnsibleDockerClient

class DockerListContainers:

    def __init__(self):
        self.docker_client = AnsibleDockerClient(
            argument_spec=dict(
                label_name=dict(type='str', required=False),
                label_value=dict(type='str', required=False)
            )
        )

        self.containers = self.docker_client.containers()
        self.label_name=self.docker_client.module.params.get('label_name')
        self.label_value=self.docker_client.module.params.get('label_value')

        if self.label_name:
            self.containers_names=self._get_containers_names_by_label()
        else:
            self.containers_names=self._get_containers_names()

        self.result=dict(
            containers=self.containers_names,
            changed=False
        )

    def _get_containers_names(self):
        return [str(container_meta.get('Names')[0][1:]) for container_meta in self.containers if 'Names' in container_meta]

    def _get_containers_names_by_label(self):
        names=[]
        for container_meta in self.containers:
            if container_meta.get('Labels',{}).get(self.label_name) == self.label_value:
                names.append(str(container_meta['Names'][0][1:])) # strip leading '/' in container name and convert to str from unicode

        return names

def main():
    cont=DockerListContainers()
    cont.docker_client.module.exit_json(**cont.result)

if __name__ == '__main__':
    main()