mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Use single parameter for source-interface (#44260)
* Compress src interface into one key * Modified regex to support varied interface types * Fixed documentation * Unpacking return values from splitting method
This commit is contained in:
parent
3960ebb8e8
commit
d63ba5999b
2 changed files with 19 additions and 35 deletions
|
@ -45,13 +45,9 @@ options:
|
||||||
description:
|
description:
|
||||||
- VRF to be used while configuring remote logging (when dest is 'server').
|
- VRF to be used while configuring remote logging (when dest is 'server').
|
||||||
version_added: '2.7'
|
version_added: '2.7'
|
||||||
interface_type:
|
|
||||||
description:
|
|
||||||
- Type of interface to be used when configuring Source-Interface for logging (e.g., 'Ethernet', 'mgmt').
|
|
||||||
version_added: '2.7'
|
|
||||||
interface:
|
interface:
|
||||||
description:
|
description:
|
||||||
- Interface number to be used when configuring Source-Interface for logging (e.g., '1/1', '1/3', '0').
|
- Interface to be used while configuring source-interface for logging (e.g., 'Ethernet1/2', 'mgmt0')
|
||||||
version_added: '2.7'
|
version_added: '2.7'
|
||||||
name:
|
name:
|
||||||
description:
|
description:
|
||||||
|
@ -113,8 +109,7 @@ EXAMPLES = """
|
||||||
state: present
|
state: present
|
||||||
- name: Configure Source Interface for Logging
|
- name: Configure Source Interface for Logging
|
||||||
nxos_logging:
|
nxos_logging:
|
||||||
interface_type: mgmt
|
interface: mgmt0
|
||||||
interface: 0
|
|
||||||
state: present
|
state: present
|
||||||
|
|
||||||
- name: Configure logging using aggregate
|
- name: Configure logging using aggregate
|
||||||
|
@ -140,7 +135,7 @@ commands:
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from ansible.module_utils.network.nxos.nxos import get_config, load_config, run_commands
|
from ansible.module_utils.network.nxos.nxos import get_config, load_config, run_commands
|
||||||
from ansible.module_utils.network.nxos.nxos import nxos_argument_spec, check_args
|
from ansible.module_utils.network.nxos.nxos import nxos_argument_spec, check_args, normalize_interface
|
||||||
from ansible.module_utils.basic import AnsibleModule
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
|
|
||||||
|
|
||||||
|
@ -169,7 +164,7 @@ def map_obj_to_commands(updates):
|
||||||
if w['dest'] == 'server':
|
if w['dest'] == 'server':
|
||||||
commands.append('no logging server {}'.format(w['remote_server']))
|
commands.append('no logging server {}'.format(w['remote_server']))
|
||||||
|
|
||||||
if w['interface_type']:
|
if w['interface']:
|
||||||
commands.append('no logging source-interface')
|
commands.append('no logging source-interface')
|
||||||
|
|
||||||
if state == 'present' and w not in have:
|
if state == 'present' and w not in have:
|
||||||
|
@ -217,12 +212,18 @@ def map_obj_to_commands(updates):
|
||||||
commands.append('logging level {} {}'.format(w['facility'],
|
commands.append('logging level {} {}'.format(w['facility'],
|
||||||
w['facility_level']))
|
w['facility_level']))
|
||||||
|
|
||||||
if w['interface_type']:
|
if w['interface']:
|
||||||
commands.append('logging source-interface {0} {1}'.format(w['interface_type'],
|
commands.append('logging source-interface {0} {1}'.format(*split_interface(w['interface'])))
|
||||||
w['interface']))
|
|
||||||
return commands
|
return commands
|
||||||
|
|
||||||
|
|
||||||
|
def split_interface(interface):
|
||||||
|
match = re.search(r'(\D+)(\S*)', interface, re.M)
|
||||||
|
if match:
|
||||||
|
return match.group(1), match.group(2)
|
||||||
|
|
||||||
|
|
||||||
def parse_name(line, dest):
|
def parse_name(line, dest):
|
||||||
name = None
|
name = None
|
||||||
|
|
||||||
|
@ -315,20 +316,10 @@ def parse_use_vrf(line, dest):
|
||||||
return use_vrf
|
return use_vrf
|
||||||
|
|
||||||
|
|
||||||
def parse_interface_type(line):
|
|
||||||
interface_type = None
|
|
||||||
|
|
||||||
match = re.search(r'logging source-interface (\D+)', line, re.M)
|
|
||||||
if match:
|
|
||||||
interface_type = match.group(1)
|
|
||||||
|
|
||||||
return interface_type
|
|
||||||
|
|
||||||
|
|
||||||
def parse_interface(line):
|
def parse_interface(line):
|
||||||
interface = None
|
interface = None
|
||||||
|
|
||||||
match = re.search(r'logging source-interface (?:\D+)(\d*([/]?\d+))', line, re.M)
|
match = re.search(r'logging source-interface (\S*)', line, re.M)
|
||||||
if match:
|
if match:
|
||||||
interface = match.group(1)
|
interface = match.group(1)
|
||||||
|
|
||||||
|
@ -367,7 +358,6 @@ def map_config_to_obj(module):
|
||||||
'facility': facility,
|
'facility': facility,
|
||||||
'dest_level': parse_dest_level(line, dest, parse_name(line, dest)),
|
'dest_level': parse_dest_level(line, dest, parse_name(line, dest)),
|
||||||
'facility_level': parse_facility_level(line, facility, dest),
|
'facility_level': parse_facility_level(line, facility, dest),
|
||||||
'interface_type': parse_interface_type(line),
|
|
||||||
'interface': parse_interface(line)})
|
'interface': parse_interface(line)})
|
||||||
|
|
||||||
cmd = [{'command': 'show logging | section enabled | section console', 'output': 'text'},
|
cmd = [{'command': 'show logging | section enabled | section console', 'output': 'text'},
|
||||||
|
@ -393,7 +383,6 @@ def map_config_to_obj(module):
|
||||||
'dest_level': dest_level,
|
'dest_level': dest_level,
|
||||||
'facility_level': None,
|
'facility_level': None,
|
||||||
'use_vrf': None,
|
'use_vrf': None,
|
||||||
'interface_type': None,
|
|
||||||
'interface': None})
|
'interface': None})
|
||||||
|
|
||||||
return obj
|
return obj
|
||||||
|
@ -410,7 +399,6 @@ def map_params_to_obj(module):
|
||||||
'facility': '',
|
'facility': '',
|
||||||
'dest_level': '',
|
'dest_level': '',
|
||||||
'facility_level': '',
|
'facility_level': '',
|
||||||
'interface_type': '',
|
|
||||||
'interface': ''}
|
'interface': ''}
|
||||||
|
|
||||||
for c in module.params['aggregate']:
|
for c in module.params['aggregate']:
|
||||||
|
@ -426,8 +414,8 @@ def map_params_to_obj(module):
|
||||||
if d['facility_level'] is not None:
|
if d['facility_level'] is not None:
|
||||||
d['facility_level'] = str(d['facility_level'])
|
d['facility_level'] = str(d['facility_level'])
|
||||||
|
|
||||||
if d['interface_type']:
|
if d['interface']:
|
||||||
d['interface'] = str(d['interface'])
|
d['interface'] = normalize_interface(d['interface'])
|
||||||
|
|
||||||
if 'state' not in d:
|
if 'state' not in d:
|
||||||
d['state'] = module.params['state']
|
d['state'] = module.params['state']
|
||||||
|
@ -452,8 +440,7 @@ def map_params_to_obj(module):
|
||||||
'facility': module.params['facility'],
|
'facility': module.params['facility'],
|
||||||
'dest_level': dest_level,
|
'dest_level': dest_level,
|
||||||
'facility_level': facility_level,
|
'facility_level': facility_level,
|
||||||
'interface_type': module.params['interface_type'],
|
'interface': normalize_interface(module.params['interface']),
|
||||||
'interface': module.params['interface'],
|
|
||||||
'state': module.params['state']
|
'state': module.params['state']
|
||||||
})
|
})
|
||||||
return obj
|
return obj
|
||||||
|
@ -470,7 +457,6 @@ def main():
|
||||||
use_vrf=dict(),
|
use_vrf=dict(),
|
||||||
dest_level=dict(type='int', aliases=['level']),
|
dest_level=dict(type='int', aliases=['level']),
|
||||||
facility_level=dict(type='int'),
|
facility_level=dict(type='int'),
|
||||||
interface_type=dict(),
|
|
||||||
interface=dict(),
|
interface=dict(),
|
||||||
state=dict(default='present', choices=['present', 'absent']),
|
state=dict(default='present', choices=['present', 'absent']),
|
||||||
aggregate=dict(type='list')
|
aggregate=dict(type='list')
|
||||||
|
@ -483,7 +469,6 @@ def main():
|
||||||
|
|
||||||
module = AnsibleModule(argument_spec=argument_spec,
|
module = AnsibleModule(argument_spec=argument_spec,
|
||||||
required_if=required_if,
|
required_if=required_if,
|
||||||
required_together=[['interface_type', 'interface']],
|
|
||||||
supports_check_mode=True)
|
supports_check_mode=True)
|
||||||
|
|
||||||
warnings = list()
|
warnings = list()
|
||||||
|
|
|
@ -194,8 +194,7 @@
|
||||||
|
|
||||||
- name: Configure Source Interface for Logging
|
- name: Configure Source Interface for Logging
|
||||||
nxos_logging: &srcint
|
nxos_logging: &srcint
|
||||||
interface_type: mgmt
|
interface: mgmt0
|
||||||
interface: 0
|
|
||||||
register: result
|
register: result
|
||||||
|
|
||||||
- assert:
|
- assert:
|
||||||
|
@ -220,7 +219,7 @@
|
||||||
- { dest: logfile, dest_level: 1, name: test }
|
- { dest: logfile, dest_level: 1, name: test }
|
||||||
- { facility: daemon, facility_level: 4 }
|
- { facility: daemon, facility_level: 4 }
|
||||||
- { dest: server, remote_server: test-syslogserver.com, facility: auth, facility_level: 1, use_vrf: management }
|
- { dest: server, remote_server: test-syslogserver.com, facility: auth, facility_level: 1, use_vrf: management }
|
||||||
- { interface_type: mgmt, interface: 0 }
|
- { interface: mgmt0 }
|
||||||
provider: "{{ connection }}"
|
provider: "{{ connection }}"
|
||||||
state: absent
|
state: absent
|
||||||
register: result
|
register: result
|
||||||
|
|
Loading…
Reference in a new issue