2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
2021-08-08 10:40:22 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2022-08-07 13:37:23 +02:00
|
|
|
# Copyright (c) 2017, Arie Bregman <abregman@redhat.com>
|
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: ip_netns
|
|
|
|
author: "Arie Bregman (@bregman-arie)"
|
|
|
|
short_description: Manage network namespaces
|
|
|
|
requirements: [ ip ]
|
|
|
|
description:
|
|
|
|
- Create or delete network namespaces using the ip command.
|
|
|
|
options:
|
|
|
|
name:
|
|
|
|
required: false
|
|
|
|
description:
|
|
|
|
- Name of the namespace
|
2020-12-01 21:28:40 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
state:
|
|
|
|
required: false
|
|
|
|
default: "present"
|
|
|
|
choices: [ present, absent ]
|
|
|
|
description:
|
|
|
|
- Whether the namespace should exist
|
2020-12-01 21:28:40 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
- name: Create a namespace named mario
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.ip_netns:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: mario
|
|
|
|
state: present
|
2020-04-20 08:40:35 +02:00
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
- name: Delete a namespace named luigi
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.ip_netns:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: luigi
|
|
|
|
state: absent
|
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = '''
|
|
|
|
# Default return values
|
|
|
|
'''
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2021-06-26 23:59:11 +02:00
|
|
|
from ansible.module_utils.common.text.converters import to_text
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Namespace(object):
|
|
|
|
"""Interface to network namespaces. """
|
|
|
|
|
|
|
|
def __init__(self, module):
|
|
|
|
self.module = module
|
|
|
|
self.name = module.params['name']
|
|
|
|
self.state = module.params['state']
|
|
|
|
|
|
|
|
def _netns(self, command):
|
|
|
|
'''Run ip nents command'''
|
|
|
|
return self.module.run_command(['ip', 'netns'] + command)
|
|
|
|
|
|
|
|
def exists(self):
|
|
|
|
'''Check if the namespace already exists'''
|
2021-12-01 07:10:46 +01:00
|
|
|
rc, out, err = self.module.run_command(['ip', 'netns', 'list'])
|
2020-03-09 10:11:07 +01:00
|
|
|
if rc != 0:
|
|
|
|
self.module.fail_json(msg=to_text(err))
|
|
|
|
return self.name in out
|
|
|
|
|
|
|
|
def add(self):
|
|
|
|
'''Create network namespace'''
|
|
|
|
rtc, out, err = self._netns(['add', self.name])
|
|
|
|
|
|
|
|
if rtc != 0:
|
|
|
|
self.module.fail_json(msg=err)
|
|
|
|
|
|
|
|
def delete(self):
|
|
|
|
'''Delete network namespace'''
|
|
|
|
rtc, out, err = self._netns(['del', self.name])
|
|
|
|
if rtc != 0:
|
|
|
|
self.module.fail_json(msg=err)
|
|
|
|
|
|
|
|
def check(self):
|
|
|
|
'''Run check mode'''
|
|
|
|
changed = False
|
|
|
|
|
|
|
|
if self.state == 'present' and self.exists():
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
elif self.state == 'absent' and self.exists():
|
|
|
|
changed = True
|
|
|
|
elif self.state == 'present' and not self.exists():
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
self.module.exit_json(changed=changed)
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
'''Make the necessary changes'''
|
|
|
|
changed = False
|
|
|
|
|
|
|
|
if self.state == 'absent':
|
|
|
|
if self.exists():
|
|
|
|
self.delete()
|
|
|
|
changed = True
|
|
|
|
elif self.state == 'present':
|
|
|
|
if not self.exists():
|
|
|
|
self.add()
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
self.module.exit_json(changed=changed)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
"""Entry point."""
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec={
|
|
|
|
'name': {'default': None},
|
|
|
|
'state': {'default': 'present', 'choices': ['present', 'absent']},
|
|
|
|
},
|
|
|
|
supports_check_mode=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
network_namespace = Namespace(module)
|
|
|
|
if module.check_mode:
|
|
|
|
network_namespace.check()
|
|
|
|
else:
|
|
|
|
network_namespace.run()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|