| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Copyright 2016 Red Hat | Ansible |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: docker_container_info |
| 11 |
|
| 12 |
short_description: Retrieves facts about docker container |
| 13 |
|
| 14 |
description: |
| 15 |
- Retrieves facts about a docker container. |
| 16 |
- Essentially returns the output of C(docker inspect <name>), similar to what M(community.docker.docker_container) returns |
| 17 |
for a non-absent container. |
| 18 |
extends_documentation_fragment: |
| 19 |
- community.docker._docker.api_documentation |
| 20 |
- community.docker._attributes |
| 21 |
- community.docker._attributes.actiongroup_docker |
| 22 |
- community.docker._attributes.info_module |
| 23 |
- community.docker._attributes.idempotent_not_modify_state |
| 24 |
|
| 25 |
options: |
| 26 |
name: |
| 27 |
description: |
| 28 |
- The name of the container to inspect. |
| 29 |
- When identifying an existing container name may be a name or a long or short container ID. |
| 30 |
type: str |
| 31 |
required: true |
| 32 |
|
| 33 |
author: |
| 34 |
- "Felix Fontein (@felixfontein)" |
| 35 |
|
| 36 |
requirements: |
| 37 |
- "Docker API >= 1.25" |
| 38 |
""" |
| 39 |
|
| 40 |
EXAMPLES = r""" |
| 41 |
--- |
| 42 |
- name: Get infos on container |
| 43 |
community.docker.docker_container_info: |
| 44 |
name: mydata |
| 45 |
register: result |
| 46 |
|
| 47 |
- name: Does container exist? |
| 48 |
ansible.builtin.debug: |
| 49 |
msg: "The container {{ 'exists' if result.exists else 'does not exist' }}" |
| 50 |
|
| 51 |
- name: Print information about container |
| 52 |
ansible.builtin.debug: |
| 53 |
var: result.container |
| 54 |
when: result.exists |
| 55 |
""" |
| 56 |
|
| 57 |
RETURN = r""" |
| 58 |
exists: |
| 59 |
description: |
| 60 |
- Returns whether the container exists. |
| 61 |
type: bool |
| 62 |
returned: always |
| 63 |
sample: true |
| 64 |
container: |
| 65 |
description: |
| 66 |
- Facts representing the current state of the container. Matches the docker inspection output. |
| 67 |
- Will be V(none) if container does not exist. |
| 68 |
returned: always |
| 69 |
type: dict |
| 70 |
sample: '{ "AppArmorProfile": "", "Args": [], "Config": { "AttachStderr": false, "AttachStdin": false, "AttachStdout": false, |
| 71 |
"Cmd": [ "/usr/bin/supervisord" ], "Domainname": "", "Entrypoint": null, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" |
| 72 |
], "ExposedPorts": { "443/tcp": {}, "80/tcp": {} }, "Hostname": "8e47bf643eb9", "Image": "lnmp_nginx:v1", "Labels": {}, |
| 73 |
"OnBuild": null, "OpenStdin": false, "StdinOnce": false, "Tty": false, "User": "", "Volumes": { "/tmp/lnmp/nginx-sites/logs/": |
| 74 |
{} }, ... }' |
| 75 |
""" |
| 76 |
|
| 77 |
import traceback |
| 78 |
|
| 79 |
from ansible_collections.community.docker.plugins.module_utils._api.errors import ( |
| 80 |
DockerException, |
| 81 |
) |
| 82 |
from ansible_collections.community.docker.plugins.module_utils._common_api import ( |
| 83 |
AnsibleDockerClient, |
| 84 |
RequestException, |
| 85 |
) |
| 86 |
|
| 87 |
|
| 88 |
def main() -> None: |
| 89 |
argument_spec = { |
| 90 |
"name": {"type": "str", "required": True}, |
| 91 |
} |
| 92 |
|
| 93 |
client = AnsibleDockerClient( |
| 94 |
argument_spec=argument_spec, |
| 95 |
supports_check_mode=True, |
| 96 |
) |
| 97 |
|
| 98 |
container_id: str = client.module.params["name"] |
| 99 |
try: |
| 100 |
container = client.get_container(container_id) |
| 101 |
|
| 102 |
client.module.exit_json( |
| 103 |
changed=False, |
| 104 |
exists=bool(container), |
| 105 |
container=container, |
| 106 |
) |
| 107 |
except DockerException as e: |
| 108 |
client.fail( |
| 109 |
f"An unexpected Docker error occurred: {e}", |
| 110 |
exception=traceback.format_exc(), |
| 111 |
) |
| 112 |
except RequestException as e: |
| 113 |
client.fail( |
| 114 |
f"An unexpected requests error occurred when trying to talk to the Docker daemon: {e}", |
| 115 |
exception=traceback.format_exc(), |
| 116 |
) |
| 117 |
|
| 118 |
|
| 119 |
if __name__ == "__main__": |
| 120 |
main() |