From c67c23234a538997ffca58b1568d1f2917db2ddb Mon Sep 17 00:00:00 2001 From: Sam Doran Date: Mon, 4 Nov 2019 09:44:10 -0500 Subject: [PATCH] clean_facts - use correct variable when evaluating the string (#64284) A regexp object has no .startswith() method, which caused an unnecessary try/except block to be added to handle this. --- ...facts-use-correct-variable-for-startswith.yaml | 2 ++ lib/ansible/vars/clean.py | 15 ++++++--------- 2 files changed, 8 insertions(+), 9 deletions(-) create mode 100644 changelogs/fragments/clean_facts-use-correct-variable-for-startswith.yaml diff --git a/changelogs/fragments/clean_facts-use-correct-variable-for-startswith.yaml b/changelogs/fragments/clean_facts-use-correct-variable-for-startswith.yaml new file mode 100644 index 00000000000..ed6f39bc381 --- /dev/null +++ b/changelogs/fragments/clean_facts-use-correct-variable-for-startswith.yaml @@ -0,0 +1,2 @@ +bugfixes: + - clean_facts - use correct variable to avoid unnecessary handling of ``AttributeError`` diff --git a/lib/ansible/vars/clean.py b/lib/ansible/vars/clean.py index 06417823c25..ba494467b7c 100644 --- a/lib/ansible/vars/clean.py +++ b/lib/ansible/vars/clean.py @@ -133,15 +133,12 @@ def clean_facts(facts): # next we remove any connection plugin specific vars for conn_path in connection_loader.all(path_only=True): - try: - conn_name = os.path.splitext(os.path.basename(conn_path))[0] - re_key = re.compile('^ansible_%s_' % conn_name) - for fact_key in fact_keys: - # most lightweight VM or container tech creates devices with this pattern, this avoids filtering them out - if (re_key.match(fact_key) and not fact_key.endswith(('_bridge', '_gwbridge'))) or re_key.startswith('ansible_become_'): - remove_keys.add(fact_key) - except AttributeError: - pass + conn_name = os.path.splitext(os.path.basename(conn_path))[0] + re_key = re.compile('^ansible_%s_' % conn_name) + for fact_key in fact_keys: + # most lightweight VM or container tech creates devices with this pattern, this avoids filtering them out + if (re_key.match(fact_key) and not fact_key.endswith(('_bridge', '_gwbridge'))) or fact_key.startswith('ansible_become_'): + remove_keys.add(fact_key) # remove some KNOWN keys for hard in C.RESTRICTED_RESULT_KEYS + C.INTERNAL_RESULT_KEYS: