mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
ae0d3cb090
* Fixed validate-modules:mutually_exclusive-unknown for plugins/modules/packaging/os/redhat_subscription.py * fixed validation-modules for plugins/modules/cloud/lxd/lxd_container.py * fixed validation-modules for plugins/modules/web_infrastructure/sophos_utm/utm_network_interface_address.py * fixed validation-modules for plugins/modules/cloud/opennebula/one_host.py * fixed validation-modules for plugins/modules/cloud/opennebula/one_image_info.py * fixed validation-modules for plugins/modules/cloud/opennebula/one_image.py * fixed validation-modules for plugins/modules/cloud/opennebula/one_service.py * fixed validation-modules for plugins/modules/cloud/opennebula/one_vm.py * fixed validation-modules for plugins/modules/net_tools/cloudflare_dns.py * fixed validation-modules for plugins/modules/net_tools/ip_netns.py * fixed validation-modules for plugins/modules/net_tools/ipinfoio_facts.py * fixed validation-modules for plugins/modules/net_tools/netcup_dns.py * fixed validation-modules for plugins/modules/remote_management/wakeonlan.py * added types to plugins/modules/remote_management/stacki/stacki_host.py but still cannot remove ignore line * added a couple of FIXME comments * fixed validation-modules for plugins/modules/remote_management/manageiq/manageiq_provider.py * fixed validation-modules for plugins/modules/notification/rocketchat.py * fixed validation-modules for plugins/modules/monitoring/bigpanda.py * fixed validation-modules for plugins/modules/identity/keycloak/keycloak_client.py * fixed validation-modules for plugins/modules/identity/keycloak/keycloak_clienttemplate.py * fixed validation-modules for plugins/modules/cloud/univention/udm_user.py * fixed validation-modules for plugins/modules/cloud/univention/udm_group.py * fixed validation-modules for plugins/modules/cloud/spotinst/spotinst_aws_elastigroup.py * fixed validation-modules for plugins/modules/cloud/smartos/imgadm.py * fixed validation-modules for plugins/modules/cloud/profitbricks/profitbricks_nic.py * fixed validation-modules for plugins/modules/cloud/ovirt/ovirt_external_provider_facts.py * Tidy up validate-modules ignores no-default-for-required-parameter + couple of other cases * Added changelog frag * fixed validation-modules for plugins/modules/cloud/centurylink/clc_alert_policy.py * fixed validation-modules for plugins/modules/cloud/centurylink/clc_firewall_policy.py * fixed validation-modules for plugins/modules/cloud/lxd/lxd_profile.py * Typos and small fixes * fixed validation-modules for plugins/modules/net_tools/ldap/ldap_passwd.py * Typos and small fixes, part 2 * Fixes from PR comments * Update plugins/modules/cloud/profitbricks/profitbricks_nic.py Co-authored-by: Felix Fontein <felix@fontein.de> * Rolled back the mutually-exclusive-unknown in redhat_subscription * Update changelogs/fragments/1423-valmod_multiple_cases.yml Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
144 lines
3.8 KiB
Python
144 lines
3.8 KiB
Python
#!/usr/bin/python
|
|
# (c) 2017, Arie Bregman <abregman@redhat.com>
|
|
#
|
|
# This file is a module for Ansible that interacts with Network Manager
|
|
#
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
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
|
|
type: str
|
|
state:
|
|
required: false
|
|
default: "present"
|
|
choices: [ present, absent ]
|
|
description:
|
|
- Whether the namespace should exist
|
|
type: str
|
|
'''
|
|
|
|
EXAMPLES = '''
|
|
- name: Create a namespace named mario
|
|
community.general.ip_netns:
|
|
name: mario
|
|
state: present
|
|
|
|
- name: Delete a namespace named luigi
|
|
community.general.ip_netns:
|
|
name: luigi
|
|
state: absent
|
|
'''
|
|
|
|
RETURN = '''
|
|
# Default return values
|
|
'''
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
from ansible.module_utils._text import to_text
|
|
|
|
|
|
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'''
|
|
rc, out, err = self.module.run_command('ip netns list')
|
|
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()
|