Debian Codegraph @

ansible_collections/containers/podman/plugins/modules/podman_network_info.py

raw
1 #!/usr/bin/python
2 # Copyright (c) 2020 Red Hat
3 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
4
5 from __future__ import absolute_import, division, print_function
6
7 __metaclass__ = type
8
9
10 DOCUMENTATION = r"""
11 module: podman_network_info
12 author:
13 - "Sagi Shnaidman (@sshnaidm)"
14 version_added: '1.0.0'
15 short_description: Gather info about podman networks
16 notes: []
17 description:
18 - Gather info about podman networks with podman inspect command.
19 requirements:
20 - "Podman installed on host"
21 options:
22 name:
23 description:
24 - Name of the network
25 type: str
26 executable:
27 description:
28 - Path to C(podman) executable if it is not in the C($PATH) on the
29 machine running C(podman)
30 default: 'podman'
31 type: str
32 """
33
34 EXAMPLES = r"""
35 - name: Gather info about all present networks
36 containers.podman.podman_network_info:
37
38 - name: Gather info about specific network
39 containers.podman.podman_network_info:
40 name: podman
41 """
42
43 RETURN = r"""
44 networks:
45 description: Facts from all or specified networks
46 returned: always
47 type: list
48 sample: [
49 {
50 "name": "dmz",
51 "id": "3227f9785ae4657c022c8da7b0e04d2d124199e66da10a9130437e3c3f0e0e42",
52 "driver": "macvlan",
53 "created": "2024-05-27T21:09:03.486699659+03:00",
54 "subnets": [
55 {
56 "subnet": "10.10.0.0/24",
57 "gateway": "10.10.0.1",
58 "lease_range": {
59 "start_ip": "10.10.0.249",
60 "end_ip": "10.10.0.255"
61 }
62 },
63 {
64 "subnet": "2001:db8:abcd:10::/64",
65 "gateway": "2001:db8:abcd:10::1"
66 }
67 ],
68 "ipv6_enabled": true,
69 "internal": false,
70 "dns_enabled": false,
71 "options": {
72 "no_default_route": "true"
73 },
74 "ipam_options": {
75 "driver": "host-local"
76 }
77 }
78 ]
79 """
80
81 import json
82 from ansible.module_utils.basic import AnsibleModule
83
84
85 def get_network_info(module, executable, name):
86 command = [executable, "network", "inspect"]
87 if not name:
88 all_names = [executable, "network", "ls", "-q"]
89 rc, out, err = module.run_command(all_names)
90 if rc != 0:
91 module.fail_json(msg="Unable to get list of networks: %s" % err)
92 name = out.split()
93 if not name:
94 return [], out, err
95 command += name
96 else:
97 command.append(name)
98 rc, out, err = module.run_command(command)
99 if rc != 0 or "unable to find network configuration" in err:
100 module.fail_json(msg="Unable to gather info for %s: %s" % (name, err))
101 if not out or json.loads(out) is None:
102 return [], out, err
103 return json.loads(out), out, err
104
105
106 def main():
107 module = AnsibleModule(
108 argument_spec=dict(executable=dict(type="str", default="podman"), name=dict(type="str")),
109 supports_check_mode=True,
110 )
111
112 name = module.params["name"]
113 executable = module.get_bin_path(module.params["executable"], required=True)
114
115 inspect_results, out, err = get_network_info(module, executable, name)
116
117 results = {"changed": False, "networks": inspect_results, "stderr": err}
118
119 module.exit_json(**results)
120
121
122 if __name__ == "__main__":
123 main()