Fix local facts unreadable (#76095)

* Catch exceptions during stat calls

If such an exception is not caught it causes the complete
local facts gathering to fail.
pull/78811/head
Ruediger Pluem 2 years ago committed by GitHub
parent 9b8e60c36d
commit 1d410ca700
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,6 @@
bugfixes:
- >-
local facts - if a local fact in the facts directory cannot be stated,
store an error message as the fact value and emit a warning just as if
just as if the facts execution has failed. The stat can fail e.g. on
dangling symlinks.

@ -50,8 +50,15 @@ class LocalFactCollector(BaseFactCollector):
for fn in sorted(glob.glob(fact_path + '/*.fact')): for fn in sorted(glob.glob(fact_path + '/*.fact')):
# use filename for key where it will sit under local facts # use filename for key where it will sit under local facts
fact_base = os.path.basename(fn).replace('.fact', '') fact_base = os.path.basename(fn).replace('.fact', '')
if stat.S_IXUSR & os.stat(fn)[stat.ST_MODE]:
failed = None failed = None
try:
executable_fact = stat.S_IXUSR & os.stat(fn)[stat.ST_MODE]
except OSError as e:
failed = 'Could not stat fact (%s): %s' % (fn, to_text(e))
local[fact_base] = failed
module.warn(failed)
continue
if executable_fact:
try: try:
# run it # run it
rc, out, err = module.run_command(fn) rc, out, err = module.run_command(fn)

@ -20,6 +20,13 @@
- name: unreadable - name: unreadable
mode: '0000' mode: '0000'
- name: Create dangling symlink
file:
path: "{{ fact_dir }}/dead_symlink.fact"
src: /tmp/dead_symlink
force: yes
state: link
- name: force fact gather to get ansible_local - name: force fact gather to get ansible_local
setup: setup:
fact_path: "{{ fact_dir | expanduser }}" fact_path: "{{ fact_dir | expanduser }}"
@ -43,3 +50,4 @@
- setup_result['ansible_facts']['ansible_local']['goodscript']['script_ran']|bool - setup_result['ansible_facts']['ansible_local']['goodscript']['script_ran']|bool
- setup_result['ansible_facts']['ansible_local']['basdscript'].startswith("Failure executing fact script") - setup_result['ansible_facts']['ansible_local']['basdscript'].startswith("Failure executing fact script")
- setup_result['ansible_facts']['ansible_local']['unreadable'].startswith('error loading facts') - setup_result['ansible_facts']['ansible_local']['unreadable'].startswith('error loading facts')
- setup_result['ansible_facts']['ansible_local']['dead_symlink'].startswith('Could not stat fact')

Loading…
Cancel
Save