diff --git a/changelogs/fragments/t8983_logging_global_15.yml b/changelogs/fragments/t8983_logging_global_15.yml new file mode 100644 index 00000000..66b24bc1 --- /dev/null +++ b/changelogs/fragments/t8983_logging_global_15.yml @@ -0,0 +1,3 @@ +--- +bugfixes: + - vyos_logging_global - Add support for 1.5.0+ CLI changes. diff --git a/docs/vyos.vyos.vyos_logging_global_module.rst b/docs/vyos.vyos.vyos_logging_global_module.rst index 71b77eb8..ef609a3d 100644 --- a/docs/vyos.vyos.vyos_logging_global_module.rst +++ b/docs/vyos.vyos.vyos_logging_global_module.rst @@ -1,1670 +1,1807 @@ .. _vyos.vyos.vyos_logging_global_module: ***************************** vyos.vyos.vyos_logging_global ***************************** **Logging resource module** Version added: 2.4.0 .. contents:: :local: :depth: 1 Synopsis -------- - This module manages the logging attributes of Vyos network devices Parameters ---------- .. raw:: html
Parameter Choices/Defaults Comments
config
dictionary
A list containing dictionary of logging options
console
dictionary
logging to serial console
facilities
list / elements=dictionary
facility configurations for console
facility
string
    Choices:
  • all
  • auth
  • authpriv
  • cron
  • daemon
  • kern
  • lpr
  • mail
  • mark
  • news
  • protocols
  • security
  • syslog
  • user
  • uucp
  • local0
  • local1
  • local2
  • local3
  • local4
  • local5
  • local6
  • local7
Facility for logging
severity
string
    Choices:
  • emerg
  • alert
  • crit
  • err
  • warning
  • notice
  • info
  • debug
  • all
logging level
state
string
    Choices:
  • enabled
  • disabled
enable or disable the command
files
list / elements=dictionary
-
logging to file
+
Logging to file. Not supported on VyOS 1.5+. If provided on a 1.5+ device, this option will be ignored with a warning.
archive
dictionary
Log file size and rotation characteristics
file_num
integer
Number of saved files (default is 5)
size
integer
Size of log files (in kilobytes, default is 256)
state
string
    Choices:
  • enabled
  • disabled
enable or disable the command
facilities
list / elements=dictionary
facility configurations
facility
string
    Choices:
  • all
  • auth
  • authpriv
  • cron
  • daemon
  • kern
  • lpr
  • mail
  • mark
  • news
  • protocols
  • security
  • syslog
  • user
  • uucp
  • local0
  • local1
  • local2
  • local3
  • local4
  • local5
  • local6
  • local7
Facility for logging
severity
string
    Choices:
  • emerg
  • alert
  • crit
  • err
  • warning
  • notice
  • info
  • debug
  • all
logging level
path
string
file name or path
global_params
dictionary
-
logging to serial console
+
Global logging parameters. On VyOS 1.5+, facilities map to set system syslog local, and marker_interval/preserve_fqdn move to top-level CLI paths. The archive suboption is not supported on VyOS 1.5+ and will be ignored with a warning.
archive
dictionary
Log file size and rotation characteristics
file_num
integer
Number of saved files (default is 5)
size
integer
Size of log files (in kilobytes, default is 256)
state
string
    Choices:
  • enabled
  • disabled
enable or disable the command
facilities
list / elements=dictionary
facility configurations
facility
string
    Choices:
  • all
  • auth
  • authpriv
  • cron
  • daemon
  • kern
  • lpr
  • mail
  • mark
  • news
  • protocols
  • security
  • syslog
  • user
  • uucp
  • local0
  • local1
  • local2
  • local3
  • local4
  • local5
  • local6
  • local7
Facility for logging
severity
string
    Choices:
  • emerg
  • alert
  • crit
  • err
  • warning
  • notice
  • info
  • debug
  • all
logging level
marker_interval
integer
time interval how often a mark message is being sent in seconds (default is 1200)
preserve_fqdn
boolean
    Choices:
  • no
  • yes
uses FQDN for logging
state
string
    Choices:
  • enabled
  • disabled
enable or disable the command
hosts
list / elements=dictionary
-
logging to serial console
+
Logging to remote hosts. On VyOS 1.5+, maps to set system syslog remote (was set system syslog host).
facilities
list / elements=dictionary
facility configurations for host
facility
string
    Choices:
  • all
  • auth
  • authpriv
  • cron
  • daemon
  • kern
  • lpr
  • mail
  • mark
  • news
  • protocols
  • security
  • syslog
  • user
  • uucp
  • local0
  • local1
  • local2
  • local3
  • local4
  • local5
  • local6
  • local7
Facility for logging
protocol
string
    Choices:
  • udp
  • tcp
syslog communication protocol. Version 1.3 and below.
severity
string
    Choices:
  • emerg
  • alert
  • crit
  • err
  • warning
  • notice
  • info
  • debug
  • all
logging level
hostname
string
Remote host name or IP address
port
integer
Destination port (1-65535)
protocol
string
    Choices:
  • udp
  • tcp
syslog communication protocol. Version 1.4+
syslog
dictionary
logging syslog
state
string
    Choices:
  • enabled
  • disabled
enable or disable the command
users
list / elements=dictionary
-
logging to file
+
Logging to a local user terminal. Not supported on VyOS 1.5+. If provided on a 1.5+ device, this option will be ignored with a warning.
facilities
list / elements=dictionary
facility configurations
facility
string
    Choices:
  • all
  • auth
  • authpriv
  • cron
  • daemon
  • kern
  • lpr
  • mail
  • mark
  • news
  • protocols
  • security
  • syslog
  • user
  • uucp
  • local0
  • local1
  • local2
  • local3
  • local4
  • local5
  • local6
  • local7
Facility for logging
severity
string
    Choices:
  • emerg
  • alert
  • crit
  • err
  • warning
  • notice
  • info
  • debug
  • all
logging level
username
string
user login name
running_config
string
This option is used only with state parsed.
The value of this option should be the output received from the VYOS device by executing the command show configuration commands | grep syslog.
The state parsed reads the configuration from running_config option and transforms it into Ansible structured data as per the resource module's argspec and the value is then returned in the parsed key within the result.
state
string
    Choices:
  • merged ←
  • replaced
  • overridden
  • deleted
  • gathered
  • parsed
  • rendered
The state the configuration should be left in
The states replaced and overridden have identical behaviour for this module.
Refer to examples for more details.

Notes ----- .. note:: - - Tested against VyOS 1.3.8, 1.4.2, the upcoming 1.5, and the rolling release of spring 2025 + - Tested against VyOS 1.3.8, 1.4.2, 1.5.0, and the rolling release of spring 2025. - This module works with connection ``network_cli``. - - The Configuration defaults of the Vyos network devices are supposed to hinder idempotent behavior of plays + - The Configuration defaults of the VyOS network devices are supposed to hinder idempotent behavior of plays. + - **VyOS 1.5+ breaking changes**: The ``files``, ``users``, and ``global_params.archive`` options are not supported on VyOS 1.5+. If provided, they will be ignored with a warning. The ``global_params`` facilities now map to ``set system syslog local`` (was ``set system syslog global``). Remote hosts now map to ``set system syslog remote`` (was ``set system syslog host``). The ``marker_interval`` and ``preserve_fqdn`` options moved to top-level (was under ``global_params`` in the CLI, argspec key unchanged). + Examples -------- .. code-block:: yaml # Using state: merged # Before state: # ------------- # vyos:~$show configuration commands | grep syslog - name: Apply the provided configuration vyos.vyos.vyos_logging_global: config: console: facilities: - facility: local7 severity: err files: - path: logFile archive: file_num: 2 facilities: - facility: local6 severity: emerg hosts: - hostname: 172.16.0.1 facilities: - facility: local7 severity: all - facility: all protocol: udp port: 223 users: - username: vyos facilities: - facility: local7 severity: debug global_params: archive: file_num: 2 size: 111 facilities: - facility: cron severity: debug marker_interval: 111 preserve_fqdn: true state: merged # Commands Fired: # --------------- # "commands": [ # "set system syslog console facility local7 level err", # "set system syslog file logFile archive file 2", # "set system syslog host 172.16.0.1 facility local7 level all", # "set system syslog file logFile facility local6 level emerg", # "set system syslog host 172.16.0.1 facility all protocol udp", # "set system syslog user vyos facility local7 level debug", # "set system syslog host 172.16.0.1 port 223", # "set system syslog global facility cron level debug", # "set system syslog global archive file 2", # "set system syslog global archive size 111", # "set system syslog global marker interval 111", # "set system syslog global preserve-fqdn" # ], # After state: # ------------ # vyos:~$ show configuration commands | grep syslog # set system syslog console facility local7 level 'err' # set system syslog file logFile archive file '2' # set system syslog file logFile facility local6 level 'emerg' # set system syslog global archive file '2' # set system syslog global archive size '111' # set system syslog global facility cron level 'debug' # set system syslog global marker interval '111' # set system syslog global preserve-fqdn # set system syslog host 172.16.0.1 facility all protocol 'udp' # set system syslog host 172.16.0.1 facility local7 level 'all' # set system syslog host 172.16.0.1 port '223' # set system syslog user vyos facility local7 level 'debug' # Using state: deleted # Before state: # ------------- # vyos:~$show configuration commands | grep syslog # set system syslog console facility local7 level 'err' # set system syslog file logFile archive file '2' # set system syslog file logFile facility local6 level 'emerg' # set system syslog global archive file '2' # set system syslog global archive size '111' # set system syslog global facility cron level 'debug' # set system syslog global marker interval '111' # set system syslog global preserve-fqdn # set system syslog host 172.16.0.1 facility all protocol 'udp' # set system syslog host 172.16.0.1 facility local7 level 'all' # set system syslog host 172.16.0.1 port '223' # set system syslog user vyos facility local7 level 'debug' - name: delete the existing configuration vyos.vyos.vyos_logging_global: state: deleted # Commands Fired: # --------------- # "commands": [ # "delete system syslog" # ], # After state: # ------------ # vyos:~$show configuration commands | grep syslog # Using state: overridden # Before state: # ------------- # vyos:~$show configuration commands | grep syslog # set system syslog console facility local7 level 'err' # set system syslog file logFile archive file '2' # set system syslog file logFile facility local6 level 'emerg' # set system syslog global archive file '2' # set system syslog global archive size '111' # set system syslog global facility cron level 'debug' # set system syslog global marker interval '111' # set system syslog global preserve-fqdn # set system syslog host 172.16.0.1 facility all protocol 'udp' # set system syslog host 172.16.0.1 facility local7 level 'all' # set system syslog host 172.16.0.1 port '223' # set system syslog user vyos facility local7 level 'debug' - name: Override the current configuration vyos.vyos.vyos_logging_global: config: console: facilities: - facility: all - facility: local7 severity: err - facility: news severity: debug files: - path: logFileNew hosts: - hostname: 172.16.0.2 facilities: - facility: local5 severity: all global_params: archive: file_num: 10 state: overridden # Commands Fired: # --------------- # "commands": [ # "delete system syslog file logFile", # "delete system syslog global facility cron", # "delete system syslog host 172.16.0.1", # "delete system syslog user vyos", # "set system syslog console facility all", # "set system syslog console facility news level debug", # "set system syslog file logFileNew", # "set system syslog host 172.16.0.2 facility local5 level all", # "set system syslog global archive file 10", # "delete system syslog global archive size 111", # "delete system syslog global marker", # "delete system syslog global preserve-fqdn" # ], # After state: # ------------ # vyos:~$show configuration commands | grep syslog # set system syslog console facility all # set system syslog console facility local7 level 'err' # set system syslog console facility news level 'debug' # set system syslog file logFileNew # set system syslog global archive file '10' # set system syslog host 172.16.0.2 facility local5 level 'all' # Using state: replaced # Before state: # ------------- # vyos:~$show configuration commands | grep syslog # set system syslog console facility all # set system syslog console facility local7 level 'err' # set system syslog console facility news level 'debug' # set system syslog file logFileNew # set system syslog global archive file '10' # set system syslog host 172.16.0.2 facility local5 level 'all' - name: Replace with the provided configuration register: result vyos.vyos.vyos_logging_global: config: console: facilities: - facility: local6 users: - username: paul facilities: - facility: local7 severity: err state: replaced # Commands Fired: # --------------- # "commands": [ # "delete system syslog console facility all", # "delete system syslog console facility local7", # "delete system syslog console facility news", # "delete system syslog file logFileNew", # "delete system syslog global archive file 10", # "delete system syslog host 172.16.0.2", # "set system syslog console facility local6", # "set system syslog user paul facility local7 level err" # ], # After state: # ------------ # vyos:~$show configuration commands | grep syslog # set system syslog console facility local6 # set system syslog user paul facility local7 level 'err' # Using state: gathered - name: Gather logging config vyos.vyos.vyos_logging_global: state: gathered # Module Execution Result: # ------------------------ # "gathered": { # "console": { # "facilities": [ # { # "facility": "local6" # }, # { # "facility": "local7", # "severity": "err" # } # ] # }, # "files": [ # { # "archive": { # "file_num": 2 # }, # "facilities": [ # { # "facility": "local6", # "severity": "emerg" # } # ], # "path": "logFile" # } # ], # "global_params": { # "archive": { # "file_num": 2, # "size": 111 # }, # "facilities": [ # { # "facility": "cron", # "severity": "debug" # } # ], # "marker_interval": 111, # "preserve_fqdn": true # }, # "hosts": [ # { # "facilities": [ # { # "facility": "all", # "protocol": "udp" # }, # { # "facility": "local7", # "severity": "all" # } # ], # "hostname": "172.16.0.1", # "port": 223 # } # ], # "users": [ # { # "facilities": [ # { # "facility": "local7", # "severity": "err" # } # ], # "username": "paul" # }, # { # "facilities": [ # { # "facility": "local7", # "severity": "debug" # } # ], # "username": "vyos" # } # ] # }, # After state: # ------------ # vyos:~$show configuration commands | grep syslog # set system syslog console facility local6 # set system syslog console facility local7 level 'err' # set system syslog file logFile archive file '2' # set system syslog file logFile facility local6 level 'emerg' # set system syslog global archive file '2' # set system syslog global archive size '111' # set system syslog global facility cron level 'debug' # set system syslog global marker interval '111' # set system syslog global preserve-fqdn # set system syslog host 172.16.0.1 facility all protocol 'udp' # set system syslog host 172.16.0.1 facility local7 level 'all' # set system syslog host 172.16.0.1 port '223' # set system syslog user paul facility local7 level 'err' # set system syslog user vyos facility local7 level 'debug' # Using state: rendered - name: Render the provided configuration vyos.vyos.vyos_logging_global: config: console: facilities: - facility: local7 severity: err files: - path: logFile archive: file_num: 2 facilities: - facility: local6 severity: emerg hosts: - hostname: 172.16.0.1 facilities: - facility: local7 severity: all - facility: all protocol: udp port: 223 users: - username: vyos facilities: - facility: local7 severity: debug global_params: archive: file_num: 2 size: 111 facilities: - facility: cron severity: debug marker_interval: 111 preserve_fqdn: true state: rendered # Module Execution Result: # ------------------------ # "rendered": [ # "set system syslog console facility local7 level err", # "set system syslog file logFile facility local6 level emerg", # "set system syslog file logFile archive file 2", # "set system syslog host 172.16.0.1 facility local7 level all", # "set system syslog host 172.16.0.1 facility all protocol udp", # "set system syslog host 172.16.0.1 port 223", # "set system syslog user vyos facility local7 level debug", # "set system syslog global facility cron level debug", # "set system syslog global archive file 2", # "set system syslog global archive size 111", # "set system syslog global marker interval 111", # "set system syslog global preserve-fqdn" # ] # Using state: parsed # File: parsed.cfg # ---------------- # set system syslog console facility local6 # set system syslog console facility local7 level 'err' # set system syslog file logFile archive file '2' # set system syslog file logFile facility local6 level 'emerg' # set system syslog global archive file '2' # set system syslog global archive size '111' # set system syslog global facility cron level 'debug' # set system syslog global marker interval '111' # set system syslog global preserve-fqdn # set system syslog host 172.16.0.1 facility all protocol 'udp' # set system syslog host 172.16.0.1 facility local7 level 'all' # set system syslog host 172.16.0.1 port '223' # set system syslog user paul facility local7 level 'err' # set system syslog user vyos facility local7 level 'debug' - name: Parse the provided configuration vyos.vyos.vyos_logging_global: running_config: "{{ lookup('file', 'parsed_vyos.cfg') }}" state: parsed # Module Execution Result: # ------------------------ # "parsed": { # "console": { # "facilities": [ # { # "facility": "local6" # }, # { # "facility": "local7", # "severity": "err" # } # ] # }, # "files": [ # { # "archive": { # "file_num": 2 # }, # "facilities": [ # { # "facility": "local6", # "severity": "emerg" # } # ], # "path": "logFile" # } # ], # "global_params": { # "archive": { # "file_num": 2, # "size": 111 # }, # "facilities": [ # { # "facility": "cron", # "severity": "debug" # } # ], # "marker_interval": 111, # "preserve_fqdn": true # }, # "hosts": [ # { # "facilities": [ # { # "facility": "all", # "protocol": "udp" # }, # { # "facility": "local7", # "severity": "all" # } # ], # "hostname": "172.16.0.1", # "port": 223 # } # ], # "users": [ # { # "facilities": [ # { # "facility": "local7", # "severity": "err" # } # ], # "username": "paul" # }, # { # "facilities": [ # { # "facility": "local7", # "severity": "debug" # } # ], # "username": "vyos" # } # ] # } # } + # Using state: merged (VyOS 1.5+) + + # Before state: + # ------------- + + # vyos:~$ show configuration commands | grep syslog + + - name: Apply the provided configuration (VyOS 1.5+) + vyos.vyos.vyos_logging_global: + config: + console: + facilities: + - facility: local7 + severity: err + hosts: + - hostname: 172.16.0.1 + facilities: + - facility: local7 + severity: all + port: 514 + protocol: udp + global_params: + facilities: + - facility: cron + severity: debug + marker_interval: 111 + preserve_fqdn: true + state: merged + + # Commands Fired: + # --------------- + + # "commands": [ + # "set system syslog console facility local7 level err", + # "set system syslog remote 172.16.0.1 facility local7 level all", + # "set system syslog remote 172.16.0.1 port 514", + # "set system syslog remote 172.16.0.1 protocol udp", + # "set system syslog local facility cron level debug", + # "set system syslog marker interval 111", + # "set system syslog preserve-fqdn" + # ], + + # After state: + # ------------ + + # vyos:~$ show configuration commands | grep syslog + # set system syslog console facility local7 level 'err' + # set system syslog local facility cron level 'debug' + # set system syslog marker interval '111' + # set system syslog preserve-fqdn + # set system syslog remote 172.16.0.1 facility local7 level 'all' + # set system syslog remote 172.16.0.1 port '514' + # set system syslog remote 172.16.0.1 protocol 'udp' + + # Using state: gathered (VyOS 1.5+) + + - name: Gather logging config (VyOS 1.5+) + vyos.vyos.vyos_logging_global: + state: gathered + + # Module Execution Result: + # ------------------------ + + # "gathered": { + # "console": { + # "facilities": [ + # { + # "facility": "local7", + # "severity": "err" + # } + # ] + # }, + # "global_params": { + # "facilities": [ + # { + # "facility": "cron", + # "severity": "debug" + # } + # ], + # "marker_interval": 111, + # "preserve_fqdn": true + # }, + # "hosts": [ + # { + # "facilities": [ + # { + # "facility": "local7", + # "severity": "all" + # } + # ], + # "hostname": "172.16.0.1", + # "port": 514, + # "protocol": "udp" + # } + # ] + # }, + + # Using state: rendered (VyOS 1.5+) + # Note: files, users, and global_params.archive are not supported on VyOS 1.5+ + # and will be ignored with a warning if provided. + + - name: Render the provided configuration (VyOS 1.5+) + vyos.vyos.vyos_logging_global: + config: + console: + facilities: + - facility: local7 + severity: err + hosts: + - hostname: 172.16.0.1 + facilities: + - facility: local7 + severity: all + port: 514 + protocol: udp + global_params: + facilities: + - facility: cron + severity: debug + marker_interval: 111 + preserve_fqdn: true + state: rendered + + # Module Execution Result: + # ------------------------ + + # "rendered": [ + # "set system syslog console facility local7 level err", + # "set system syslog remote 172.16.0.1 facility local7 level all", + # "set system syslog remote 172.16.0.1 port 514", + # "set system syslog remote 172.16.0.1 protocol udp", + # "set system syslog local facility cron level debug", + # "set system syslog marker interval 111", + # "set system syslog preserve-fqdn" + # ] Return Values ------------- Common return values are documented `here `_, the following are the fields unique to this module: .. raw:: html
Key Returned Description
after
dictionary
when changed
The resulting configuration after module execution.

Sample:
This output will always be in the same format as the module argspec.
before
dictionary
when state is merged, replaced, overridden, deleted or purged
The configuration prior to the module execution.

Sample:
This output will always be in the same format as the module argspec.
commands
list
when state is merged, replaced, overridden, deleted or purged
The set of commands pushed to the remote device.

Sample:
['set system syslog console facility local7 level err', 'set system syslog host 172.16.0.1 port 223', 'set system syslog global archive size 111']
gathered
list
when state is gathered
Facts about the network resource gathered from the remote device as structured data.

Sample:
This output will always be in the same format as the module argspec.
parsed
list
when state is parsed
The device native config provided in running_config option parsed into structured data as per module argspec.

Sample:
This output will always be in the same format as the module argspec.
rendered
list
when state is rendered
The provided configuration in the task rendered in device-native format (offline).

Sample:
['set system syslog host 172.16.0.1 port 223', 'set system syslog user vyos facility local7 level debug', 'set system syslog global facility cron level debu']


Status ------ Authors ~~~~~~~ - Sagar Paul (@KB-perByte) diff --git a/plugins/module_utils/network/vyos/config/logging_global/logging_global.py b/plugins/module_utils/network/vyos/config/logging_global/logging_global.py index 35a2624a..73828109 100644 --- a/plugins/module_utils/network/vyos/config/logging_global/logging_global.py +++ b/plugins/module_utils/network/vyos/config/logging_global/logging_global.py @@ -1,222 +1,257 @@ # # -*- coding: utf-8 -*- # Copyright 2021 Red Hat # 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 """ The vyos_logging_global config file. It is in this file where the current configuration (as dict) is compared to the provided configuration (as dict) and the command set necessary to bring the current configuration to its desired end-state is created. """ from copy import deepcopy from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.rm_base.resource_module import ( ResourceModule, ) from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( dict_merge, get_from_dict, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.facts import Facts from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.rm_templates.logging_global import ( Logging_globalTemplate, ) +from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.rm_templates.logging_global_15 import ( + Logging_globalTemplate15, +) +from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.utils.version import ( + LooseVersion, +) +from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.vyos import get_os_version class Logging_global(ResourceModule): """ The vyos_logging_global config class """ def __init__(self, module): super(Logging_global, self).__init__( empty_fact_val={}, facts_module=Facts(module), module=module, resource="logging_global", tmplt=Logging_globalTemplate(), ) - self.parsers = [ - "console.facilities", - "global_params.archive.file_num", - "global_params.archive.size", - "global_params.marker_interval", - "global_params.preserve_fqdn", - "global_params.facilities", - "files.archive.size", - "files.archive.file_num", - "files", - "hosts.port", - "hosts.facility.protocol", # 1.3 and below - "hosts.protocol", - "hosts", - "users", - ] + + def _validate_template(self): + version = get_os_version(self._module) + if LooseVersion(version) >= LooseVersion("1.5"): + self._tmplt = Logging_globalTemplate15() + else: + self._tmplt = Logging_globalTemplate() + + self.parsers = [p["name"] for p in self._tmplt.PARSERS if not p["name"].endswith(".state")] + + def parse(self): + """override parse to check template""" + self._validate_template() + return super().parse() + + def get_parser(self, name): + """get_parsers""" + self._validate_template() + return super().get_parser(name) def execute_module(self): """Execute the module :rtype: A dictionary :returns: The result from module execution """ + self._validate_template() if self.state not in ["parsed", "gathered"]: self.generate_commands() self.run_commands() return self.result + def _strip_unsupported_15(self, data): + """Remove 1.4-only keys from a list_to_dict result for 1.5 devices.""" + if not data: + return data + warnings = [] + for key in ("files", "users"): + if data.pop(key, None) is not None: + warnings.append( + "'{0}' is not supported on VyOS 1.5+, ignoring.".format(key), + ) + if "global_params" in data: + if data["global_params"].pop("archive", None) is not None: + warnings.append( + "'global_params.archive' is not supported on VyOS 1.5+, ignoring.", + ) + for warning in warnings: + self._module.warn(warning) + return data + def generate_commands(self): """Generate configuration commands to send based on want, have and desired state. """ if self.want: wantd = self.list_to_dict(self.want, "want") else: wantd = dict() if self.have: haved = self.list_to_dict(self.have, "have") else: haved = dict() + version = get_os_version(self._module) + if LooseVersion(version) >= LooseVersion("1.5"): + wantd = self._strip_unsupported_15(wantd) + haved = self._strip_unsupported_15(haved) + if self.state in ["overridden", "replaced"]: if wantd != haved: wantx, havex = self.call_op(wantd, haved, "overridden") for k, have in havex.items(): if k not in wantx: self._compare(want={}, have=have) if not self.state == "deleted": wantd, haved = self.call_op(wantd, haved) if self.state == "merged": wantd = dict_merge(haved, wantd) for k, want in wantd.items(): self._compare(want=want, have=haved.pop(k, {})) def _compare(self, want, have): """Leverages the base class `compare()` method and populates the list of commands to be run by comparing the `want` and `have` data with the `parsers` defined for the Logging_global network resource. """ self.compare(parsers=self.parsers, want=want, have=have) self.handleStates(want=want, have=have) def operation_rep(self, params): op_val = dict() for k, val in params.items(): if k in ["console", "global_params"]: mod_val = deepcopy(val) op_val.update(self.flatten_facility({k: mod_val})) elif k in ["files", "hosts", "users"]: for m, n in val.items(): mod_n = deepcopy(n) if mod_n.get("archive"): del mod_n["archive"] if mod_n.get("facilities"): del mod_n["facilities"] if mod_n.get("port"): del mod_n["port"] tm = self.flatten_facility({k: {m: mod_n}}) op_val.update(tm) return op_val def call_op(self, _want, _have, mode=None): if mode == "overridden": w = self.operation_rep(_want) h = self.operation_rep(_have) else: w = self.flatten_facility(_want) h = self.flatten_facility(_have) return w, h def handleStates(self, want=None, have=None): stateparsers = [ "syslog.state", "console.state", "global_params.state", "global_params.archive.state", "files.archive.state", ] for par in stateparsers: op = get_from_dict(want, par) if op == "enabled": self.addcmd(want, par) elif op == "disabled": self.addcmd(want, par, True) break def flatten_facility(self, param): temp_param = dict() for element, val in param.items(): if element in ["console", "global_params", "syslog"]: if element != "syslog" and val.get("facilities"): for k, v in val.get("facilities").items(): temp_param[k + element] = {element: {"facilities": v}} del val["facilities"] if val: temp_param[element] = {element: val} if element in ["files", "hosts", "users"]: for k, v in val.items(): if v.get("facilities"): for pk, dat in v.get("facilities").items(): temp_param[pk + k] = { element: { "facilities": dat, self.pkey.get(element): v.get(self.pkey.get(element)), }, } del v["facilities"] if len(list(v.keys())) > 1: temp_param[k] = {element: v} else: temp_param[k] = {element: v} return temp_param def list_to_dict(self, param, op=None): updated_param = dict() if self.state == "deleted": if op == "have" and param: self.handleStates({"syslog": {"state": "disabled"}}) updated_param == {} else: self.pkey = { "files": "path", "hosts": "hostname", "users": "username", } for element, val in param.items(): if element == "facilities": # only with recursion call _tem_par = {} for par in val: if par.get("facility") and par.get("severity"): _tem_par.update({par.get("facility") + par.get("severity"): par}) elif par.get("facility") and par.get("protocol"): _tem_par.update({par.get("facility") + par.get("protocol"): par}) else: _tem_par.update({par.get("facility"): par}) return _tem_par elif element in ["console", "global_params", "syslog"]: if element != "syslog" and val.get("facilities"): val["facilities"] = self.list_to_dict(val) updated_param[element] = val elif element in ["hosts", "users", "files"]: for v in val: if v.get("facilities"): v["facilities"] = self.list_to_dict(v) if updated_param.get(element): updated_param[element].update({v.get(self.pkey.get(element)): v}) else: updated_param[element] = {v.get(self.pkey.get(element)): v} return updated_param diff --git a/plugins/module_utils/network/vyos/facts/logging_global/logging_global.py b/plugins/module_utils/network/vyos/facts/logging_global/logging_global.py index da80d967..0364a656 100644 --- a/plugins/module_utils/network/vyos/facts/logging_global/logging_global.py +++ b/plugins/module_utils/network/vyos/facts/logging_global/logging_global.py @@ -1,100 +1,116 @@ # -*- coding: utf-8 -*- # Copyright 2021 Red Hat # 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 """ The vyos logging_global fact class It is in this file the configuration is collected from the device for a given resource, parsed, and the facts tree is populated based on the configuration. """ from ansible_collections.ansible.netcommon.plugins.module_utils.network.common import utils from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.argspec.logging_global.logging_global import ( Logging_globalArgs, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.rm_templates.logging_global import ( Logging_globalTemplate, ) +from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.rm_templates.logging_global_15 import ( + Logging_globalTemplate15, +) +from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.utils.version import ( + LooseVersion, +) +from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.vyos import get_os_version class Logging_globalFacts(object): """The vyos logging_global facts class""" def __init__(self, module, subspec="config", options="options"): self._module = module self.argument_spec = Logging_globalArgs.argument_spec def get_logging_data(self, connection): return connection.get("show configuration commands | grep syslog") def process_facts(self, objFinal): if objFinal: for ke, vl in objFinal.items(): if ke == "files": _files = [] for k, v in vl.items(): _files.append(v) objFinal[ke] = _files objFinal[ke] = sorted(objFinal[ke], key=lambda item: item["path"]) elif ke == "hosts": _hosts = [] for k, v in vl.items(): _hosts.append(v) objFinal[ke] = _hosts objFinal[ke] = sorted(objFinal[ke], key=lambda item: item["hostname"]) elif ke == "users": _users = [] for k, v in vl.items(): _users.append(v) objFinal[ke] = _users objFinal[ke] = sorted(objFinal[ke], key=lambda item: item["username"]) elif ke == "console" or ke == "global_params": if objFinal[ke].get("facilities"): objFinal[ke]["facilities"] = sorted( objFinal[ke]["facilities"], key=lambda item: item["facility"], ) return objFinal def populate_facts(self, connection, ansible_facts, data=None): """Populate the facts for Logging_global network resource :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected conf :rtype: dictionary :returns: facts """ facts = {} objs = [] if not data: data = self.get_logging_data(connection) - # parse native config using the Logging_global template - logging_global_parser = Logging_globalTemplate(lines=data.splitlines(), module=self._module) + if LooseVersion(get_os_version(self._module)) >= LooseVersion("1.5"): + logging_global_parser = Logging_globalTemplate15( + lines=data.splitlines(), + module=self._module, + ) + else: + logging_global_parser = Logging_globalTemplate( + lines=data.splitlines(), + module=self._module, + ) + objs = logging_global_parser.parse() ansible_facts["ansible_network_resources"].pop("logging_global", None) objs = self.process_facts(objs) params = utils.remove_empties( logging_global_parser.validate_config( self.argument_spec, {"config": objs}, redact=True, ), ) facts["logging_global"] = params.get("config", {}) ansible_facts["ansible_network_resources"].update(facts) return ansible_facts diff --git a/plugins/module_utils/network/vyos/rm_templates/logging_global_15.py b/plugins/module_utils/network/vyos/rm_templates/logging_global_15.py new file mode 100644 index 00000000..3216747d --- /dev/null +++ b/plugins/module_utils/network/vyos/rm_templates/logging_global_15.py @@ -0,0 +1,241 @@ +# -*- coding: utf-8 -*- +# Copyright 2021 Red Hat +# 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 + +import re + +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.rm_base.network_template import ( + NetworkTemplate, +) + + +def tmplt_params(config_data): + def templt_common(val, tmplt): + if val.get("facility"): + tmplt += " facility {facility}".format(facility=val["facility"]) + if val.get("severity"): + tmplt += " level {level}".format(level=val["severity"]) + return tmplt + + tmplt = "" + if config_data.get("global_params"): + val = config_data.get("global_params") + tmplt += "system syslog local" + tmplt = templt_common(val.get("facilities", {}), tmplt) + elif config_data.get("console"): + val = config_data.get("console") + tmplt += "system syslog console" + tmplt = templt_common(val.get("facilities", {}), tmplt) + elif config_data.get("hosts"): + val = config_data.get("hosts") + if val.get("hostname") and not val.get("port") and not val.get("protocol"): + tmplt += "system syslog remote {hostname}".format(hostname=val["hostname"]) + if val.get("facilities"): + tmplt = templt_common(val.get("facilities"), tmplt) + return tmplt + + +class Logging_globalTemplate15(NetworkTemplate): + def __init__(self, lines=None, module=None): + prefix = {"set": "set", "remove": "delete"} + super(Logging_globalTemplate15, self).__init__( + lines=lines, + tmplt=self, + prefix=prefix, + module=module, + ) + + # fmt: off + PARSERS = [ + { + "name": "syslog.state", + "getval": re.compile( + r""" + ^set\ssystem + (\s(?Psyslog)) + $""", re.VERBOSE, + ), + "setval": "system syslog", + "result": { + "syslog": { + "state": "{{ 'enabled' if syslog is defined else 'disabled' }}", + }, + }, + }, + { + "name": "console.state", + "getval": re.compile( + r""" + ^set\ssystem\ssyslog + (\s(?Pconsole)) + $""", re.VERBOSE, + ), + "setval": "system syslog console", + "result": { + "console": { + "state": "{{ 'enabled' if console is defined else 'disabled' }}", + }, + }, + }, + { + "name": "console.facilities", + "getval": re.compile( + r""" + ^set\ssystem\ssyslog\sconsole\sfacility + (\s(?Pall|auth|authpriv|cron|daemon|kern|lpr|mail|mark|news|protocols|security|syslog|user|uucp|local[0-7]))? + (\slevel\s(?P'(emerg|alert|crit|err|warning|notice|info|debug|all)'))? + $""", re.VERBOSE, + ), + "setval": tmplt_params, + "remval": "system syslog console facility {{ console.facilities.facility }}", + "result": { + "console": { + "facilities": [ + { + "facility": "{{ facility }}", + "severity": "{{ level }}", + }, + ], + }, + }, + }, + { + "name": "global_params.state", + "getval": re.compile( + r""" + ^set\ssystem\ssyslog + (\s(?Plocal)) + $""", re.VERBOSE, + ), + "setval": "system syslog local", + "result": { + "global_params": { + "state": "{{ 'enabled' if local is defined else 'disabled' }}", + }, + }, + }, + { + "name": "global_params.marker_interval", + "getval": re.compile( + r""" + ^set\ssystem\ssyslog\smarker\sinterval + (\s(?P'(\d+)'))? + $""", re.VERBOSE, + ), + "setval": "system syslog marker interval {{ global_params.marker_interval }}", + "remval": "system syslog marker", + "result": { + "global_params": { + "marker_interval": "{{ marker_interval }}", + }, + }, + }, + { + "name": "global_params.preserve_fqdn", + "getval": re.compile( + r""" + ^set\ssystem\ssyslog + (\s(?Ppreserve-fqdn)) + $""", re.VERBOSE, + ), + "setval": "system syslog preserve-fqdn", + "result": { + "global_params": { + "preserve_fqdn": "{{ True if preserve_fqdn is defined }}", + }, + }, + }, + { + "name": "global_params.facilities", + "getval": re.compile( + r""" + ^set\ssystem\ssyslog\slocal\sfacility + (\s(?Pall|auth|authpriv|cron|daemon|kern|lpr|mail|mark|news|protocols|security|syslog|user|uucp|local[0-7]))? + (\slevel\s(?P'(emerg|alert|crit|err|warning|notice|info|debug|all)'))? + $""", re.VERBOSE, + ), + "setval": tmplt_params, + "remval": "system syslog local facility {{ global_params.facilities.facility }}", + "result": { + "global_params": { + "facilities": [ + { + "facility": "{{ facility }}", + "severity": "{{ level }}", + }, + ], + }, + }, + }, + { + "name": "hosts.port", + "getval": re.compile( + r""" + ^set\ssystem\ssyslog\sremote + (\s(?P\S+)) + (\sport\s(?P'(\d+)')) + $""", re.VERBOSE, + ), + "setval": "system syslog remote {{ hosts.hostname }} port {{ hosts.port }}", + "result": { + "hosts": { + "{{ hostname }}": { + "hostname": "{{ hostname }}", + "port": "{{ port }}", + }, + }, + }, + }, + { + "name": "hosts.protocol", + "getval": re.compile( + r""" + ^set\ssystem\ssyslog\sremote + (\s(?P\S+)) + (\sprotocol\s(?P'(udp|tcp)')) + $""", re.VERBOSE, + ), + "setval": "system syslog remote {{ hosts.hostname }} protocol {{ hosts.protocol }}", + "result": { + "hosts": { + "{{ hostname }}": { + "hostname": "{{ hostname }}", + "protocol": "{{ protocol }}", + }, + }, + }, + }, + { + "name": "hosts", + "getval": re.compile( + r""" + ^set\ssystem\ssyslog\sremote + (\s(?P\S+)) + (\sfacility\s(?Pall|auth|authpriv|cron|daemon|kern|lpr|mail|mark|news|protocols|security|syslog|user|uucp|local[0-7])) + (\slevel\s(?P'(emerg|alert|crit|err|warning|notice|info|debug|all)'))? + $""", re.VERBOSE, + ), + "setval": tmplt_params, + "remval": "system syslog remote {{ hosts.hostname }}", + "result": { + "hosts": { + "{{ hostname }}": { + "hostname": "{{ hostname }}", + "facilities": [ + { + "facility": "{{ facility }}", + "severity": "{{ level }}", + }, + ], + }, + }, + }, + }, + ] + # fmt: on diff --git a/plugins/modules/vyos_logging_global.py b/plugins/modules/vyos_logging_global.py index 3380b9ea..7050870f 100644 --- a/plugins/modules/vyos_logging_global.py +++ b/plugins/modules/vyos_logging_global.py @@ -1,785 +1,940 @@ #!/usr/bin/python # -*- coding: utf-8 -*- # Copyright 2024 Red Hat # GNU General Public License v3.0+ # (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) """ The module file for vyos_logging_global """ from __future__ import absolute_import, division, print_function __metaclass__ = type DOCUMENTATION = """ module: vyos_logging_global version_added: 2.4.0 short_description: Logging resource module description: This module manages the logging attributes of Vyos network devices author: Sagar Paul (@KB-perByte) notes: - - Tested against VyOS 1.3.8, 1.4.2, the upcoming 1.5, and the rolling release of spring 2025 + - Tested against VyOS 1.3.8, 1.4.2, 1.5.0, and the rolling release of spring 2025. - This module works with connection C(network_cli). - - The Configuration defaults of the Vyos network devices - are supposed to hinder idempotent behavior of plays + - The Configuration defaults of the VyOS network devices + are supposed to hinder idempotent behavior of plays. + - > + B(VyOS 1.5+ breaking changes): The C(files), C(users), and + C(global_params.archive) options are not supported on VyOS 1.5+. + If provided, they will be ignored with a warning. + The C(global_params) facilities now map to C(set system syslog local) + (was C(set system syslog global)). + Remote hosts now map to C(set system syslog remote) + (was C(set system syslog host)). + The C(marker_interval) and C(preserve_fqdn) options moved to top-level + (was under C(global_params) in the CLI, argspec key unchanged). options: config: description: A list containing dictionary of logging options type: dict suboptions: console: description: logging to serial console type: dict suboptions: state: &state_config description: enable or disable the command type: str choices: - enabled - disabled facilities: description: facility configurations for console type: list elements: dict suboptions: facility: &facility description: Facility for logging type: str choices: - all - auth - authpriv - cron - daemon - kern - lpr - mail - mark - news - protocols - security - syslog - user - uucp - local0 - local1 - local2 - local3 - local4 - local5 - local6 - local7 severity: &severity description: logging level type: str choices: - emerg - alert - crit - err - warning - notice - info - debug - all files: - description: logging to file + description: > + Logging to file. B(Not supported on VyOS 1.5+.) If provided on a + 1.5+ device, this option will be ignored with a warning. type: list elements: dict suboptions: path: description: file name or path type: str archive: &archive description: Log file size and rotation characteristics type: dict suboptions: state: *state_config file_num: description: Number of saved files (default is 5) type: int size: description: Size of log files (in kilobytes, default is 256) type: int facilities: ¶ms description: facility configurations type: list elements: dict suboptions: facility: *facility severity: *severity global_params: - description: logging to serial console + description: > + Global logging parameters. On VyOS 1.5+, facilities map to + C(set system syslog local), and C(marker_interval)/C(preserve_fqdn) + move to top-level CLI paths. The C(archive) suboption is + B(not supported on VyOS 1.5+) and will be ignored with a warning. type: dict suboptions: state: *state_config archive: *archive facilities: *params marker_interval: description: time interval how often a mark message is being sent in seconds (default is 1200) type: int preserve_fqdn: description: uses FQDN for logging type: bool hosts: - description: logging to serial console + description: > + Logging to remote hosts. On VyOS 1.5+, maps to + C(set system syslog remote) (was C(set system syslog host)). type: list elements: dict suboptions: port: description: Destination port (1-65535) type: int facilities: description: facility configurations for host type: list elements: dict suboptions: facility: *facility severity: *severity protocol: description: syslog communication protocol. Version 1.3 and below. type: str choices: - udp - tcp hostname: description: Remote host name or IP address type: str protocol: description: syslog communication protocol. Version 1.4+ type: str choices: - udp - tcp syslog: description: logging syslog type: dict suboptions: state: *state_config users: - description: logging to file + description: > + Logging to a local user terminal. B(Not supported on VyOS 1.5+.) + If provided on a 1.5+ device, this option will be ignored with a warning. type: list elements: dict suboptions: username: description: user login name type: str facilities: *params running_config: description: - This option is used only with state I(parsed). - The value of this option should be the output received from the VYOS device by executing the command B(show configuration commands | grep syslog). - The state I(parsed) reads the configuration from C(running_config) option and transforms it into Ansible structured data as per the resource module's argspec and the value is then returned in the I(parsed) key within the result. type: str state: choices: - merged - replaced - overridden - deleted - gathered - parsed - rendered default: merged description: - The state the configuration should be left in - The states I(replaced) and I(overridden) have identical behaviour for this module. - Refer to examples for more details. type: str """ EXAMPLES = """ # Using state: merged # Before state: # ------------- # vyos:~$show configuration commands | grep syslog - name: Apply the provided configuration vyos.vyos.vyos_logging_global: config: console: facilities: - facility: local7 severity: err files: - path: logFile archive: file_num: 2 facilities: - facility: local6 severity: emerg hosts: - hostname: 172.16.0.1 facilities: - facility: local7 severity: all - facility: all protocol: udp port: 223 users: - username: vyos facilities: - facility: local7 severity: debug global_params: archive: file_num: 2 size: 111 facilities: - facility: cron severity: debug marker_interval: 111 preserve_fqdn: true state: merged # Commands Fired: # --------------- # "commands": [ # "set system syslog console facility local7 level err", # "set system syslog file logFile archive file 2", # "set system syslog host 172.16.0.1 facility local7 level all", # "set system syslog file logFile facility local6 level emerg", # "set system syslog host 172.16.0.1 facility all protocol udp", # "set system syslog user vyos facility local7 level debug", # "set system syslog host 172.16.0.1 port 223", # "set system syslog global facility cron level debug", # "set system syslog global archive file 2", # "set system syslog global archive size 111", # "set system syslog global marker interval 111", # "set system syslog global preserve-fqdn" # ], # After state: # ------------ # vyos:~$ show configuration commands | grep syslog # set system syslog console facility local7 level 'err' # set system syslog file logFile archive file '2' # set system syslog file logFile facility local6 level 'emerg' # set system syslog global archive file '2' # set system syslog global archive size '111' # set system syslog global facility cron level 'debug' # set system syslog global marker interval '111' # set system syslog global preserve-fqdn # set system syslog host 172.16.0.1 facility all protocol 'udp' # set system syslog host 172.16.0.1 facility local7 level 'all' # set system syslog host 172.16.0.1 port '223' # set system syslog user vyos facility local7 level 'debug' # Using state: deleted # Before state: # ------------- # vyos:~$show configuration commands | grep syslog # set system syslog console facility local7 level 'err' # set system syslog file logFile archive file '2' # set system syslog file logFile facility local6 level 'emerg' # set system syslog global archive file '2' # set system syslog global archive size '111' # set system syslog global facility cron level 'debug' # set system syslog global marker interval '111' # set system syslog global preserve-fqdn # set system syslog host 172.16.0.1 facility all protocol 'udp' # set system syslog host 172.16.0.1 facility local7 level 'all' # set system syslog host 172.16.0.1 port '223' # set system syslog user vyos facility local7 level 'debug' - name: delete the existing configuration vyos.vyos.vyos_logging_global: state: deleted # Commands Fired: # --------------- # "commands": [ # "delete system syslog" # ], # After state: # ------------ # vyos:~$show configuration commands | grep syslog # Using state: overridden # Before state: # ------------- # vyos:~$show configuration commands | grep syslog # set system syslog console facility local7 level 'err' # set system syslog file logFile archive file '2' # set system syslog file logFile facility local6 level 'emerg' # set system syslog global archive file '2' # set system syslog global archive size '111' # set system syslog global facility cron level 'debug' # set system syslog global marker interval '111' # set system syslog global preserve-fqdn # set system syslog host 172.16.0.1 facility all protocol 'udp' # set system syslog host 172.16.0.1 facility local7 level 'all' # set system syslog host 172.16.0.1 port '223' # set system syslog user vyos facility local7 level 'debug' - name: Override the current configuration vyos.vyos.vyos_logging_global: config: console: facilities: - facility: all - facility: local7 severity: err - facility: news severity: debug files: - path: logFileNew hosts: - hostname: 172.16.0.2 facilities: - facility: local5 severity: all global_params: archive: file_num: 10 state: overridden # Commands Fired: # --------------- # "commands": [ # "delete system syslog file logFile", # "delete system syslog global facility cron", # "delete system syslog host 172.16.0.1", # "delete system syslog user vyos", # "set system syslog console facility all", # "set system syslog console facility news level debug", # "set system syslog file logFileNew", # "set system syslog host 172.16.0.2 facility local5 level all", # "set system syslog global archive file 10", # "delete system syslog global archive size 111", # "delete system syslog global marker", # "delete system syslog global preserve-fqdn" # ], # After state: # ------------ # vyos:~$show configuration commands | grep syslog # set system syslog console facility all # set system syslog console facility local7 level 'err' # set system syslog console facility news level 'debug' # set system syslog file logFileNew # set system syslog global archive file '10' # set system syslog host 172.16.0.2 facility local5 level 'all' # Using state: replaced # Before state: # ------------- # vyos:~$show configuration commands | grep syslog # set system syslog console facility all # set system syslog console facility local7 level 'err' # set system syslog console facility news level 'debug' # set system syslog file logFileNew # set system syslog global archive file '10' # set system syslog host 172.16.0.2 facility local5 level 'all' - name: Replace with the provided configuration register: result vyos.vyos.vyos_logging_global: config: console: facilities: - facility: local6 users: - username: paul facilities: - facility: local7 severity: err state: replaced # Commands Fired: # --------------- # "commands": [ # "delete system syslog console facility all", # "delete system syslog console facility local7", # "delete system syslog console facility news", # "delete system syslog file logFileNew", # "delete system syslog global archive file 10", # "delete system syslog host 172.16.0.2", # "set system syslog console facility local6", # "set system syslog user paul facility local7 level err" # ], # After state: # ------------ # vyos:~$show configuration commands | grep syslog # set system syslog console facility local6 # set system syslog user paul facility local7 level 'err' # Using state: gathered - name: Gather logging config vyos.vyos.vyos_logging_global: state: gathered # Module Execution Result: # ------------------------ # "gathered": { # "console": { # "facilities": [ # { # "facility": "local6" # }, # { # "facility": "local7", # "severity": "err" # } # ] # }, # "files": [ # { # "archive": { # "file_num": 2 # }, # "facilities": [ # { # "facility": "local6", # "severity": "emerg" # } # ], # "path": "logFile" # } # ], # "global_params": { # "archive": { # "file_num": 2, # "size": 111 # }, # "facilities": [ # { # "facility": "cron", # "severity": "debug" # } # ], # "marker_interval": 111, # "preserve_fqdn": true # }, # "hosts": [ # { # "facilities": [ # { # "facility": "all", # "protocol": "udp" # }, # { # "facility": "local7", # "severity": "all" # } # ], # "hostname": "172.16.0.1", # "port": 223 # } # ], # "users": [ # { # "facilities": [ # { # "facility": "local7", # "severity": "err" # } # ], # "username": "paul" # }, # { # "facilities": [ # { # "facility": "local7", # "severity": "debug" # } # ], # "username": "vyos" # } # ] # }, # After state: # ------------ # vyos:~$show configuration commands | grep syslog # set system syslog console facility local6 # set system syslog console facility local7 level 'err' # set system syslog file logFile archive file '2' # set system syslog file logFile facility local6 level 'emerg' # set system syslog global archive file '2' # set system syslog global archive size '111' # set system syslog global facility cron level 'debug' # set system syslog global marker interval '111' # set system syslog global preserve-fqdn # set system syslog host 172.16.0.1 facility all protocol 'udp' # set system syslog host 172.16.0.1 facility local7 level 'all' # set system syslog host 172.16.0.1 port '223' # set system syslog user paul facility local7 level 'err' # set system syslog user vyos facility local7 level 'debug' # Using state: rendered - name: Render the provided configuration vyos.vyos.vyos_logging_global: config: console: facilities: - facility: local7 severity: err files: - path: logFile archive: file_num: 2 facilities: - facility: local6 severity: emerg hosts: - hostname: 172.16.0.1 facilities: - facility: local7 severity: all - facility: all protocol: udp port: 223 users: - username: vyos facilities: - facility: local7 severity: debug global_params: archive: file_num: 2 size: 111 facilities: - facility: cron severity: debug marker_interval: 111 preserve_fqdn: true state: rendered # Module Execution Result: # ------------------------ # "rendered": [ # "set system syslog console facility local7 level err", # "set system syslog file logFile facility local6 level emerg", # "set system syslog file logFile archive file 2", # "set system syslog host 172.16.0.1 facility local7 level all", # "set system syslog host 172.16.0.1 facility all protocol udp", # "set system syslog host 172.16.0.1 port 223", # "set system syslog user vyos facility local7 level debug", # "set system syslog global facility cron level debug", # "set system syslog global archive file 2", # "set system syslog global archive size 111", # "set system syslog global marker interval 111", # "set system syslog global preserve-fqdn" # ] # Using state: parsed # File: parsed.cfg # ---------------- # set system syslog console facility local6 # set system syslog console facility local7 level 'err' # set system syslog file logFile archive file '2' # set system syslog file logFile facility local6 level 'emerg' # set system syslog global archive file '2' # set system syslog global archive size '111' # set system syslog global facility cron level 'debug' # set system syslog global marker interval '111' # set system syslog global preserve-fqdn # set system syslog host 172.16.0.1 facility all protocol 'udp' # set system syslog host 172.16.0.1 facility local7 level 'all' # set system syslog host 172.16.0.1 port '223' # set system syslog user paul facility local7 level 'err' # set system syslog user vyos facility local7 level 'debug' - name: Parse the provided configuration vyos.vyos.vyos_logging_global: running_config: "{{ lookup('file', 'parsed_vyos.cfg') }}" state: parsed # Module Execution Result: # ------------------------ # "parsed": { # "console": { # "facilities": [ # { # "facility": "local6" # }, # { # "facility": "local7", # "severity": "err" # } # ] # }, # "files": [ # { # "archive": { # "file_num": 2 # }, # "facilities": [ # { # "facility": "local6", # "severity": "emerg" # } # ], # "path": "logFile" # } # ], # "global_params": { # "archive": { # "file_num": 2, # "size": 111 # }, # "facilities": [ # { # "facility": "cron", # "severity": "debug" # } # ], # "marker_interval": 111, # "preserve_fqdn": true # }, # "hosts": [ # { # "facilities": [ # { # "facility": "all", # "protocol": "udp" # }, # { # "facility": "local7", # "severity": "all" # } # ], # "hostname": "172.16.0.1", # "port": 223 # } # ], # "users": [ # { # "facilities": [ # { # "facility": "local7", # "severity": "err" # } # ], # "username": "paul" # }, # { # "facilities": [ # { # "facility": "local7", # "severity": "debug" # } # ], # "username": "vyos" # } # ] # } # } +# Using state: merged (VyOS 1.5+) + +# Before state: +# ------------- + +# vyos:~$ show configuration commands | grep syslog + +- name: Apply the provided configuration (VyOS 1.5+) + vyos.vyos.vyos_logging_global: + config: + console: + facilities: + - facility: local7 + severity: err + hosts: + - hostname: 172.16.0.1 + facilities: + - facility: local7 + severity: all + port: 514 + protocol: udp + global_params: + facilities: + - facility: cron + severity: debug + marker_interval: 111 + preserve_fqdn: true + state: merged + +# Commands Fired: +# --------------- + +# "commands": [ +# "set system syslog console facility local7 level err", +# "set system syslog remote 172.16.0.1 facility local7 level all", +# "set system syslog remote 172.16.0.1 port 514", +# "set system syslog remote 172.16.0.1 protocol udp", +# "set system syslog local facility cron level debug", +# "set system syslog marker interval 111", +# "set system syslog preserve-fqdn" +# ], + +# After state: +# ------------ + +# vyos:~$ show configuration commands | grep syslog +# set system syslog console facility local7 level 'err' +# set system syslog local facility cron level 'debug' +# set system syslog marker interval '111' +# set system syslog preserve-fqdn +# set system syslog remote 172.16.0.1 facility local7 level 'all' +# set system syslog remote 172.16.0.1 port '514' +# set system syslog remote 172.16.0.1 protocol 'udp' + +# Using state: gathered (VyOS 1.5+) + +- name: Gather logging config (VyOS 1.5+) + vyos.vyos.vyos_logging_global: + state: gathered + +# Module Execution Result: +# ------------------------ + +# "gathered": { +# "console": { +# "facilities": [ +# { +# "facility": "local7", +# "severity": "err" +# } +# ] +# }, +# "global_params": { +# "facilities": [ +# { +# "facility": "cron", +# "severity": "debug" +# } +# ], +# "marker_interval": 111, +# "preserve_fqdn": true +# }, +# "hosts": [ +# { +# "facilities": [ +# { +# "facility": "local7", +# "severity": "all" +# } +# ], +# "hostname": "172.16.0.1", +# "port": 514, +# "protocol": "udp" +# } +# ] +# }, + +# Using state: rendered (VyOS 1.5+) +# Note: files, users, and global_params.archive are not supported on VyOS 1.5+ +# and will be ignored with a warning if provided. + +- name: Render the provided configuration (VyOS 1.5+) + vyos.vyos.vyos_logging_global: + config: + console: + facilities: + - facility: local7 + severity: err + hosts: + - hostname: 172.16.0.1 + facilities: + - facility: local7 + severity: all + port: 514 + protocol: udp + global_params: + facilities: + - facility: cron + severity: debug + marker_interval: 111 + preserve_fqdn: true + state: rendered + +# Module Execution Result: +# ------------------------ + +# "rendered": [ +# "set system syslog console facility local7 level err", +# "set system syslog remote 172.16.0.1 facility local7 level all", +# "set system syslog remote 172.16.0.1 port 514", +# "set system syslog remote 172.16.0.1 protocol udp", +# "set system syslog local facility cron level debug", +# "set system syslog marker interval 111", +# "set system syslog preserve-fqdn" +# ] """ RETURN = """ before: description: The configuration prior to the module execution. returned: when I(state) is C(merged), C(replaced), C(overridden), C(deleted) or C(purged) type: dict sample: > This output will always be in the same format as the module argspec. after: description: The resulting configuration after module execution. returned: when changed type: dict sample: > This output will always be in the same format as the module argspec. commands: description: The set of commands pushed to the remote device. returned: when I(state) is C(merged), C(replaced), C(overridden), C(deleted) or C(purged) type: list sample: - set system syslog console facility local7 level err - set system syslog host 172.16.0.1 port 223 - set system syslog global archive size 111 rendered: description: The provided configuration in the task rendered in device-native format (offline). returned: when I(state) is C(rendered) type: list sample: - set system syslog host 172.16.0.1 port 223 - set system syslog user vyos facility local7 level debug - set system syslog global facility cron level debu gathered: description: Facts about the network resource gathered from the remote device as structured data. returned: when I(state) is C(gathered) type: list sample: > This output will always be in the same format as the module argspec. parsed: description: The device native config provided in I(running_config) option parsed into structured data as per module argspec. returned: when I(state) is C(parsed) type: list sample: > This output will always be in the same format as the module argspec. """ from ansible.module_utils.basic import AnsibleModule from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.argspec.logging_global.logging_global import ( Logging_globalArgs, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.config.logging_global.logging_global import ( Logging_global, ) def main(): """ Main entry point for module execution :returns: the result form module invocation """ module = AnsibleModule( argument_spec=Logging_globalArgs.argument_spec, mutually_exclusive=[["config", "running_config"]], required_if=[ ["state", "merged", ["config"]], ["state", "replaced", ["config"]], ["state", "overridden", ["config"]], ["state", "rendered", ["config"]], ["state", "parsed", ["running_config"]], ], supports_check_mode=True, ) result = Logging_global(module).execute_module() module.exit_json(**result) if __name__ == "__main__": main() diff --git a/tests/integration/targets/vyos_logging_global/tests/cli/_get_version.yaml b/tests/integration/targets/vyos_logging_global/tests/cli/_get_version.yaml index 2588b194..d2c09490 100644 --- a/tests/integration/targets/vyos_logging_global/tests/cli/_get_version.yaml +++ b/tests/integration/targets/vyos_logging_global/tests/cli/_get_version.yaml @@ -1,28 +1,34 @@ - name: make sure to get facts vyos.vyos.vyos_facts: vars: ansible_connection: ansible.netcommon.network_cli register: vyos_facts when: vyos_version is not defined - name: debug vyos_facts debug: var: vyos_facts - name: pull version from facts set_fact: vyos_version: "{{ vyos_facts.ansible_facts.ansible_net_version.split('-')[0].split(' ')[-1] }}" when: vyos_version is not defined - name: fix '.0' versions set_fact: vyos_version: "{{ vyos_version }}.0" when: vyos_version.count('.') == 1 - name: include correct vars include_vars: pre-v1_4.yaml when: vyos_version is version('1.4.0', '<', version_type='semver') - name: include correct vars include_vars: v1_4.yaml - when: vyos_version is version('1.4.0', '>=', version_type='semver') + when: + - vyos_version is version('1.4.0', '>=', version_type='semver') + - vyos_version is version('1.5.0', '<', version_type='semver') + +- name: include correct vars + include_vars: v1_5.yaml + when: vyos_version is version('1.5.0', '>=', version_type='semver') diff --git a/tests/integration/targets/vyos_logging_global/tests/cli/_parsed_15.cfg b/tests/integration/targets/vyos_logging_global/tests/cli/_parsed_15.cfg new file mode 100644 index 00000000..c1fcea5c --- /dev/null +++ b/tests/integration/targets/vyos_logging_global/tests/cli/_parsed_15.cfg @@ -0,0 +1,10 @@ +set system syslog console facility all +set system syslog console facility news level 'debug' +set system syslog local facility cron level 'debug' +set system syslog local facility local7 level 'debug' +set system syslog marker interval '111' +set system syslog preserve-fqdn +set system syslog remote 10.0.2.12 facility all level 'info' +set system syslog remote 10.0.2.15 facility all level 'all' +set system syslog remote 10.0.2.15 port '514' +set system syslog remote 10.0.2.15 protocol 'udp' diff --git a/tests/integration/targets/vyos_logging_global/tests/cli/_populate.yaml b/tests/integration/targets/vyos_logging_global/tests/cli/_populate.yaml index a17021b4..c659a577 100644 --- a/tests/integration/targets/vyos_logging_global/tests/cli/_populate.yaml +++ b/tests/integration/targets/vyos_logging_global/tests/cli/_populate.yaml @@ -1,43 +1,9 @@ --- - ansible.builtin.include_tasks: _get_version.yaml - ansible.builtin.include_tasks: _remove_config.yaml - name: POPULATE Apply the provided configuration register: result vyos.vyos.vyos_logging_global: - config: - console: - facilities: - - facility: all - - facility: local7 - severity: err - - facility: news - severity: debug - files: - - path: def - archive: - file_num: 2 - facilities: - - facility: local6 - severity: emerg - - facility: local7 - severity: emerg - hosts: "{{ populate_logging_global_hosts }}" - users: - - username: vyos - facilities: - - facility: local7 - severity: debug - - facility: local6 - severity: alert - - username: paul - facilities: - - facility: local7 - severity: err - global_params: - facilities: - - facility: cron - severity: debug - - facility: local7 - severity: debug + config: "{{ populate_config }}" state: merged diff --git a/tests/integration/targets/vyos_logging_global/tests/cli/parsed.yaml b/tests/integration/targets/vyos_logging_global/tests/cli/parsed.yaml index 40a14f62..9ed8d7b2 100644 --- a/tests/integration/targets/vyos_logging_global/tests/cli/parsed.yaml +++ b/tests/integration/targets/vyos_logging_global/tests/cli/parsed.yaml @@ -1,14 +1,16 @@ --- - debug: msg: START vyos_logging_global parsed integration tests on connection={{ ansible_connection }} +- ansible.builtin.include_tasks: _get_version.yaml + - name: TEST - [parsed] Parse the commands for provided configuration register: result vyos.vyos.vyos_logging_global: - running_config: "{{ lookup('file', '_parsed.cfg') }}" + running_config: "{{ lookup('file', parsed_config_file) }}" state: parsed - assert: that: - result.changed == false - parsed['after'] == result.parsed diff --git a/tests/integration/targets/vyos_logging_global/tests/cli/unsupported_15.yaml b/tests/integration/targets/vyos_logging_global/tests/cli/unsupported_15.yaml new file mode 100644 index 00000000..ee583439 --- /dev/null +++ b/tests/integration/targets/vyos_logging_global/tests/cli/unsupported_15.yaml @@ -0,0 +1,46 @@ +--- +- debug: + msg: START vyos_logging_global unsupported 1.5 keys integration tests on connection={{ ansible_connection }} + +- name: ensure facts + include_tasks: _get_version.yaml + +- block: + - name: Test warning on unsupported 1.5 keys - merged + register: result + vyos.vyos.vyos_logging_global: + config: + console: + facilities: + - facility: all + files: + - path: abc + archive: + size: 125 + users: + - username: vyos + facilities: + - facility: local7 + severity: debug + global_params: + archive: + size: 111 + facilities: + - facility: cron + severity: debug + state: merged + + - name: Assert unsupported keys warned and ignored + ansible.builtin.assert: + that: + - result.warnings | select('search', 'files') | list | length > 0 + - result.warnings | select('search', 'users') | list | length > 0 + - result.warnings | select('search', 'global_params.archive') | list | length > 0 + - "'set system syslog file' not in result.commands | join(' ')" + - "'set system syslog user' not in result.commands | join(' ')" + - "'set system syslog local facility cron level debug' in result.commands" + + always: + - include_tasks: _remove_config.yaml + + when: vyos_version is version('1.5', '>=') diff --git a/tests/integration/targets/vyos_logging_global/vars/main.yaml b/tests/integration/targets/vyos_logging_global/vars/main.yaml index 13ca654f..ed97d539 100644 --- a/tests/integration/targets/vyos_logging_global/vars/main.yaml +++ b/tests/integration/targets/vyos_logging_global/vars/main.yaml @@ -1,28 +1 @@ --- -rendered: - commands: - - set system syslog console facility all - - set system syslog file abc archive size 125 - - set system syslog host 172.16.2.15 facility all level all - - set system syslog user vyos facility local7 level debug - - set system syslog global facility cron level debug - -deleted: - commands: - - delete system syslog - after: {} -parsed: - after: - console: - facilities: - - facility: news - severity: debug - files: - - path: def - facilities: - - facility: local7 - severity: emerg - global_params: - facilities: - - facility: local7 - severity: debug diff --git a/tests/integration/targets/vyos_logging_global/vars/pre-v1_4.yaml b/tests/integration/targets/vyos_logging_global/vars/pre-v1_4.yaml index 6af72488..b0115edf 100644 --- a/tests/integration/targets/vyos_logging_global/vars/pre-v1_4.yaml +++ b/tests/integration/targets/vyos_logging_global/vars/pre-v1_4.yaml @@ -1,68 +1,135 @@ --- host_172_16_2_15: &host_172_16_2_15 - hostname: 172.16.2.15 facilities: - facility: all severity: all merged: before: {} commands: - set system syslog host 172.16.2.15 facility all level all - set system syslog console facility all - set system syslog user vyos facility local7 level debug - set system syslog global facility cron level debug - set system syslog file def archive file 2 - set system syslog file def facility local6 level emerg after: console: facilities: - facility: all files: - path: def facilities: - facility: local6 severity: emerg archive: file_num: 2 hosts: *host_172_16_2_15 users: - username: vyos facilities: - facility: local7 severity: debug global_params: facilities: - facility: cron severity: debug overridden: commands: - delete system syslog file def - delete system syslog global facility cron - delete system syslog global facility local7 - delete system syslog host 172.16.2.12 - delete system syslog host 172.16.2.15 - delete system syslog user paul - delete system syslog user vyos after: console: facilities: - facility: all - facility: local7 severity: err - facility: news severity: debug files: - path: Myfile populate_logging_global_hosts: - hostname: 172.16.2.15 facilities: - facility: all protocol: udp - hostname: 172.16.2.12 facilities: - facility: all protocol: udp + +rendered: + commands: + - set system syslog console facility all + - set system syslog file abc archive size 125 + - set system syslog host 172.16.2.15 facility all level all + - set system syslog user vyos facility local7 level debug + - set system syslog global facility cron level debug + +deleted: + commands: + - delete system syslog + after: {} + +parsed: + after: + console: + facilities: + - facility: news + severity: debug + files: + - path: def + facilities: + - facility: local7 + severity: emerg + global_params: + facilities: + - facility: local7 + severity: debug + +populate_config: + console: + facilities: + - facility: all + - facility: local7 + severity: err + - facility: news + severity: debug + files: + - path: def + archive: + file_num: 2 + facilities: + - facility: local6 + severity: emerg + - facility: local7 + severity: emerg + hosts: "{{ populate_logging_global_hosts }}" + users: + - username: vyos + facilities: + - facility: local7 + severity: debug + - facility: local6 + severity: alert + - username: paul + facilities: + - facility: local7 + severity: err + global_params: + facilities: + - facility: cron + severity: debug + - facility: local7 + severity: debug + +parsed_config_file: "_parsed.cfg" diff --git a/tests/integration/targets/vyos_logging_global/vars/v1_4.yaml b/tests/integration/targets/vyos_logging_global/vars/v1_4.yaml index bfe5e12d..599cc42f 100644 --- a/tests/integration/targets/vyos_logging_global/vars/v1_4.yaml +++ b/tests/integration/targets/vyos_logging_global/vars/v1_4.yaml @@ -1,74 +1,141 @@ --- host_172_16_2_15: &host_172_16_2_15 - hostname: 172.16.2.15 facilities: - facility: all severity: all protocol: tcp merged: before: {} commands: - set system syslog host 172.16.2.15 facility all level all - set system syslog host 172.16.2.15 protocol tcp - set system syslog console facility all - set system syslog user vyos facility local7 level debug - set system syslog global facility cron level debug - set system syslog file def archive file 2 - set system syslog file def facility local6 level emerg after: console: facilities: - facility: all files: - path: def facilities: - facility: local6 severity: emerg archive: file_num: 2 hosts: *host_172_16_2_15 users: - username: vyos facilities: - facility: local7 severity: debug global_params: facilities: - facility: cron severity: debug overridden: commands: - delete system syslog file def - delete system syslog global facility cron - delete system syslog global facility local7 - delete system syslog host 172.16.2.12 protocol udp - delete system syslog host 172.16.2.15 protocol udp - delete system syslog host 172.16.2.12 - delete system syslog host 172.16.2.15 - delete system syslog user paul - delete system syslog user vyos after: console: facilities: - facility: all - facility: local7 severity: err - facility: news severity: debug files: - path: Myfile populate_logging_global_hosts: - hostname: 172.16.2.15 facilities: - facility: all severity: all - facility: all protocol: udp - hostname: 172.16.2.12 facilities: - facility: all protocol: udp + +rendered: + commands: + - set system syslog console facility all + - set system syslog file abc archive size 125 + - set system syslog host 172.16.2.15 facility all level all + - set system syslog user vyos facility local7 level debug + - set system syslog global facility cron level debug + +deleted: + commands: + - delete system syslog + after: {} + +parsed: + after: + console: + facilities: + - facility: news + severity: debug + files: + - path: def + facilities: + - facility: local7 + severity: emerg + global_params: + facilities: + - facility: local7 + severity: debug + +populate_config: + console: + facilities: + - facility: all + - facility: local7 + severity: err + - facility: news + severity: debug + files: + - path: def + archive: + file_num: 2 + facilities: + - facility: local6 + severity: emerg + - facility: local7 + severity: emerg + hosts: "{{ populate_logging_global_hosts }}" + users: + - username: vyos + facilities: + - facility: local7 + severity: debug + - facility: local6 + severity: alert + - username: paul + facilities: + - facility: local7 + severity: err + global_params: + facilities: + - facility: cron + severity: debug + - facility: local7 + severity: debug + +parsed_config_file: "_parsed.cfg" diff --git a/tests/integration/targets/vyos_logging_global/vars/v1_5.yaml b/tests/integration/targets/vyos_logging_global/vars/v1_5.yaml new file mode 100644 index 00000000..69c477d3 --- /dev/null +++ b/tests/integration/targets/vyos_logging_global/vars/v1_5.yaml @@ -0,0 +1,122 @@ +--- +merged: + before: {} + commands: + - set system syslog console facility all + - set system syslog local facility cron level debug + after: + console: + facilities: + - facility: all + global_params: + facilities: + - facility: cron + severity: debug + +overridden: + commands: + - delete system syslog local facility cron + - delete system syslog local facility local7 + - delete system syslog remote 172.16.2.12 protocol udp + - delete system syslog remote 172.16.2.15 protocol udp + - delete system syslog remote 172.16.2.12 + - delete system syslog remote 172.16.2.15 + + after: + console: + facilities: + - facility: all + - facility: local7 + severity: err + - facility: news + severity: debug + files: + - path: Myfile + +populate_logging_global_hosts: + - hostname: 172.16.2.15 + facilities: + - facility: all + severity: all + - facility: all + protocol: udp + - hostname: 172.16.2.12 + facilities: + - facility: all + protocol: udp + +rendered: + commands: + - set system syslog console facility all + - set system syslog remote 172.16.2.15 facility all level all + - set system syslog local facility cron level debug + +deleted: + commands: + - delete system syslog + after: {} + +parsed: + after: + console: + facilities: + - facility: all + - facility: news + severity: debug + global_params: + marker_interval: 111 + preserve_fqdn: true + facilities: + - facility: cron + severity: debug + - facility: local7 + severity: debug + hosts: + - hostname: 10.0.2.12 + facilities: + - facility: all + severity: info + - hostname: 10.0.2.15 + port: 514 + protocol: udp + facilities: + - facility: all + severity: all + +populate_config: + console: + facilities: + - facility: all + - facility: local7 + severity: err + - facility: news + severity: debug + files: + - path: def + archive: + file_num: 2 + facilities: + - facility: local6 + severity: emerg + - facility: local7 + severity: emerg + hosts: "{{ populate_logging_global_hosts }}" + users: + - username: vyos + facilities: + - facility: local7 + severity: debug + - facility: local6 + severity: alert + - username: paul + facilities: + - facility: local7 + severity: err + global_params: + facilities: + - facility: cron + severity: debug + - facility: local7 + severity: debug + +parsed_config_file: "_parsed_15.cfg" diff --git a/tests/unit/modules/network/vyos/test_vyos_logging_global.py b/tests/unit/modules/network/vyos/test_vyos_logging_global.py index a6751518..54288b6e 100644 --- a/tests/unit/modules/network/vyos/test_vyos_logging_global.py +++ b/tests/unit/modules/network/vyos/test_vyos_logging_global.py @@ -1,451 +1,465 @@ # # (c) 2021, Ansible by Red Hat, inc # 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 from textwrap import dedent from unittest.mock import patch from ansible_collections.vyos.vyos.plugins.modules import vyos_logging_global from ansible_collections.vyos.vyos.tests.unit.modules.utils import set_module_args from .vyos_module import TestVyosModule class TestVyosLoggingGlobalModule(TestVyosModule): module = vyos_logging_global def setUp(self): super(TestVyosLoggingGlobalModule, self).setUp() self.mock_get_resource_connection_config = patch( "ansible_collections.ansible.netcommon.plugins.module_utils.network.common.rm_base.resource_module_base.get_resource_connection", ) self.get_resource_connection_config = self.mock_get_resource_connection_config.start() self.mock_get_resource_connection_facts = patch( "ansible_collections.ansible.netcommon.plugins.module_utils.network.common.facts.facts.get_resource_connection", ) self.get_resource_connection_facts = self.mock_get_resource_connection_facts.start() self.mock_execute_show_command = patch( "ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.logging_global.logging_global.Logging_globalFacts.get_logging_data", ) self.execute_show_command = self.mock_execute_show_command.start() + self.mock_get_os_version = patch( + "ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.config.logging_global.logging_global.get_os_version", + ) + self.get_os_version = self.mock_get_os_version.start() + self.get_os_version.return_value = "1.3" + + self.mock_facts_get_os_version = patch( + "ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.logging_global.logging_global.get_os_version", + ) + self.get_facts_os_version = self.mock_facts_get_os_version.start() + self.get_facts_os_version.return_value = "1.3" + def tearDown(self): super(TestVyosLoggingGlobalModule, self).tearDown() self.mock_get_resource_connection_config.stop() self.mock_get_resource_connection_facts.stop() self.mock_execute_show_command.stop() + self.mock_get_os_version.stop() + self.mock_facts_get_os_version.stop() def test_vyos_logging_global_merged_idempotent(self): self.execute_show_command.return_value = dedent( """\ set system syslog console facility all set system syslog console facility local7 level 'err' set system syslog console facility news level 'debug' set system syslog file xyz set system syslog file abc archive size '125' set system syslog file def archive file '2' set system syslog file def facility local6 level 'emerg' set system syslog file def facility local7 level 'emerg' set system syslog global archive file '2' set system syslog global archive size '111' set system syslog global facility cron level 'debug' set system syslog global facility local7 level 'debug' set system syslog global marker interval '111' set system syslog global preserve-fqdn set system syslog host 10.0.2.12 facility all protocol 'udp' set system syslog host 10.0.2.15 facility all level 'all' set system syslog host 10.0.2.15 facility all protocol 'udp' set system syslog host 10.0.2.15 port '122' set system syslog user paul facility local7 level 'err' set system syslog user vyos facility local6 level 'alert' set system syslog user vyos facility local7 level 'debug' """, ) playbook = dict( config=dict( console=dict( facilities=[ dict(facility="all"), dict(facility="local7", severity="err"), dict(facility="news", severity="debug"), ], ), files=[ dict(path="xyz"), dict(path="abc", archive=dict(size=125)), dict( path="def", archive=dict(file_num=2), facilities=[ dict(facility="local6", severity="emerg"), dict(facility="local7", severity="emerg"), ], ), ], hosts=[ dict( hostname="10.0.2.15", port=122, facilities=[ dict(facility="all", severity="all"), dict(facility="all", protocol="udp"), ], ), dict( hostname="10.0.2.12", facilities=[dict(facility="all", protocol="udp")], ), ], users=[ dict( username="vyos", facilities=[ dict(facility="local7", severity="debug"), dict(facility="local6", severity="alert"), ], ), dict( username="paul", facilities=[dict(facility="local7", severity="err")], ), ], global_params=dict( archive=dict(size=111, file_num=2), marker_interval=111, - preserve_fqdn="True", + preserve_fqdn=True, facilities=[ dict(facility="cron", severity="debug"), dict(facility="local7", severity="debug"), ], ), ), ) compare_cmds = [] playbook["state"] = "merged" set_module_args(playbook) result = self.execute_module() self.maxDiff = None self.assertEqual(sorted(result["commands"]), sorted(compare_cmds)) def test_vyos_logging_global_merged(self): self.execute_show_command.return_value = dedent( """\ """, ) playbook = dict( config=dict( console=dict( facilities=[ dict(facility="all"), dict(facility="local7", severity="err"), dict(facility="news", severity="debug"), ], ), files=[ dict(path="xyz"), dict(path="abc", archive=dict(size=125)), dict( path="def", archive=dict(file_num=2), facilities=[ dict(facility="local6", severity="emerg"), dict(facility="local7", severity="emerg"), ], ), ], hosts=[ dict( hostname="10.0.2.15", port=122, facilities=[ dict(facility="all", severity="all"), dict(facility="all", protocol="udp"), ], ), dict( hostname="10.0.2.12", facilities=[dict(facility="all", protocol="udp")], ), ], users=[ dict( username="vyos", facilities=[ dict(facility="local7", severity="debug"), dict(facility="local6", severity="alert"), ], ), dict( username="paul", facilities=[dict(facility="local7", severity="err")], ), ], global_params=dict( archive=dict(size=111, file_num=2), marker_interval=111, - preserve_fqdn="True", + preserve_fqdn=True, facilities=[ dict(facility="cron", severity="debug"), dict(facility="local7", severity="debug"), ], ), ), ) compare_cmds = [ "set system syslog user paul facility local7 level err", "set system syslog host 10.0.2.15 facility all protocol udp", "set system syslog global marker interval 111", "set system syslog file def archive file 2", "set system syslog file abc archive size 125", "set system syslog console facility local7 level err", "set system syslog host 10.0.2.12 facility all protocol udp", "set system syslog global facility local7 level debug", "set system syslog host 10.0.2.15 facility all level all", "set system syslog global preserve-fqdn", "set system syslog global archive file 2", "set system syslog console facility all", "set system syslog file xyz", "set system syslog file def facility local7 level emerg", "set system syslog console facility news level debug", "set system syslog user vyos facility local6 level alert", "set system syslog global facility cron level debug", "set system syslog file def facility local6 level emerg", "set system syslog global archive size 111", "set system syslog host 10.0.2.15 port 122", "set system syslog user vyos facility local7 level debug", ] playbook["state"] = "merged" set_module_args(playbook) result = self.execute_module(changed=True) self.maxDiff = None self.assertEqual(sorted(result["commands"]), sorted(compare_cmds)) def test_vyos_logging_global_deleted(self): self.execute_show_command.return_value = dedent( """\ set system syslog console facility all set system syslog console facility local7 level 'err' set system syslog console facility news level 'debug' set system syslog file xyz set system syslog file abc archive size '125' set system syslog file def archive file '2' set system syslog file def facility local6 level 'emerg' set system syslog file def facility local7 level 'emerg' set system syslog global archive file '2' set system syslog global archive size '111' set system syslog global facility cron level 'debug' set system syslog global facility local7 level 'debug' set system syslog global marker interval '111' set system syslog global preserve-fqdn set system syslog host 10.0.2.12 facility all protocol 'udp' set system syslog host 10.0.2.15 facility all level 'all' set system syslog host 10.0.2.15 facility all protocol 'udp' set system syslog host 10.0.2.15 port '122' set system syslog user paul facility local7 level 'err' set system syslog user vyos facility local6 level 'alert' set system syslog user vyos facility local7 level 'debug' """, ) playbook = dict(config=dict()) compare_cmds = ["delete system syslog"] playbook["state"] = "deleted" set_module_args(playbook) result = self.execute_module(changed=True) self.maxDiff = None self.assertEqual(sorted(result["commands"]), sorted(compare_cmds)) def test_vyos_logging_global_replaced(self): """ passing all commands as have and expecting [] commands """ self.execute_show_command.return_value = dedent( """\ set system syslog console facility all set system syslog console facility local7 level 'err' set system syslog console facility news level 'debug' set system syslog file xyz set system syslog file abc archive size '125' set system syslog file def archive file '2' set system syslog file def facility local6 level 'emerg' set system syslog file def facility local7 level 'emerg' set system syslog global archive file '2' set system syslog global archive size '111' set system syslog global facility cron level 'debug' set system syslog global facility local7 level 'debug' set system syslog global marker interval '111' set system syslog global preserve-fqdn set system syslog host 10.0.2.12 facility all protocol 'udp' set system syslog host 10.0.2.15 facility all level 'all' set system syslog host 10.0.2.15 facility all protocol 'udp' set system syslog host 10.0.2.15 port '122' set system syslog user paul facility local7 level 'err' set system syslog user vyos facility local6 level 'alert' set system syslog user vyos facility local7 level 'debug' """, ) playbook = dict( config=dict( console=dict(facilities=[dict(facility="local7", severity="emerg")]), files=[ dict( path="abc", archive=dict(file_num=2), facilities=[ dict(facility="local6", severity="err"), dict(facility="local7", severity="emerg"), ], ), ], ), ) compare_cmds = [ "delete system syslog console facility all", "delete system syslog console facility local7", "delete system syslog console facility news", "delete system syslog file def", "delete system syslog file xyz", "delete system syslog global facility cron", "delete system syslog global facility local7", "delete system syslog global archive file 2", "delete system syslog global archive size 111", "delete system syslog global marker", "delete system syslog global preserve-fqdn", "delete system syslog host 10.0.2.12", "delete system syslog host 10.0.2.15", "delete system syslog user paul", "delete system syslog user vyos", "set system syslog console facility local7 level emerg", "set system syslog file abc facility local6 level err", "set system syslog file abc facility local7 level emerg", "delete system syslog file abc archive size 125", "set system syslog file abc archive file 2", ] playbook["state"] = "replaced" set_module_args(playbook) result = self.execute_module(changed=True) self.maxDiff = None self.assertEqual(sorted(result["commands"]), sorted(compare_cmds)) def test_vyos_logging_global_replaced_idempotent(self): """ passing all commands as have and expecting [] commands """ self.execute_show_command.return_value = dedent( """\ set system syslog console facility local6 """, ) playbook = dict(config=dict(console=dict(facilities=[dict(facility="local6")]))) compare_cmds = [] playbook["state"] = "replaced" set_module_args(playbook) result = self.execute_module(changed=False) self.maxDiff = None self.assertEqual(sorted(result["commands"]), sorted(compare_cmds)) def test_vyos_logging_global_overridden(self): self.execute_show_command.return_value = dedent( """\ set system syslog console set system syslog global """, ) playbook = dict( config=dict( console=dict(facilities=[dict(facility="local7", severity="emerg")]), files=[ dict( path="abc", archive=dict(file_num=2), facilities=[ dict(facility="local6", severity="err"), dict(facility="local7", severity="emerg"), ], ), ], ), ) compare_cmds = [ "set system syslog console facility local7 level emerg", "set system syslog file abc facility local6 level err", "set system syslog file abc facility local7 level emerg", "set system syslog file abc archive file 2", ] playbook["state"] = "overridden" set_module_args(playbook) result = self.execute_module(changed=True) self.maxDiff = None self.assertEqual(sorted(result["commands"]), sorted(compare_cmds)) def test_vyos_logging_global_rendered(self): playbook = dict( config=dict( console=dict(facilities=[dict(facility="all")]), hosts=[ dict( hostname="10.0.2.16", facilities=[dict(facility="local6")], ), ], users=[ dict(username="vyos"), dict(username="paul", facilities=[dict(facility="local7")]), ], ), ) compare_cmds = [ "set system syslog console facility all", "set system syslog host 10.0.2.16 facility local6", "set system syslog user vyos", "set system syslog user paul facility local7", ] playbook["state"] = "rendered" set_module_args(playbook) result = self.execute_module() self.maxDiff = None self.assertEqual(sorted(result["rendered"]), sorted(compare_cmds)) def test_vyos_logging_global_parsed(self): set_module_args( dict( running_config=dedent( """\ set system syslog console facility all set system syslog file xyz """, ), state="parsed", ), ) parsed = dict( console=dict(facilities=[dict(facility="all")]), files=[dict(path="xyz")], ) result = self.execute_module(changed=False) self.maxDiff = None self.assertEqual(sorted(result["parsed"]), sorted(parsed)) def test_vyos_logging_global_gathered(self): self.execute_show_command.return_value = dedent( """\ set system syslog console facility all """, ) set_module_args(dict(state="gathered")) gathered = dict(console=dict(facilities=[dict(facility="all")])) result = self.execute_module(changed=False) self.maxDiff = None self.assertEqual(sorted(result["gathered"]), sorted(gathered)) diff --git a/tests/unit/modules/network/vyos/test_vyos_logging_global15.py b/tests/unit/modules/network/vyos/test_vyos_logging_global15.py new file mode 100644 index 00000000..63b3fd31 --- /dev/null +++ b/tests/unit/modules/network/vyos/test_vyos_logging_global15.py @@ -0,0 +1,353 @@ +# +# (c) 2021, Ansible by Red Hat, inc +# 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 + +from textwrap import dedent +from unittest.mock import patch + +from ansible_collections.vyos.vyos.plugins.modules import vyos_logging_global +from ansible_collections.vyos.vyos.tests.unit.modules.utils import set_module_args + +from .vyos_module import TestVyosModule + + +class TestVyosLoggingGlobalModule(TestVyosModule): + module = vyos_logging_global + + def setUp(self): + super(TestVyosLoggingGlobalModule, self).setUp() + + self.mock_get_resource_connection_config = patch( + "ansible_collections.ansible.netcommon.plugins.module_utils.network.common.rm_base.resource_module_base.get_resource_connection", + ) + self.get_resource_connection_config = self.mock_get_resource_connection_config.start() + + self.mock_get_resource_connection_facts = patch( + "ansible_collections.ansible.netcommon.plugins.module_utils.network.common.facts.facts.get_resource_connection", + ) + self.get_resource_connection_facts = self.mock_get_resource_connection_facts.start() + + self.mock_execute_show_command = patch( + "ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.logging_global.logging_global.Logging_globalFacts.get_logging_data", + ) + self.execute_show_command = self.mock_execute_show_command.start() + + self.mock_get_os_version = patch( + "ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.config.logging_global.logging_global.get_os_version", + ) + self.get_os_version = self.mock_get_os_version.start() + self.get_os_version.return_value = "1.5" + + self.mock_facts_get_os_version = patch( + "ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.logging_global.logging_global.get_os_version", + ) + self.get_facts_os_version = self.mock_facts_get_os_version.start() + self.get_facts_os_version.return_value = "1.5" + + def tearDown(self): + super(TestVyosLoggingGlobalModule, self).tearDown() + self.mock_get_resource_connection_config.stop() + self.mock_get_resource_connection_facts.stop() + self.mock_execute_show_command.stop() + self.mock_get_os_version.stop() + self.mock_facts_get_os_version.stop() + + def test_vyos_logging_global_merged_idempotent(self): + self.execute_show_command.return_value = dedent( + """\ + set system syslog console facility all + set system syslog console facility local7 level 'err' + set system syslog console facility news level 'debug' + set system syslog local facility cron level 'debug' + set system syslog local facility local7 level 'debug' + set system syslog marker interval '111' + set system syslog preserve-fqdn + set system syslog remote 10.0.2.12 facility all + set system syslog remote 10.0.2.15 facility all level 'all' + set system syslog remote 10.0.2.15 port '122' + """, + ) + playbook = dict( + config=dict( + console=dict( + facilities=[ + dict(facility="all"), + dict(facility="local7", severity="err"), + dict(facility="news", severity="debug"), + ], + ), + hosts=[ + dict( + hostname="10.0.2.15", + port=122, + facilities=[dict(facility="all", severity="all")], + ), + dict( + hostname="10.0.2.12", + facilities=[dict(facility="all")], + ), + ], + global_params=dict( + marker_interval=111, + preserve_fqdn=True, + facilities=[ + dict(facility="cron", severity="debug"), + dict(facility="local7", severity="debug"), + ], + ), + ), + ) + compare_cmds = [] + playbook["state"] = "merged" + set_module_args(playbook) + result = self.execute_module() + self.maxDiff = None + self.assertEqual(sorted(result["commands"]), sorted(compare_cmds)) + + def test_vyos_logging_global_merged(self): + self.execute_show_command.return_value = dedent( + """\ + """, + ) + playbook = dict( + config=dict( + console=dict( + facilities=[ + dict(facility="all"), + dict(facility="local7", severity="err"), + dict(facility="news", severity="debug"), + ], + ), + hosts=[ + dict( + hostname="10.0.2.15", + port=122, + facilities=[dict(facility="all", severity="all")], + ), + dict( + hostname="10.0.2.12", + facilities=[dict(facility="all", severity="info")], + ), + ], + global_params=dict( + marker_interval=111, + preserve_fqdn=True, + facilities=[ + dict(facility="cron", severity="debug"), + dict(facility="local7", severity="debug"), + ], + ), + ), + ) + compare_cmds = [ + "set system syslog console facility all", + "set system syslog console facility local7 level err", + "set system syslog console facility news level debug", + "set system syslog local facility cron level debug", + "set system syslog local facility local7 level debug", + "set system syslog marker interval 111", + "set system syslog preserve-fqdn", + "set system syslog remote 10.0.2.15 facility all level all", + "set system syslog remote 10.0.2.15 port 122", + "set system syslog remote 10.0.2.12 facility all level info", + ] + playbook["state"] = "merged" + set_module_args(playbook) + result = self.execute_module(changed=True) + self.maxDiff = None + self.assertEqual(sorted(result["commands"]), sorted(compare_cmds)) + + def test_vyos_logging_global_deleted(self): + self.execute_show_command.return_value = dedent( + """\ + set system syslog console facility all + set system syslog console facility local7 level 'err' + set system syslog local facility cron level 'debug' + set system syslog local facility local7 level 'debug' + set system syslog marker interval '111' + set system syslog preserve-fqdn + set system syslog remote 10.0.2.12 facility all + set system syslog remote 10.0.2.15 facility all level 'all' + set system syslog remote 10.0.2.15 port '122' + """, + ) + playbook = dict(config=dict()) + compare_cmds = ["delete system syslog"] + playbook["state"] = "deleted" + set_module_args(playbook) + result = self.execute_module(changed=True) + self.maxDiff = None + self.assertEqual(sorted(result["commands"]), sorted(compare_cmds)) + + def test_vyos_logging_global_replaced(self): + self.execute_show_command.return_value = dedent( + """\ + set system syslog console facility all + set system syslog console facility local7 level 'err' + set system syslog console facility news level 'debug' + set system syslog local facility cron level 'debug' + set system syslog local facility local7 level 'debug' + set system syslog marker interval '111' + set system syslog preserve-fqdn + set system syslog remote 10.0.2.12 facility all + set system syslog remote 10.0.2.15 facility all level 'all' + set system syslog remote 10.0.2.15 port '122' + """, + ) + playbook = dict( + config=dict( + console=dict( + facilities=[dict(facility="local7", severity="emerg")], + ), + hosts=[ + dict( + hostname="10.0.2.15", + port=122, + facilities=[dict(facility="all", severity="all")], + ), + ], + ), + ) + compare_cmds = [ + "delete system syslog console facility all", + "delete system syslog console facility local7", + "delete system syslog console facility news", + "delete system syslog local facility cron", + "delete system syslog local facility local7", + "delete system syslog marker", + "delete system syslog preserve-fqdn", + "delete system syslog remote 10.0.2.12", + "set system syslog console facility local7 level emerg", + ] + playbook["state"] = "replaced" + set_module_args(playbook) + result = self.execute_module(changed=True) + self.maxDiff = None + self.assertEqual(sorted(result["commands"]), sorted(compare_cmds)) + + def test_vyos_logging_global_replaced_idempotent(self): + self.execute_show_command.return_value = dedent( + """\ + set system syslog console facility local6 + """, + ) + playbook = dict(config=dict(console=dict(facilities=[dict(facility="local6")]))) + compare_cmds = [] + playbook["state"] = "replaced" + set_module_args(playbook) + result = self.execute_module(changed=False) + self.maxDiff = None + self.assertEqual(sorted(result["commands"]), sorted(compare_cmds)) + + def test_vyos_logging_global_overridden(self): + self.execute_show_command.return_value = dedent( + """\ + set system syslog console + set system syslog local + """, + ) + playbook = dict( + config=dict( + console=dict( + facilities=[dict(facility="local7", severity="emerg")], + ), + global_params=dict( + facilities=[dict(facility="all", severity="info")], + ), + ), + ) + compare_cmds = [ + "set system syslog console facility local7 level emerg", + "set system syslog local facility all level info", + ] + playbook["state"] = "overridden" + set_module_args(playbook) + result = self.execute_module(changed=True) + self.maxDiff = None + self.assertEqual(sorted(result["commands"]), sorted(compare_cmds)) + + def test_vyos_logging_global_rendered(self): + playbook = dict( + config=dict( + console=dict(facilities=[dict(facility="all")]), + hosts=[ + dict( + hostname="10.0.2.16", + facilities=[dict(facility="local6")], + ), + ], + global_params=dict( + preserve_fqdn=True, + facilities=[dict(facility="all", severity="info")], + ), + ), + ) + compare_cmds = [ + "set system syslog console facility all", + "set system syslog remote 10.0.2.16 facility local6", + "set system syslog preserve-fqdn", + "set system syslog local facility all level info", + ] + playbook["state"] = "rendered" + set_module_args(playbook) + result = self.execute_module() + self.maxDiff = None + self.assertEqual(sorted(result["rendered"]), sorted(compare_cmds)) + + def test_vyos_logging_global_parsed(self): + set_module_args( + dict( + running_config=dedent( + """\ + set system syslog console facility all + set system syslog local facility all level 'info' + set system syslog remote 10.0.2.1 facility all level 'info' + set system syslog remote 10.0.2.1 port '514' + """, + ), + state="parsed", + ), + ) + parsed = dict( + console=dict(facilities=[dict(facility="all")]), + global_params=dict(facilities=[dict(facility="all", severity="info")]), + hosts=[ + dict( + hostname="10.0.2.1", + port=514, + facilities=[dict(facility="all", severity="info")], + ), + ], + ) + result = self.execute_module(changed=False) + self.maxDiff = None + self.assertEqual(result["parsed"], parsed) + + def test_vyos_logging_global_gathered(self): + self.execute_show_command.return_value = dedent( + """\ + set system syslog console facility all + set system syslog local facility all level 'info' + set system syslog remote 10.0.2.1 facility all level 'info' + """, + ) + set_module_args(dict(state="gathered")) + gathered = dict( + console=dict(facilities=[dict(facility="all")]), + global_params=dict(facilities=[dict(facility="all", severity="info")]), + hosts=[ + dict( + hostname="10.0.2.1", + facilities=[dict(facility="all", severity="info")], + ), + ], + ) + result = self.execute_module(changed=False) + self.maxDiff = None + self.assertEqual(result["gathered"], gathered)