Debian Codegraph @

ansible_collections/community/docker/plugins/modules/docker_network_info.py

raw
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_network_info
11
12 short_description: Retrieves facts about docker network
13
14 description:
15 - Retrieves facts about a docker network.
16 - Essentially returns the output of C(docker network inspect <name>), similar to what M(community.docker.docker_network)
17 returns for a non-absent network.
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 network to inspect.
29 - When identifying an existing network name may be a name or a long or short network ID.
30 type: str
31 required: true
32
33 author:
34 - "Dave Bendit (@DBendit)"
35
36 requirements:
37 - "Docker API >= 1.25"
38 """
39
40 EXAMPLES = r"""
41 ---
42 - name: Get infos on network
43 community.docker.docker_network_info:
44 name: mydata
45 register: result
46
47 - name: Does network exist?
48 ansible.builtin.debug:
49 msg: "The network {{ 'exists' if result.exists else 'does not exist' }}"
50
51 - name: Print information about network
52 ansible.builtin.debug:
53 var: result.network
54 when: result.exists
55 """
56
57 RETURN = r"""
58 exists:
59 description:
60 - Returns whether the network exists.
61 type: bool
62 returned: always
63 sample: true
64 network:
65 description:
66 - Facts representing the current state of the network. Matches the docker inspection output.
67 - Will be V(none) if network does not exist.
68 returned: always
69 type: dict
70 sample: {
71 "Attachable": false,
72 "ConfigFrom": {"Network": ""},
73 "ConfigOnly": false,
74 "Containers": {},
75 "Created": "2018-12-07T01:47:51.250835114-06:00",
76 "Driver": "bridge",
77 "EnableIPv6": false,
78 "IPAM": {
79 "Config": [
80 {
81 "Gateway": "192.168.96.1",
82 "Subnet": "192.168.96.0/20",
83 },
84 ],
85 "Driver": "default",
86 "Options": null,
87 },
88 "Id": "0856968545f22026c41c2c7c3d448319d3b4a6a03a40b148b3ac4031696d1c0a",
89 "Ingress": false,
90 "Internal": false,
91 "Labels": {},
92 "Name": "ansible-test-f2700bba",
93 "Options": {},
94 "Scope": "local",
95 }
96 """
97
98 import traceback
99
100 from ansible_collections.community.docker.plugins.module_utils._api.errors import (
101 DockerException,
102 )
103 from ansible_collections.community.docker.plugins.module_utils._common_api import (
104 AnsibleDockerClient,
105 RequestException,
106 )
107
108
109 def main() -> None:
110 argument_spec = {
111 "name": {"type": "str", "required": True},
112 }
113
114 client = AnsibleDockerClient(
115 argument_spec=argument_spec,
116 supports_check_mode=True,
117 )
118
119 try:
120 network = client.get_network(client.module.params["name"])
121
122 client.module.exit_json(
123 changed=False,
124 exists=bool(network),
125 network=network,
126 )
127 except DockerException as e:
128 client.fail(
129 f"An unexpected Docker error occurred: {e}",
130 exception=traceback.format_exc(),
131 )
132 except RequestException as e:
133 client.fail(
134 f"An unexpected requests error occurred when trying to talk to the Docker daemon: {e}",
135 exception=traceback.format_exc(),
136 )
137
138
139 if __name__ == "__main__":
140 main()