1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

ios_facts: Report space of file systems (#41850)

* ios_facts: Report file system space

Parse total and free space from dir output. For this, add a hash
filesystems_info containing the keys spacetotal_kb and spacefree_kb.

* ios_facts: Add unit test for file system space reporting

* ios_facts: Add integration test for file system space reporting
This commit is contained in:
Paul Neumann 2018-07-09 15:15:31 +02:00 committed by Nathaniel Case
parent dbb58b34c3
commit dfb2b3fdd5
7 changed files with 62 additions and 0 deletions

View file

@ -105,6 +105,10 @@ ansible_net_filesystems:
description: All file system names available on the device description: All file system names available on the device
returned: when hardware is configured returned: when hardware is configured
type: list type: list
ansible_net_filesystems_info:
description: A hash of all file systems containing info about each file system (e.g. free and total space)
returned: when hardware is configured
type: dict
ansible_net_memfree_mb: ansible_net_memfree_mb:
description: The available free memory on the remote device in Mb description: The available free memory on the remote device in Mb
returned: when hardware is configured returned: when hardware is configured
@ -225,6 +229,7 @@ class Hardware(FactsBase):
data = self.responses[0] data = self.responses[0]
if data: if data:
self.facts['filesystems'] = self.parse_filesystems(data) self.facts['filesystems'] = self.parse_filesystems(data)
self.facts['filesystems_info'] = self.parse_filesystems_info(data)
data = self.responses[1] data = self.responses[1]
if data: if data:
@ -241,6 +246,21 @@ class Hardware(FactsBase):
def parse_filesystems(self, data): def parse_filesystems(self, data):
return re.findall(r'^Directory of (\S+)/', data, re.M) return re.findall(r'^Directory of (\S+)/', data, re.M)
def parse_filesystems_info(self, data):
facts = dict()
fs = ''
for line in data.split('\n'):
match = re.match(r'^Directory of (\S+)/', line)
if match:
fs = match.group(1)
facts[fs] = dict()
continue
match = re.match(r'^(\d+) bytes total \((\d+) bytes free\)', line)
if match:
facts[fs]['spacetotal_kb'] = int(match.group(1)) / 1024
facts[fs]['spacefree_kb'] = int(match.group(2)) / 1024
return facts
class Config(FactsBase): class Config(FactsBase):

View file

@ -28,4 +28,8 @@
- "result.ansible_facts.ansible_net_memfree_mb > 1" - "result.ansible_facts.ansible_net_memfree_mb > 1"
- "result.ansible_facts.ansible_net_memtotal_mb > 1" - "result.ansible_facts.ansible_net_memtotal_mb > 1"
- assert:
that: "{{ item.value.spacetotal_kb }} > {{ item.value.spacefree_kb }}"
loop: "{{ lookup('dict', result.ansible_facts.ansible_net_filesystems_info, wantlist=True) }}"
- debug: msg="END cli/all_facts.yaml on connection={{ ansible_connection }}" - debug: msg="END cli/all_facts.yaml on connection={{ ansible_connection }}"

View file

@ -28,4 +28,8 @@
# ... and not present # ... and not present
- "result.ansible_facts.ansible_net_config is not defined" # config - "result.ansible_facts.ansible_net_config is not defined" # config
- assert:
that: "{{ item.value.spacetotal_kb }} > {{ item.value.spacefree_kb }}"
loop: "{{ lookup('dict', result.ansible_facts.ansible_net_filesystems_info, wantlist=True) }}"
- debug: msg="END cli/default.yaml on connection={{ ansible_connection }}" - debug: msg="END cli/default.yaml on connection={{ ansible_connection }}"

View file

@ -26,5 +26,6 @@
- "result.ansible_facts.ansible_net_interfaces | length > 1" # more than one interface returned - "result.ansible_facts.ansible_net_interfaces | length > 1" # more than one interface returned
# ... and not present # ... and not present
- "result.ansible_facts.ansible_net_filesystems is not defined" - "result.ansible_facts.ansible_net_filesystems is not defined"
- "result.ansible_facts.ansible_net_filesystems_info is not defined"
- debug: msg="END cli/not_hardware_facts.yaml on connection={{ ansible_connection }}" - debug: msg="END cli/not_hardware_facts.yaml on connection={{ ansible_connection }}"

View file

@ -0,0 +1,23 @@
Directory of bootflash:/
11 drwx 16384 Jun 1 2017 13:03:27 +00:00 lost+found
325121 drwx 4096 Jun 1 2017 13:03:54 +00:00 .super.iso.dir
12 -rw- 31 Jun 22 2018 15:17:06 +00:00 .CsrLxc_LastInstall
13 -rw- 69 Jun 1 2017 13:05:53 +00:00 virtual-instance.conf
438913 drwx 4096 Jun 1 2017 13:04:57 +00:00 core
15 -rw- 125736960 Jun 1 2017 13:03:54 +00:00 iosxe-remote-mgmt.16.03.04.ova
105667 -rw- 292164568 Jun 1 2017 13:04:04 +00:00 csr1000v-mono-universalk9.16.03.04.SPA.pkg
105668 -rw- 34370768 Jun 1 2017 13:04:10 +00:00 csr1000v-rpboot.16.03.04.SPA.pkg
105666 -rw- 5317 Jun 1 2017 13:04:10 +00:00 packages.conf
195073 drwx 4096 Jun 1 2017 13:04:51 +00:00 .prst_sync
414529 drwx 4096 Jun 1 2017 13:04:57 +00:00 .rollback_timer
16 -rw- 0 Jun 1 2017 13:05:00 +00:00 tracelogs.kZn
16257 drwx 24576 Jun 22 2018 16:03:11 +00:00 tracelogs
349505 drwx 4096 Jun 1 2017 13:05:08 +00:00 .installer
292609 drwx 4096 Jun 1 2017 13:05:59 +00:00 virtual-instance
17 -rw- 30 Jun 22 2018 15:17:59 +00:00 throughput_monitor_params
48769 drwx 4096 Jun 1 2017 13:06:04 +00:00 onep
19 -rw- 376 Jun 22 2018 15:18:11 +00:00 csrlxc-cfg.log
20 -rw- 0 Jun 22 2018 15:17:59 +00:00 cvac.log
7897796608 bytes total (6608056320 bytes free)

View file

@ -77,3 +77,13 @@ class TestIosFactsModule(TestIosModule):
self.assertIsNone( self.assertIsNone(
result['ansible_facts']['ansible_net_interfaces']['Tunnel1110']['macaddress'] result['ansible_facts']['ansible_net_interfaces']['Tunnel1110']['macaddress']
) )
def test_ios_facts_filesystems_info(self):
set_module_args(dict(gather_subset='hardware'))
result = self.execute_module()
self.assertEqual(
result['ansible_facts']['ansible_net_filesystems_info']['bootflash:']['spacetotal_kb'], 7712692.0
)
self.assertEqual(
result['ansible_facts']['ansible_net_filesystems_info']['bootflash:']['spacefree_kb'], 6453180.0
)