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
e756ee37
Commit
e756ee37
authored
Sep 30, 2012
by
Michael DeHaan
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1159 from dhozac/include-with_items
Allow task includes to work with with_items
parents
5aab75e7
8a8ffa23
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
42 additions
and
23 deletions
+42
-23
lib/ansible/playbook/play.py
+17
-11
lib/ansible/runner/__init__.py
+1
-1
lib/ansible/utils.py
+24
-11
No files found.
lib/ansible/playbook/play.py
View file @
e756ee37
...
...
@@ -101,23 +101,29 @@ class Play(object):
tasks
=
ds
.
get
(
keyname
,
[])
results
=
[]
for
x
in
tasks
:
task_vars
=
self
.
vars
.
copy
()
if
'include'
in
x
:
task_vars
=
self
.
vars
.
copy
()
tokens
=
shlex
.
split
(
x
[
'include'
])
for
t
in
tokens
[
1
:]:
(
k
,
v
)
=
t
.
split
(
"="
,
1
)
task_vars
[
k
]
=
utils
.
template
(
self
.
basedir
,
v
,
task_vars
)
include_file
=
utils
.
template
(
self
.
basedir
,
tokens
[
0
],
task_vars
)
data
=
utils
.
parse_yaml_from_file
(
utils
.
path_dwim
(
self
.
basedir
,
include_file
))
if
'with_items'
in
x
:
items
=
utils
.
varReplaceWithItems
(
self
.
basedir
,
x
[
'with_items'
],
task_vars
)
else
:
items
=
[
''
]
for
item
in
items
:
mv
=
task_vars
.
copy
()
mv
[
'item'
]
=
item
for
t
in
tokens
[
1
:]:
(
k
,
v
)
=
t
.
split
(
"="
,
1
)
mv
[
k
]
=
utils
.
varReplaceWithItems
(
self
.
basedir
,
v
,
mv
)
include_file
=
utils
.
template
(
self
.
basedir
,
tokens
[
0
],
mv
)
data
=
utils
.
parse_yaml_from_file
(
utils
.
path_dwim
(
self
.
basedir
,
include_file
))
for
y
in
data
:
results
.
append
(
Task
(
self
,
y
,
module_vars
=
mv
))
elif
type
(
x
)
==
dict
:
data
=
[
x
]
task_vars
=
self
.
vars
.
copy
()
results
.
append
(
Task
(
self
,
x
,
module_vars
=
task_vars
))
else
:
raise
Exception
(
"unexpected task type"
)
for
y
in
data
:
mv
=
task_vars
.
copy
()
results
.
append
(
Task
(
self
,
y
,
module_vars
=
mv
))
for
x
in
results
:
if
self
.
tags
is
not
None
:
x
.
tags
.
extend
(
self
.
tags
)
...
...
lib/ansible/runner/__init__.py
View file @
e756ee37
...
...
@@ -261,7 +261,7 @@ class Runner(object):
items
=
self
.
module_vars
.
get
(
'items'
,
[])
if
isinstance
(
items
,
basestring
)
and
items
.
startswith
(
"$"
):
items
=
utils
.
var
Lookup
(
items
,
inject
)
items
=
utils
.
var
ReplaceWithItems
(
self
.
basedir
,
items
,
inject
)
if
type
(
items
)
!=
list
:
raise
errors
.
AnsibleError
(
"with_items only takes a list:
%
s"
%
items
)
...
...
lib/ansible/utils.py
View file @
e756ee37
...
...
@@ -278,17 +278,6 @@ def _varFind(text):
path
.
append
(
text
[
part_start
[
0
]:
var_end
])
return
{
'path'
:
path
,
'start'
:
start
,
'end'
:
end
}
def
varLookup
(
varname
,
vars
):
''' helper function used by with_items '''
m
=
_varFind
(
varname
)
if
not
m
:
return
None
try
:
return
_varLookup
(
m
[
'path'
],
vars
)
except
VarNotFoundException
:
return
None
def
varReplace
(
raw
,
vars
,
depth
=
0
):
''' Perform variable replacement of $variables in string raw using vars dictionary '''
# this code originally from yum
...
...
@@ -360,6 +349,30 @@ def varReplaceFilesAndPipes(basedir, raw):
return
''
.
join
(
done
)
def
varReplaceWithItems
(
basedir
,
varname
,
vars
):
''' helper function used by with_items '''
if
isinstance
(
varname
,
basestring
):
m
=
_varFind
(
varname
)
if
not
m
:
return
varname
if
m
[
'start'
]
==
0
and
m
[
'end'
]
==
len
(
varname
):
try
:
return
varReplaceWithItems
(
basedir
,
_varLookup
(
m
[
'path'
],
vars
),
vars
)
except
VarNotFoundException
:
return
varname
else
:
return
template
(
basedir
,
varname
,
vars
)
elif
isinstance
(
varname
,
(
list
,
tuple
)):
return
[
varReplaceWithItems
(
basedir
,
v
,
vars
)
for
v
in
varname
]
elif
isinstance
(
varname
,
dict
):
d
=
{}
for
(
k
,
v
)
in
varname
.
iteritems
():
d
[
k
]
=
varReplaceWithItems
(
basedir
,
v
,
vars
)
return
d
else
:
raise
Exception
(
"invalid with_items type"
)
def
template
(
basedir
,
text
,
vars
):
''' run a text buffer through the templating engine until it no longer changes '''
...
...
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