Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow 'ignore_missing' for fileglob lookups #47344

Open
wants to merge 1 commit into
base: devel
Choose a base branch
from

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 community_review feature needs_triage new_contributor support: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 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 label Oct 27, 2018
@philfry philfry force-pushed the fileglob-ignore_missing branch from e0389d0 to 5d61144 Compare Nov 19, 2018
@ansibot ansibot added needs_revision and removed stale_ci community_review labels Nov 19, 2018
@ansibot ansibot added community_review stale_ci and removed needs_revision labels Dec 4, 2018
@philfry philfry force-pushed the fileglob-ignore_missing branch from 5d61144 to f57a138 Compare May 22, 2019
@ansibot ansibot added needs_revision and removed stale_ci community_review labels May 22, 2019
@ansibot ansibot added the stale_ci label May 30, 2019
@ansibot ansibot added community_review and removed needs_revision labels Jun 13, 2019
@ansibot ansibot added core_review support:core and removed community_review support:community labels Jun 29, 2019
@ansibot ansibot added pre_azp and removed stale_ci labels Dec 5, 2020
@philfry philfry force-pushed the fileglob-ignore_missing branch from f57a138 to 824a822 Compare Nov 4, 2021
@philfry
Copy link
Contributor Author

philfry commented Nov 4, 2021

affects_2.8, affects_2.9, affects_2.10, affects_2.11, …
I'd really like to see that merged, as the unnescessary warnings are messing up lots of playbook output.

@ansibot ansibot added core_review and removed needs_rebase needs_revision pre_azp labels Nov 4, 2021
@ansibot ansibot added the stale_ci label Nov 12, 2021
@philfry philfry force-pushed the fileglob-ignore_missing branch from 824a822 to d295406 Compare Nov 24, 2021
@philfry
Copy link
Contributor Author

philfry commented Nov 24, 2021

rebased (again)

@ansibot ansibot removed the stale_ci label Nov 24, 2021
@philfry philfry force-pushed the fileglob-ignore_missing branch from d295406 to 8280d2a Compare Dec 1, 2021
@ansibot ansibot added stale_ci and removed new_contributor labels Mar 2, 2022
@netsandbox
Copy link
Contributor

netsandbox commented Mar 8, 2022

Please see also https://groups.google.com/g/ansible-project/c/C668U45mz7U why this option is maybe needed.

Copy link
Contributor

@jborean93 jborean93 left a comment

Are you able to add an integration test for this, once in and passing I am happy to merge.

description: Flag to control whether or not no matches found return a warning
type: boolean
default: False
version_added: "2.13"
Copy link
Contributor

@jborean93 jborean93 Aug 10, 2022

Choose a reason for hiding this comment

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

Sorry for the delay but 2.14 is now the latest devel version.

@ansibot ansibot added needs_revision and removed core_review labels Aug 10, 2022
@philfry philfry force-pushed the fileglob-ignore_missing branch from 8280d2a to 37c616d Compare Aug 11, 2022
@philfry
Copy link
Contributor Author

philfry commented Aug 11, 2022

Are you able to add an integration test for this, once in and passing I am happy to merge.

I'm not sure. As far as I understood I cannot register the outputs (and thus warnings) from lookups. So something like this:

- debug: msg="hello world"
  loop: '{{q("fileglob", "/path/to/nowhere")}}'
  register: foo

or

- set_fact: bar={{q("fileglob", "/path/to/nowhere")}}'

won't result in having either foo.warnings or bar.warnings defined. Or am I mistaken?

@ansibot ansibot added core_review needs_revision and removed needs_revision stale_ci core_review labels Aug 11, 2022
@bcoca
Copy link
Member

bcoca commented Aug 11, 2022

true, register only takes in module/action output. You cannot get the warning but you can inspect the result in an assert/fail task:

- assert:
     that:
       - found|length > 0
  vars:
     found: '{{q("fileglob", "/path/to/nowhere")}}'

@philfry
Copy link
Contributor Author

philfry commented Aug 11, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects_2.8 feature needs_revision support:core
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants