diff --git a/changelogs/fragments/23-idempotent_config.yml b/changelogs/fragments/23-idempotent_config.yml new file mode 100644 index 00000000..b43c5846 --- /dev/null +++ b/changelogs/fragments/23-idempotent_config.yml @@ -0,0 +1,2 @@ +minor_changes: + - fact - add fact ``ansible_net_config_nonverbose`` to get idempotent config (no date, no verbose) (https://github.com/ansible-collections/community.routeros/pull/23). diff --git a/plugins/modules/facts.py b/plugins/modules/facts.py index 4a4db7c3..6b37f23d 100644 --- a/plugins/modules/facts.py +++ b/plugins/modules/facts.py @@ -110,6 +110,16 @@ returned: when config is configured type: str + ansible_net_config_nonverbose: + description: + - The current active config from the device in minimal form. + - This value is idempotent in the sense that if the facts module is run twice and the device's config + was not changed between the runs, the value is identical. This is achieved by running C(/export) + and stripping the timestamp from the comment in the first line. + returned: when config is configured + type: str + version_added: 1.2.0 + # interfaces ansible_net_all_ipv4_addresses: description: All IPv4 addresses configured on the device @@ -280,14 +290,26 @@ def to_megabytes(self, data): class Config(FactsBase): - COMMANDS = ['/export verbose'] + COMMANDS = [ + '/export verbose', + '/export', + ] + + RM_DATE_RE = re.compile(r'^# [a-z0-9/][a-z0-9/]* [0-9:]* by RouterOS') def populate(self): super(Config, self).populate() + data = self.responses[0] if data: self.facts['config'] = data + data = self.responses[1] + if data: + # remove datetime + data = re.sub(self.RM_DATE_RE, r'# RouterOS', data) + self.facts['config_nonverbose'] = data + class Interfaces(FactsBase): diff --git a/tests/unit/plugins/modules/fixtures/facts/export b/tests/unit/plugins/modules/fixtures/facts/export new file mode 100644 index 00000000..d1ac4914 --- /dev/null +++ b/tests/unit/plugins/modules/fixtures/facts/export @@ -0,0 +1,24 @@ +# sep/25/2018 10:10:52 by RouterOS 6.42.5 +# software id = 9EER-511K +# +# +# +/interface wireless security-profiles +set [ find default=yes ] supplicant-identity=MikroTik +/tool user-manager customer +set admin access=own-routers,own-users,own-profiles,own-limits,config-payment-gw +/ip address +add address=192.168.88.1/24 comment=defconf interface=ether1 network=192.168.88.0 +/ip dhcp-client +add dhcp-options=hostname,clientid disabled=no interface=ether1 +/system lcd page +set time disabled=yes display-time=5s +set resources disabled=yes display-time=5s +set uptime disabled=yes display-time=5s +set packets disabled=yes display-time=5s +set bits disabled=yes display-time=5s +set version disabled=yes display-time=5s +set identity disabled=yes display-time=5s +set ether1 disabled=yes display-time=5s +/tool user-manager database +set db-path=user-manager diff --git a/tests/unit/plugins/modules/test_facts.py b/tests/unit/plugins/modules/test_facts.py index 25fb04e2..ea111df6 100644 --- a/tests/unit/plugins/modules/test_facts.py +++ b/tests/unit/plugins/modules/test_facts.py @@ -94,6 +94,10 @@ def test_facts_config(self): result['ansible_facts']['ansible_net_config'], str ) + self.assertIsInstance( + result['ansible_facts']['ansible_net_config_nonverbose'], str + ) + def test_facts_interfaces(self): set_module_args(dict(gather_subset='interfaces')) result = self.execute_module()