Fix for memory fact gathering

I have a host which started to fail while gathering facts after the addition
of expanded memory facts in PR #9839:

Traceback (most recent call last):
  File "/home/ansible/.ansible/tmp/ansible-tmp-1422536976.05-133253824703289/setup", line 4278, in <module>
    main()
  File "/home/ansible/.ansible/tmp/ansible-tmp-1422536976.05-133253824703289/setup", line 137, in main
    data = run_setup(module)
  File "/home/ansible/.ansible/tmp/ansible-tmp-1422536976.05-133253824703289/setup", line 81, in run_setup
    facts = ansible_facts(module)
  File "/home/ansible/.ansible/tmp/ansible-tmp-1422536976.05-133253824703289/setup", line 4217, in ansible_facts
    facts.update(Hardware().populate())
  File "/home/ansible/.ansible/tmp/ansible-tmp-1422536976.05-133253824703289/setup", line 2339, in populate
    self.get_memory_facts()
  File "/home/ansible/.ansible/tmp/ansible-tmp-1422536976.05-133253824703289/setup", line 2375, in get_memory_facts
    'cached': memstats['swapcached']
KeyError: 'swapcached'

My problem host doesn't have SwapCached in /proc/meminfo. It may be better to
set defaults for these keys, since the values provided by /proc/meminfo can
change from version to version.
pull/10129/head
Patrick McConnell 10 years ago
parent fef435cc2a
commit 0c3a273805

@ -635,19 +635,19 @@ class LinuxHardware(Hardware):
memstats[key.lower()] = long(val) / 1024 memstats[key.lower()] = long(val) / 1024
self.facts['memory_mb'] = { self.facts['memory_mb'] = {
'real' : { 'real' : {
'total': memstats['memtotal'], 'total': memstats.get('memtotal', 0),
'used': (memstats['memtotal'] - memstats['memfree']), 'used': (memstats.get('memtotal', 0) - memstats.get('memfree', 0)),
'free': memstats['memfree'] 'free': memstats.get('memfree', 0)
}, },
'nocache' : { 'nocache' : {
'free': memstats['cached'] + memstats['memfree'] + memstats['buffers'], 'free': memstats.get('cached', 0) + memstats.get('memfree', 0) + memstats.get('buffers', 0),
'used': memstats['memtotal'] - (memstats['cached'] + memstats['memfree'] + memstats['buffers']) 'used': memstats.get('memtotal', 0) - (memstats.get('cached', 0) + memstats.get('memfree', 0) + memstats.get('buffers', 0))
}, },
'swap' : { 'swap' : {
'total': memstats['swaptotal'], 'total': memstats.get('swaptotal', 0),
'free': memstats['swapfree'], 'free': memstats.get('swapfree', 0),
'used': memstats['swaptotal'] - memstats['swapfree'], 'used': memstats.get('swaptotal', 0) - memstats.get('swapfree', 0),
'cached': memstats['swapcached'] 'cached': memstats.get('swapcached', 0)
} }
} }

Loading…
Cancel
Save