Skip to content

Conversation

@philfry
Copy link
Contributor

@philfry philfry commented Oct 19, 2018

SUMMARY

There are situations where it's useful to hide the Unable to find ... in expected paths (use -vvvvv to see paths) message. This PR adds the ability to pass ignore_missing to the fileglob plugin. See below for example.

ISSUE TYPE
  • Feature Pull Request
COMPONENT NAME

fileglob

ANSIBLE VERSION
ansible 2.8.0.dev0 (fileglob-ignore_missing e0389d0aab) last updated 2018/10/19 10:44:40 (GMT +200)
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/phil/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /home/phil/projects/3rdparty/ansible/lib/ansible
  executable location = /home/phil/projects/3rdparty/ansible/bin/ansible
  python version = 2.7.15 (default, Sep 21 2018, 23:26:48) [GCC 8.1.1 20180712 (Red Hat 8.1.1-5)]
ADDITIONAL INFORMATION
echo -e "foo\nbar\nbaz" > inventory
for f in foo baz; do install -D /dev/null files/${f}/iptables; done
cat > test.yml <<EOF
---
- hosts: all
  gather_facts: no
  tasks:
    - name: "without ignore_missing"
      debug: var=item
      loop: '{{lookup("fileglob",
        "files/"+inventory_hostname+"/iptables",
        wantlist=true)}}'
    - name: "with ignore_missing"
      debug: var=item
      loop: '{{lookup("fileglob",
        "files/"+inventory_hostname+"/iptables",
        wantlist=true, ignore_missing=true)}}'
...
EOF
ansible-playbook -i inventory test.yml 

output:

PLAY [all] ********************************************************************************

TASK [without ignore_missing] *************************************************************
 [WARNING]: Unable to find 'files/bar' in expected paths (use -vvvvv to see paths)

ok: [foo] => (item=/home/phil/projects/3rdparty/ansible/tmp/files/foo/iptables) => {
    "item": "/home/phil/projects/3rdparty/ansible/tmp/files/foo/iptables"
}
ok: [baz] => (item=/home/phil/projects/3rdparty/ansible/tmp/files/baz/iptables) => {
    "item": "/home/phil/projects/3rdparty/ansible/tmp/files/baz/iptables"
}

TASK [with ignore_missing] ****************************************************************
ok: [foo] => (item=/home/phil/projects/3rdparty/ansible/tmp/files/foo/iptables) => {
    "item": "/home/phil/projects/3rdparty/ansible/tmp/files/foo/iptables"
}
ok: [baz] => (item=/home/phil/projects/3rdparty/ansible/tmp/files/baz/iptables) => {
    "item": "/home/phil/projects/3rdparty/ansible/tmp/files/baz/iptables"
}

PLAY RECAP ********************************************************************************
bar                        : ok=0    changed=0    unreachable=0    failed=0    skipped=2   
baz                        : ok=2    changed=0    unreachable=0    failed=0    skipped=0
foo                        : ok=2    changed=0    unreachable=0    failed=0    skipped=0

@ansibot
Copy link
Contributor

ansibot commented Oct 19, 2018

Hi @philfry, thank you for submitting this pull-request!

click here for bot help

@ansibot ansibot added affects_2.8 This issue/PR affects Ansible v2.8 community_review In order to be merged, this PR must follow the community review workflow. feature This issue/PR relates to a feature request. needs_triage Needs a first human triage before being processed. new_contributor This PR is the first contribution by a new community member. support:community This issue/PR relates to code supported by the Ansible community. labels Oct 19, 2018
@bcoca
Copy link
Member

bcoca commented Oct 19, 2018

this is redundant with the 'errors' option available to all lookups. https://docs.ansible.com/ansible/latest/plugins/lookup.html#using-lookup-plugins

@bcoca bcoca removed the needs_triage Needs a first human triage before being processed. label Oct 19, 2018
@philfry
Copy link
Contributor Author

philfry commented Oct 19, 2018

this is redundant with the 'errors' option available to all lookups.

The errors option does not eleminate the warnings for me. Am I missing something?

---
- hosts: all
  gather_facts: no
  tasks:
    - name: "with errors=strict"
      debug: var=item
      loop: '{{lookup("fileglob",
        "files/"+inventory_hostname+"/iptables",
        wantlist=true, errors="strict")}}'
    - name: "with errors=ignore"
      debug: var=item
      loop: '{{lookup("fileglob",
        "files/"+inventory_hostname+"/iptables",
        wantlist=true, errors="ignore")}}'
    - name: "with errors=warn"
      debug: var=item
      loop: '{{lookup("fileglob",
        "files/"+inventory_hostname+"/iptables",
        wantlist=true, errors="warn")}}'
...
ansible-playbook -i inventory test2.yml 

PLAY [all] ***************************************************************************************************************************************************************************

TASK [with errors=strict] ************************************************************************************************************************************************************
 [WARNING]: Unable to find 'files/bar' in expected paths (use -vvvvv to see paths)

ok: [foo] => (item=/home/phil/projects/3rdparty/ansible/tmp/files/foo/iptables) => {
    "item": "/home/phil/projects/3rdparty/ansible/tmp/files/foo/iptables"
}
ok: [baz] => (item=/home/phil/projects/3rdparty/ansible/tmp/files/baz/iptables) => {
    "item": "/home/phil/projects/3rdparty/ansible/tmp/files/baz/iptables"
}

TASK [with errors=ignore] ************************************************************************************************************************************************************
 [WARNING]: Unable to find 'files/bar' in expected paths (use -vvvvv to see paths)

ok: [foo] => (item=/home/phil/projects/3rdparty/ansible/tmp/files/foo/iptables) => {
    "item": "/home/phil/projects/3rdparty/ansible/tmp/files/foo/iptables"
}
ok: [baz] => (item=/home/phil/projects/3rdparty/ansible/tmp/files/baz/iptables) => {
    "item": "/home/phil/projects/3rdparty/ansible/tmp/files/baz/iptables"
}

TASK [with errors=warn] **************************************************************************************************************************************************************
 [WARNING]: Unable to find 'files/bar' in expected paths (use -vvvvv to see paths)

ok: [foo] => (item=/home/phil/projects/3rdparty/ansible/tmp/files/foo/iptables) => {
    "item": "/home/phil/projects/3rdparty/ansible/tmp/files/foo/iptables"
}
ok: [baz] => (item=/home/phil/projects/3rdparty/ansible/tmp/files/baz/iptables) => {
    "item": "/home/phil/projects/3rdparty/ansible/tmp/files/baz/iptables"
}

PLAY RECAP ***************************************************************************************************************************************************************************
bar                        : ok=0    changed=0    unreachable=0    failed=0   
baz                        : ok=3    changed=0    unreachable=0    failed=0   
foo                        : ok=3    changed=0    unreachable=0    failed=0   

@bcoca
Copy link
Member

bcoca commented Oct 19, 2018

ah, nvmd thought you wanted to handle an error, not a warning

@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Oct 27, 2018
@philfry philfry force-pushed the fileglob-ignore_missing branch from e0389d0 to 5d61144 Compare November 19, 2018 11:19
@ansibot ansibot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. community_review In order to be merged, this PR must follow the community review workflow. labels Nov 19, 2018
@ansibot ansibot added community_review In order to be merged, this PR must follow the community review workflow. stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. and removed needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Dec 4, 2018
@philfry philfry force-pushed the fileglob-ignore_missing branch from 5d61144 to f57a138 Compare May 22, 2019 08:40
@ansibot ansibot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. community_review In order to be merged, this PR must follow the community review workflow. labels May 22, 2019
@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label May 30, 2019
@ansibot ansibot added community_review In order to be merged, this PR must follow the community review workflow. and removed needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Jun 13, 2019
@ansibot ansibot added core_review In order to be merged, this PR must follow the core review workflow. support:core This issue/PR relates to code supported by the Ansible Engineering Team. and removed community_review In order to be merged, this PR must follow the community review workflow. support:community This issue/PR relates to code supported by the Ansible community. labels Jun 29, 2019
Copy link
Contributor

@s-hertel s-hertel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@philfry You can test this with a runme.sh style test or by running ansible-playbook from within the play (you'd just need to escape variables so they're templated at the right time). I put together a test for you:

diff --git a/test/integration/targets/lookup_fileglob/non_existent/play.yml b/test/integration/targets/lookup_fileglob/non_existent/play.yml
index e92dff5acc..6733952991 100644
--- a/test/integration/targets/lookup_fileglob/non_existent/play.yml
+++ b/test/integration/targets/lookup_fileglob/non_existent/play.yml
@@ -4,3 +4,22 @@
     - name: fileglob should be empty
       assert:
         that: q("fileglob", seed) | length == 0
+
+    - name: Test ignore_missing
+      vars:
+        query_with_warnings: "query('fileglob', 'missing/file')"
+        query_without_warnings: "query('fileglob', 'missing/file', ignore_missing=true)"
+        warning: "Unable to find 'missing' in expected paths"
+      block:
+        - name: Test a warning is given for a missing path
+          command: ansible localhost -m debug -a msg="{{ '{{' + query_with_warnings + '}}' }}"
+          register: warn_for_missing
+
+        - name: Test ignoring the warning
+          command: ansible localhost -m debug -a msg="{{ '{{' + query_without_warnings + '}}' }}"
+          register: ignore_missing
+
+        - assert:
+            that:
+              - warning in warn_for_missing.stderr
+              - warning not in ignore_missing.stderr

But while I was looking into this, I feel like a new option is redundant with the general lookup option errors which is handled in the core engine. @bcoca what do you think about adding the errors option instead, defaulting to warn for backwards compatibilty? Since fileglob is handling missing files itself and always warns, this effectively means support is missing for both errors=strict and errors=ignore and only behaves like errors=warn.

I also noticed the first_found plugin has done basically the same thing - in addition to skipping a missing file with errors=warn|ignore, it has it's own skip option, which also seems redundant to me - but adding an option with either of the existing names might be nice for consistency.

@bcoca
Copy link
Member

bcoca commented Mar 6, 2023

first_found had preexisting options, we should really pass in the errors to the file finding function and suppress warnings when apropos.

@ansibot ansibot removed needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. stale_review Updates were made after the last review and the last review is more than 7 days old. labels Jun 18, 2023
@ansibot ansibot added the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. label Jul 12, 2023
@ansibot ansibot added the needs_ci This PR requires CI testing to be performed. Please close and re-open this PR to trigger CI. label Oct 26, 2023
@webknjaz

This comment was marked as resolved.

@azure-pipelines

This comment was marked as resolved.

@ansibot ansibot removed needs_ci This PR requires CI testing to be performed. Please close and re-open this PR to trigger CI. stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. labels Oct 30, 2023
@webknjaz
Copy link
Member

The branch needs to be rebased for the CI to pick it up.

@webknjaz webknjaz added the ci_verified Changes made in this PR are causing tests to fail. label Oct 30, 2023
@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Nov 13, 2023
@philfry philfry force-pushed the fileglob-ignore_missing branch from 37c616d to 88f5af8 Compare September 4, 2024 10:11
@ansibot ansibot added the stale_review Updates were made after the last review and the last review is more than 7 days old. label Sep 4, 2024
@bcoca bcoca requested review from jborean93 and s-hertel September 4, 2024 15:21
@bcoca bcoca mentioned this pull request Sep 5, 2024
@ansibot ansibot added the needs_ci This PR requires CI testing to be performed. Please close and re-open this PR to trigger CI. label Sep 5, 2024
@webknjaz
Copy link
Member

webknjaz commented Sep 5, 2024

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@ansibot ansibot removed needs_ci This PR requires CI testing to be performed. Please close and re-open this PR to trigger CI. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. ci_verified Changes made in this PR are causing tests to fail. stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. labels Sep 5, 2024
@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Sep 19, 2024
@ansibot ansibot added the stale_pr This PR has not been pushed to for more than one year. label Sep 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

affects_2.8 This issue/PR affects Ansible v2.8 feature This issue/PR relates to a feature request. stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. stale_pr This PR has not been pushed to for more than one year. stale_review Updates were made after the last review and the last review is more than 7 days old. support:core This issue/PR relates to code supported by the Ansible Engineering Team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants