Debian Codegraph @

ansible_collections/community/general/plugins/modules/ip_netns.py

raw
1 #!/usr/bin/python
2 # Copyright (c) 2017, Arie Bregman <[email protected]>
3 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4 # SPDX-License-Identifier: GPL-3.0-or-later
5
6 from __future__ import annotations
7
8 DOCUMENTATION = r"""
9 module: ip_netns
10 author: "Arie Bregman (@bregman-arie)"
11 short_description: Manage network namespaces
12 requirements: [ip]
13 description:
14 - Create or delete network namespaces using the C(ip) command.
15 extends_documentation_fragment:
16 - community.general._attributes
17 attributes:
18 check_mode:
19 support: full
20 diff_mode:
21 support: none
22 options:
23 name:
24 description:
25 - Name of the namespace.
26 type: str
27 state:
28 default: "present"
29 choices: [present, absent]
30 description:
31 - Whether the namespace should exist.
32 type: str
33 """
34
35 EXAMPLES = r"""
36 - name: Create a namespace named mario
37 community.general.ip_netns:
38 name: mario
39 state: present
40
41 - name: Delete a namespace named luigi
42 community.general.ip_netns:
43 name: luigi
44 state: absent
45 """
46
47 RETURN = r"""
48 # Default return values
49 """
50
51 from ansible.module_utils.basic import AnsibleModule
52
53
54 class Namespace:
55 """Interface to network namespaces."""
56
57 def __init__(self, module):
58 self.module = module
59 self.name = module.params["name"]
60 self.state = module.params["state"]
61
62 def _netns(self, command):
63 """Run ip nents command"""
64 return self.module.run_command(["ip", "netns"] + command)
65
66 def exists(self):
67 """Check if the namespace already exists"""
68 rc, out, err = self.module.run_command(["ip", "netns", "list"])
69 if rc != 0:
70 self.module.fail_json(msg=f"{err}")
71 return self.name in out
72
73 def add(self):
74 """Create network namespace"""
75 rtc, out, err = self._netns(["add", self.name])
76
77 if rtc != 0:
78 self.module.fail_json(msg=err)
79
80 def delete(self):
81 """Delete network namespace"""
82 rtc, out, err = self._netns(["del", self.name])
83 if rtc != 0:
84 self.module.fail_json(msg=err)
85
86 def check(self):
87 """Run check mode"""
88 changed = False
89
90 if self.state == "present" and self.exists():
91 changed = True
92
93 elif self.state == "absent" and self.exists():
94 changed = True
95 elif self.state == "present" and not self.exists():
96 changed = True
97
98 self.module.exit_json(changed=changed)
99
100 def run(self):
101 """Make the necessary changes"""
102 changed = False
103
104 if self.state == "absent":
105 if self.exists():
106 self.delete()
107 changed = True
108 elif self.state == "present":
109 if not self.exists():
110 self.add()
111 changed = True
112
113 self.module.exit_json(changed=changed)
114
115
116 def main():
117 """Entry point."""
118 module = AnsibleModule(
119 argument_spec={
120 "name": {"default": None},
121 "state": {"default": "present", "choices": ["present", "absent"]},
122 },
123 supports_check_mode=True,
124 )
125 module.run_command_environ_update = {"LANGUAGE": "C", "LC_ALL": "C"}
126
127 network_namespace = Namespace(module)
128 if module.check_mode:
129 network_namespace.check()
130 else:
131 network_namespace.run()
132
133
134 if __name__ == "__main__":
135 main()