clean up first_found to fix a few issues:
- add a skip option so it won't raise an exception if you don't match anything - make it work as a drop-in replacement for first_available_file - document in the module comments all of the above cases
This commit is contained in:
parent
5f1e2afc34
commit
7b8cec3f59
1 changed files with 84 additions and 33 deletions
|
@ -63,9 +63,46 @@
|
||||||
# with_first_found:
|
# with_first_found:
|
||||||
# - files: generic
|
# - files: generic
|
||||||
# paths: tasks/staging tasks/production
|
# paths: tasks/staging tasks/production
|
||||||
|
|
||||||
# this will include the tasks in the file generic where it is found first (staging or production)
|
# this will include the tasks in the file generic where it is found first (staging or production)
|
||||||
|
|
||||||
|
# example simple file lists
|
||||||
|
#tasks:
|
||||||
|
#- name: first found file
|
||||||
|
# action: copy src=$item dest=/etc/file.cfg
|
||||||
|
# with_first_found:
|
||||||
|
# - files: foo.${inventory_hostname} foo
|
||||||
|
|
||||||
|
|
||||||
|
# example skipping if no matched files
|
||||||
|
# First_found also offers the ability to control whether or not failing
|
||||||
|
# to find a file returns an error or not
|
||||||
|
#
|
||||||
|
#- name: first found file - or skip
|
||||||
|
# action: copy src=$item dest=/etc/file.cfg
|
||||||
|
# with_first_found:
|
||||||
|
# - files: foo.${inventory_hostname}
|
||||||
|
# skip: true
|
||||||
|
|
||||||
|
# the above will return an empty list if the files cannot be found at all
|
||||||
|
# if skip is unspecificed or if it is set to false then it will return a list
|
||||||
|
# error which can be caught bye ignore_errors: true for that action.
|
||||||
|
|
||||||
|
# finally - if you want you can use it, in place to replace first_available_file:
|
||||||
|
# you simply cannot use the - files, path or skip options. simply replace
|
||||||
|
# first_available_file with with_first_found and leave the file listing in place
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# - name: with_first_found like first_available_file
|
||||||
|
# action: copy src=$item dest=/tmp/faftest
|
||||||
|
# with_first_found:
|
||||||
|
# - ../files/foo
|
||||||
|
# - ../files/bar
|
||||||
|
# - ../files/baz
|
||||||
|
# ignore_errors: true
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
from ansible import utils, errors
|
from ansible import utils, errors
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
@ -79,11 +116,19 @@ class LookupModule(object):
|
||||||
terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
|
terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
|
||||||
|
|
||||||
result = None
|
result = None
|
||||||
|
anydict = False
|
||||||
|
skip = False
|
||||||
|
|
||||||
|
for term in terms:
|
||||||
|
if isinstance(term, dict):
|
||||||
|
anydict = True
|
||||||
|
|
||||||
|
if anydict:
|
||||||
for term in terms:
|
for term in terms:
|
||||||
if isinstance(term, dict):
|
if isinstance(term, dict):
|
||||||
files = term.get('files', [])
|
files = term.get('files', [])
|
||||||
paths = term.get('paths', [])
|
paths = term.get('paths', [])
|
||||||
|
skip = utils.boolean(term.get('skip', False))
|
||||||
|
|
||||||
filelist = files
|
filelist = files
|
||||||
if isinstance(files, basestring):
|
if isinstance(files, basestring):
|
||||||
|
@ -111,6 +156,8 @@ class LookupModule(object):
|
||||||
total_search.append(f)
|
total_search.append(f)
|
||||||
else:
|
else:
|
||||||
total_search = [term]
|
total_search = [term]
|
||||||
|
else:
|
||||||
|
total_search = terms
|
||||||
|
|
||||||
result = None
|
result = None
|
||||||
for fn in total_search:
|
for fn in total_search:
|
||||||
|
@ -120,4 +167,8 @@ class LookupModule(object):
|
||||||
|
|
||||||
|
|
||||||
if not result:
|
if not result:
|
||||||
raise errors.AnsibleError("no match found: %s, %s" % (pathlist, filelist))
|
if skip:
|
||||||
|
return []
|
||||||
|
else:
|
||||||
|
return [None]
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue