Server IP : 85.214.239.14 / Your IP : 18.220.144.56 Web Server : Apache/2.4.62 (Debian) System : Linux h2886529.stratoserver.net 4.9.0 #1 SMP Mon Sep 30 15:36:27 MSK 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.18 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : OFF | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /usr/lib/python3/dist-packages/ansible_collections/community/network/plugins/modules/ |
Upload File : |
#!/usr/bin/python # -*- coding: utf-8 -*- # (c) 2015, Adam Števko <adam.stevko@gmail.com> # # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) from __future__ import absolute_import, division, print_function __metaclass__ = type DOCUMENTATION = ''' --- module: dladm_vnic short_description: Manage VNICs on Solaris/illumos systems. description: - Create or delete VNICs on Solaris/illumos systems. author: Adam Števko (@xen0l) options: name: description: - VNIC name. required: true type: str link: description: - VNIC underlying link name. required: true type: str temporary: description: - Specifies that the VNIC is temporary. Temporary VNICs do not persist across reboots. required: false default: false type: bool mac: description: - Sets the VNIC's MAC address. Must be valid unicast MAC address. required: false aliases: [ "macaddr" ] type: str vlan: description: - Enable VLAN tagging for this VNIC. The VLAN tag will have id I(vlan). required: false default: false aliases: [ "vlan_id" ] type: int state: description: - Create or delete Solaris/illumos VNIC. required: false default: "present" choices: [ "present", "absent" ] type: str ''' EXAMPLES = ''' - name: Create 'vnic0' VNIC over 'bnx0' link community.network.dladm_vnic: name: vnic0 link: bnx0 state: present - name: Create VNIC with specified MAC and VLAN tag over 'aggr0' community.network.dladm_vnic: name: vnic1 link: aggr0 mac: '00:00:5E:00:53:23' vlan: 4 - name: Remove 'vnic0' VNIC community.network.dladm_vnic: name: vnic0 link: bnx0 state: absent ''' RETURN = ''' name: description: VNIC name returned: always type: str sample: "vnic0" link: description: VNIC underlying link name returned: always type: str sample: "igb0" state: description: state of the target returned: always type: str sample: "present" temporary: description: VNIC's persistence returned: always type: bool sample: "True" mac: description: MAC address to use for VNIC returned: if mac is specified type: str sample: "00:00:5E:00:53:42" vlan: description: VLAN to use for VNIC returned: success type: int sample: 42 ''' import re from ansible.module_utils.basic import AnsibleModule class VNIC(object): UNICAST_MAC_REGEX = r'^[a-f0-9][2-9a-f0]:([a-f0-9]{2}:){4}[a-f0-9]{2}$' def __init__(self, module): self.module = module self.name = module.params['name'] self.link = module.params['link'] self.mac = module.params['mac'] self.vlan = module.params['vlan'] self.temporary = module.params['temporary'] self.state = module.params['state'] def vnic_exists(self): cmd = [self.module.get_bin_path('dladm', True)] cmd.append('show-vnic') cmd.append(self.name) (rc, dummy, dummy) = self.module.run_command(cmd) if rc == 0: return True else: return False def create_vnic(self): cmd = [self.module.get_bin_path('dladm', True)] cmd.append('create-vnic') if self.temporary: cmd.append('-t') if self.mac: cmd.append('-m') cmd.append(self.mac) if self.vlan: cmd.append('-v') cmd.append(self.vlan) cmd.append('-l') cmd.append(self.link) cmd.append(self.name) return self.module.run_command(cmd) def delete_vnic(self): cmd = [self.module.get_bin_path('dladm', True)] cmd.append('delete-vnic') if self.temporary: cmd.append('-t') cmd.append(self.name) return self.module.run_command(cmd) def is_valid_unicast_mac(self): mac_re = re.match(self.UNICAST_MAC_REGEX, self.mac) return mac_re is not None def is_valid_vlan_id(self): return 0 < self.vlan < 4095 def main(): module = AnsibleModule( argument_spec=dict( name=dict(type='str', required=True), link=dict(type='str', required=True), mac=dict(type='str', aliases=['macaddr']), vlan=dict(type='int', aliases=['vlan_id']), temporary=dict(type='bool', default=False), state=dict(type='str', default='present', choices=['absent', 'present']), ), supports_check_mode=True ) vnic = VNIC(module) rc = None out = '' err = '' result = {} result['name'] = vnic.name result['link'] = vnic.link result['state'] = vnic.state result['temporary'] = vnic.temporary if vnic.mac is not None: if not vnic.is_valid_unicast_mac(): module.fail_json(msg='Invalid unicast MAC address', mac=vnic.mac, name=vnic.name, state=vnic.state, link=vnic.link, vlan=vnic.vlan) result['mac'] = vnic.mac if vnic.vlan is not None: if not vnic.is_valid_vlan_id(): module.fail_json(msg='Invalid VLAN tag', mac=vnic.mac, name=vnic.name, state=vnic.state, link=vnic.link, vlan=vnic.vlan) result['vlan'] = vnic.vlan if vnic.state == 'absent': if vnic.vnic_exists(): if module.check_mode: module.exit_json(changed=True) (rc, out, err) = vnic.delete_vnic() if rc != 0: module.fail_json(name=vnic.name, msg=err, rc=rc) elif vnic.state == 'present': if not vnic.vnic_exists(): if module.check_mode: module.exit_json(changed=True) (rc, out, err) = vnic.create_vnic() if rc is not None and rc != 0: module.fail_json(name=vnic.name, msg=err, rc=rc) if rc is None: result['changed'] = False else: result['changed'] = True if out: result['stdout'] = out if err: result['stderr'] = err module.exit_json(**result) if __name__ == '__main__': main()