Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
ansible
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
ansible
Commits
90a6c82d
Commit
90a6c82d
authored
Nov 25, 2014
by
Brian Coca
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #9626 from bcoca/minor_template_fixes
fixes to template function
parents
da7e75b8
db145a36
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
14 additions
and
13 deletions
+14
-13
lib/ansible/runner/__init__.py
+3
-0
lib/ansible/runner/lookup_plugins/flattened.py
+1
-1
lib/ansible/utils/__init__.py
+2
-2
lib/ansible/utils/template.py
+6
-6
test/units/TestUtils.py
+2
-4
No files found.
lib/ansible/runner/__init__.py
View file @
90a6c82d
...
@@ -723,6 +723,9 @@ class Runner(object):
...
@@ -723,6 +723,9 @@ class Runner(object):
# strip out any jinja2 template syntax within
# strip out any jinja2 template syntax within
# the data returned by the lookup plugin
# the data returned by the lookup plugin
items
=
utils
.
_clean_data_struct
(
items
,
from_remote
=
True
)
items
=
utils
.
_clean_data_struct
(
items
,
from_remote
=
True
)
if
items
is
None
:
items
=
[]
else
:
if
type
(
items
)
!=
list
:
if
type
(
items
)
!=
list
:
raise
errors
.
AnsibleError
(
"lookup plugins have to return a list:
%
r"
%
items
)
raise
errors
.
AnsibleError
(
"lookup plugins have to return a list:
%
r"
%
items
)
...
...
lib/ansible/runner/lookup_plugins/flattened.py
View file @
90a6c82d
...
@@ -50,7 +50,7 @@ class LookupModule(object):
...
@@ -50,7 +50,7 @@ class LookupModule(object):
if
isinstance
(
term
,
basestring
):
if
isinstance
(
term
,
basestring
):
# convert a variable to a list
# convert a variable to a list
term2
=
utils
.
listify_lookup_plugin_terms
(
term
,
self
.
basedir
,
inject
)
term2
=
utils
.
listify_lookup_plugin_terms
(
term
,
self
.
basedir
,
inject
,
fail_on_undefined
=
False
)
# but avoid converting a plain string to a list of one string
# but avoid converting a plain string to a list of one string
if
term2
!=
[
term
]:
if
term2
!=
[
term
]:
term
=
term2
term
=
term2
...
...
lib/ansible/utils/__init__.py
View file @
90a6c82d
...
@@ -1451,7 +1451,7 @@ def safe_eval(expr, locals={}, include_exceptions=False):
...
@@ -1451,7 +1451,7 @@ def safe_eval(expr, locals={}, include_exceptions=False):
return
expr
return
expr
def
listify_lookup_plugin_terms
(
terms
,
basedir
,
inject
):
def
listify_lookup_plugin_terms
(
terms
,
basedir
,
inject
,
fail_on_undefined
=
C
.
DEFAULT_UNDEFINED_VAR_BEHAVIOR
):
from
ansible.utils
import
template
from
ansible.utils
import
template
...
@@ -1469,7 +1469,7 @@ def listify_lookup_plugin_terms(terms, basedir, inject):
...
@@ -1469,7 +1469,7 @@ def listify_lookup_plugin_terms(terms, basedir, inject):
# if not already a list, get ready to evaluate with Jinja2
# if not already a list, get ready to evaluate with Jinja2
# not sure why the "/" is in above code :)
# not sure why the "/" is in above code :)
try
:
try
:
new_terms
=
template
.
template
(
basedir
,
terms
,
inject
,
convert_bare
=
True
,
fail_on_undefined
=
C
.
DEFAULT_UNDEFINED_VAR_BEHAVIOR
)
new_terms
=
template
.
template
(
basedir
,
"{{
%
s}}"
%
terms
,
inject
,
convert_bare
=
True
,
fail_on_undefined
=
fail_on_undefined
)
if
isinstance
(
new_terms
,
basestring
)
and
"{{"
in
new_terms
:
if
isinstance
(
new_terms
,
basestring
)
and
"{{"
in
new_terms
:
pass
pass
else
:
else
:
...
...
lib/ansible/utils/template.py
View file @
90a6c82d
...
@@ -100,33 +100,33 @@ def lookup(name, *args, **kwargs):
...
@@ -100,33 +100,33 @@ def lookup(name, *args, **kwargs):
else
:
else
:
raise
errors
.
AnsibleError
(
"lookup plugin (
%
s) not found"
%
name
)
raise
errors
.
AnsibleError
(
"lookup plugin (
%
s) not found"
%
name
)
def
template
(
basedir
,
varname
,
vars
,
lookup_fatal
=
True
,
depth
=
0
,
expand_lists
=
True
,
convert_bare
=
False
,
fail_on_undefined
=
False
,
filter_fatal
=
True
):
def
template
(
basedir
,
varname
,
template
vars
,
lookup_fatal
=
True
,
depth
=
0
,
expand_lists
=
True
,
convert_bare
=
False
,
fail_on_undefined
=
False
,
filter_fatal
=
True
):
''' templates a data structure by traversing it and substituting for other data structures '''
''' templates a data structure by traversing it and substituting for other data structures '''
from
ansible
import
utils
from
ansible
import
utils
try
:
try
:
if
convert_bare
and
isinstance
(
varname
,
basestring
):
if
convert_bare
and
isinstance
(
varname
,
basestring
):
first_part
=
varname
.
split
(
"."
)[
0
]
.
split
(
"["
)[
0
]
first_part
=
varname
.
split
(
"."
)[
0
]
.
split
(
"["
)[
0
]
if
first_part
in
vars
and
'{{'
not
in
varname
and
'$'
not
in
varname
:
if
first_part
in
template
vars
and
'{{'
not
in
varname
and
'$'
not
in
varname
:
varname
=
"{{
%
s}}"
%
varname
varname
=
"{{
%
s}}"
%
varname
if
isinstance
(
varname
,
basestring
):
if
isinstance
(
varname
,
basestring
):
if
'{{'
in
varname
or
'{
%
'
in
varname
:
if
'{{'
in
varname
or
'{
%
'
in
varname
:
varname
=
template_from_string
(
basedir
,
varname
,
vars
,
fail_on_undefined
)
varname
=
template_from_string
(
basedir
,
varname
,
template
vars
,
fail_on_undefined
)
if
(
varname
.
startswith
(
"{"
)
and
not
varname
.
startswith
(
"{{"
))
or
varname
.
startswith
(
"["
):
if
(
varname
.
startswith
(
"{"
)
and
not
varname
.
startswith
(
"{{"
))
or
varname
.
startswith
(
"["
):
eval_results
=
utils
.
safe_eval
(
varname
,
locals
=
vars
,
include_exceptions
=
True
)
eval_results
=
utils
.
safe_eval
(
varname
,
locals
=
template
vars
,
include_exceptions
=
True
)
if
eval_results
[
1
]
is
None
:
if
eval_results
[
1
]
is
None
:
varname
=
eval_results
[
0
]
varname
=
eval_results
[
0
]
return
varname
return
varname
elif
isinstance
(
varname
,
(
list
,
tuple
)):
elif
isinstance
(
varname
,
(
list
,
tuple
)):
return
[
template
(
basedir
,
v
,
vars
,
lookup_fatal
,
depth
,
expand_lists
,
fail_on_undefined
=
fail_on_undefined
)
for
v
in
varname
]
return
[
template
(
basedir
,
v
,
templatevars
,
lookup_fatal
,
depth
,
expand_lists
,
convert_bare
,
fail_on_undefined
,
filter_fatal
)
for
v
in
varname
]
elif
isinstance
(
varname
,
dict
):
elif
isinstance
(
varname
,
dict
):
d
=
{}
d
=
{}
for
(
k
,
v
)
in
varname
.
iteritems
():
for
(
k
,
v
)
in
varname
.
iteritems
():
d
[
k
]
=
template
(
basedir
,
v
,
vars
,
lookup_fatal
,
depth
,
expand_lists
,
fail_on_undefined
=
fail_on_undefined
)
d
[
k
]
=
template
(
basedir
,
v
,
templatevars
,
lookup_fatal
,
depth
,
expand_lists
,
convert_bare
,
fail_on_undefined
,
filter_fatal
)
return
d
return
d
else
:
else
:
return
varname
return
varname
...
...
test/units/TestUtils.py
View file @
90a6c82d
...
@@ -568,10 +568,8 @@ class TestUtils(unittest.TestCase):
...
@@ -568,10 +568,8 @@ class TestUtils(unittest.TestCase):
basedir
=
os
.
path
.
dirname
(
__file__
)
basedir
=
os
.
path
.
dirname
(
__file__
)
# Straight lookups
# Straight lookups
self
.
assertEqual
(
ansible
.
utils
.
listify_lookup_plugin_terms
(
'things'
,
basedir
,
dict
()),
self
.
assertEqual
(
ansible
.
utils
.
listify_lookup_plugin_terms
(
'things'
,
basedir
,
dict
(
things
=
[])),
[])
[
'things'
])
self
.
assertEqual
(
ansible
.
utils
.
listify_lookup_plugin_terms
(
'things'
,
basedir
,
dict
(
things
=
[
'one'
,
'two'
])),
[
'one'
,
'two'
])
self
.
assertEqual
(
ansible
.
utils
.
listify_lookup_plugin_terms
(
'things'
,
basedir
,
dict
(
things
=
[
'one'
,
'two'
])),
[
'one'
,
'two'
])
# Variable interpolation
# Variable interpolation
self
.
assertEqual
(
ansible
.
utils
.
listify_lookup_plugin_terms
(
'things'
,
basedir
,
dict
(
things
=
[
'{{ foo }}'
,
'{{ bar }}'
],
foo
=
"hello"
,
bar
=
"world"
)),
self
.
assertEqual
(
ansible
.
utils
.
listify_lookup_plugin_terms
(
'things'
,
basedir
,
dict
(
things
=
[
'{{ foo }}'
,
'{{ bar }}'
],
foo
=
"hello"
,
bar
=
"world"
)),
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment