Nonfatal facts (#73804)
continue with local facts vs at script error actually capture execution errors better error messages in general add more local facts tests fixes #52427
This commit is contained in:
parent
212837defc
commit
9db557e431
7 changed files with 68 additions and 41 deletions
2
changelogs/fragments/local_facts_continue.yml
Normal file
2
changelogs/fragments/local_facts_continue.yml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
bugfixes:
|
||||||
|
- setup, don't give up on all local facts gathering if one script file fails.
|
|
@ -21,12 +21,10 @@ import json
|
||||||
import os
|
import os
|
||||||
import stat
|
import stat
|
||||||
|
|
||||||
from ansible.module_utils.six.moves import configparser
|
from ansible.module_utils._text import to_text
|
||||||
from ansible.module_utils.six.moves import StringIO
|
|
||||||
|
|
||||||
from ansible.module_utils.facts.utils import get_file_content
|
from ansible.module_utils.facts.utils import get_file_content
|
||||||
|
|
||||||
from ansible.module_utils.facts.collector import BaseFactCollector
|
from ansible.module_utils.facts.collector import BaseFactCollector
|
||||||
|
from ansible.module_utils.six.moves import configparser, StringIO
|
||||||
|
|
||||||
|
|
||||||
class LocalFactCollector(BaseFactCollector):
|
class LocalFactCollector(BaseFactCollector):
|
||||||
|
@ -46,36 +44,47 @@ class LocalFactCollector(BaseFactCollector):
|
||||||
return local_facts
|
return local_facts
|
||||||
|
|
||||||
local = {}
|
local = {}
|
||||||
|
# go over .fact files, run executables, read rest, skip bad with warning and note
|
||||||
for fn in sorted(glob.glob(fact_path + '/*.fact')):
|
for fn in sorted(glob.glob(fact_path + '/*.fact')):
|
||||||
# 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]:
|
if stat.S_IXUSR & os.stat(fn)[stat.ST_MODE]:
|
||||||
# run it
|
failed = None
|
||||||
# try to read it as json first
|
|
||||||
# if that fails read it with ConfigParser
|
|
||||||
# if that fails, skip it
|
|
||||||
try:
|
try:
|
||||||
|
# run it
|
||||||
rc, out, err = module.run_command(fn)
|
rc, out, err = module.run_command(fn)
|
||||||
except UnicodeError:
|
if rc != 0:
|
||||||
fact = 'error loading fact - output of running %s was not utf-8' % fn
|
failed = 'Failure executing fact script (%s), rc: %s, err: %s' % (fn, rc, err)
|
||||||
local[fact_base] = fact
|
except (IOError, OSError) as e:
|
||||||
local_facts['local'] = local
|
failed = 'Could not execute fact script (%s): %s' % (fn, to_text(e))
|
||||||
module.warn(fact)
|
|
||||||
return local_facts
|
if failed is not None:
|
||||||
|
local[fact_base] = failed
|
||||||
|
module.warn(failed)
|
||||||
|
continue
|
||||||
else:
|
else:
|
||||||
|
# ignores exceptions and returns empty
|
||||||
out = get_file_content(fn, default='')
|
out = get_file_content(fn, default='')
|
||||||
|
|
||||||
# load raw json
|
try:
|
||||||
fact = 'loading %s' % fact_base
|
# ensure we have unicode
|
||||||
|
out = to_text(out, errors='surrogate_or_strict')
|
||||||
|
except UnicodeError:
|
||||||
|
fact = 'error loading fact - output of running "%s" was not utf-8' % fn
|
||||||
|
local[fact_base] = fact
|
||||||
|
module.warn(fact)
|
||||||
|
continue
|
||||||
|
|
||||||
|
# try to read it as json first
|
||||||
try:
|
try:
|
||||||
fact = json.loads(out)
|
fact = json.loads(out)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
# load raw ini
|
# if that fails read it with ConfigParser
|
||||||
cp = configparser.ConfigParser()
|
cp = configparser.ConfigParser()
|
||||||
try:
|
try:
|
||||||
cp.readfp(StringIO(out))
|
cp.readfp(StringIO(out))
|
||||||
except configparser.Error:
|
except configparser.Error:
|
||||||
fact = "error loading fact - please check content"
|
fact = "error loading facts as JSON or ini - please check content: %s" % fn
|
||||||
module.warn(fact)
|
module.warn(fact)
|
||||||
else:
|
else:
|
||||||
fact = {}
|
fact = {}
|
||||||
|
@ -85,6 +94,9 @@ class LocalFactCollector(BaseFactCollector):
|
||||||
for opt in cp.options(sect):
|
for opt in cp.options(sect):
|
||||||
val = cp.get(sect, opt)
|
val = cp.get(sect, opt)
|
||||||
fact[sect][opt] = val
|
fact[sect][opt] = val
|
||||||
|
except Exception as e:
|
||||||
|
fact = "Failed to convert (%s) to JSON: %s" % (fn, to_text(e))
|
||||||
|
module.warn(fact)
|
||||||
|
|
||||||
local[fact_base] = fact
|
local[fact_base] = fact
|
||||||
|
|
||||||
|
|
3
test/integration/targets/facts_d/files/basdscript.fact
Normal file
3
test/integration/targets/facts_d/files/basdscript.fact
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
exit 1
|
3
test/integration/targets/facts_d/files/goodscript.fact
Normal file
3
test/integration/targets/facts_d/files/goodscript.fact
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
echo '{"script_ran": true}'
|
2
test/integration/targets/facts_d/files/preferences.fact
Normal file
2
test/integration/targets/facts_d/files/preferences.fact
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
[general]
|
||||||
|
bar=loaded
|
1
test/integration/targets/facts_d/files/unreadable.fact
Normal file
1
test/integration/targets/facts_d/files/unreadable.fact
Normal file
|
@ -0,0 +1 @@
|
||||||
|
wontbeseen=ever
|
|
@ -1,35 +1,36 @@
|
||||||
# Test code for facts.d and setup filters
|
|
||||||
# (c) 2014, James Tanner <tanner.jc@gmail.com>
|
# (c) 2014, James Tanner <tanner.jc@gmail.com>
|
||||||
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||||
|
|
||||||
# This file is part of Ansible
|
- name: prep for local facts tests
|
||||||
#
|
block:
|
||||||
# Ansible is free software: you can redistribute it and/or modify
|
- name: set factdir var
|
||||||
# it under the terms of the GNU General Public License as published by
|
set_fact: fact_dir={{output_dir}}/facts.d
|
||||||
# the Free Software Foundation, either version 3 of the License, or
|
|
||||||
# (at your option) any later version.
|
|
||||||
#
|
|
||||||
# Ansible is distributed in the hope that it will be useful,
|
|
||||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
# GNU General Public License for more details.
|
|
||||||
#
|
|
||||||
# You should have received a copy of the GNU General Public License
|
|
||||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
- set_fact: fact_dir={{output_dir}}/facts.d
|
- name: create fact dir
|
||||||
|
file: path={{ fact_dir }} state=directory
|
||||||
|
|
||||||
- file: path={{ fact_dir }} state=directory
|
- name: copy local facts test files
|
||||||
- shell: echo "[general]" > {{ fact_dir }}/preferences.fact
|
copy: src={{ item['name'] }}.fact dest={{ fact_dir }}/ mode={{ item['mode']|default(omit) }}
|
||||||
- shell: echo "bar=loaded" >> {{ fact_dir }}/preferences.fact
|
loop:
|
||||||
|
- name: preferences
|
||||||
|
- name: basdscript
|
||||||
|
mode: '0775'
|
||||||
|
- name: goodscript
|
||||||
|
mode: '0775'
|
||||||
|
- name: unreadable
|
||||||
|
mode: '0000'
|
||||||
|
|
||||||
- setup:
|
- name: force fact gather to get ansible_local
|
||||||
|
setup:
|
||||||
fact_path: "{{ fact_dir | expanduser }}"
|
fact_path: "{{ fact_dir | expanduser }}"
|
||||||
filter: "*local*"
|
filter: "*local*"
|
||||||
register: setup_result
|
register: setup_result
|
||||||
|
|
||||||
- debug: var=setup_result
|
- name: show gathering results if rerun with -vvv
|
||||||
|
debug: var=setup_result verbosity=3
|
||||||
|
|
||||||
- assert:
|
- name: check for expected results from local facts
|
||||||
|
assert:
|
||||||
that:
|
that:
|
||||||
- "'ansible_facts' in setup_result"
|
- "'ansible_facts' in setup_result"
|
||||||
- "'ansible_local' in setup_result.ansible_facts"
|
- "'ansible_local' in setup_result.ansible_facts"
|
||||||
|
@ -39,3 +40,6 @@
|
||||||
- "'general' in setup_result.ansible_facts['ansible_local']['preferences']"
|
- "'general' in setup_result.ansible_facts['ansible_local']['preferences']"
|
||||||
- "'bar' in setup_result.ansible_facts['ansible_local']['preferences']['general']"
|
- "'bar' in setup_result.ansible_facts['ansible_local']['preferences']['general']"
|
||||||
- "setup_result.ansible_facts['ansible_local']['preferences']['general']['bar'] == 'loaded'"
|
- "setup_result.ansible_facts['ansible_local']['preferences']['general']['bar'] == 'loaded'"
|
||||||
|
- 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']['unreadable'].startswith('error loading facts')
|
||||||
|
|
Loading…
Reference in a new issue