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
d95e8846
Commit
d95e8846
authored
Jun 13, 2012
by
Michael DeHaan
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #459 from dhozac/with_items-variable
Allow a variable in with_items
parents
509f657a
ba1e3730
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
21 additions
and
20 deletions
+21
-20
lib/ansible/playbook/play.py
+12
-17
lib/ansible/utils.py
+8
-2
test/TestUtils.py
+1
-1
No files found.
lib/ansible/playbook/play.py
View file @
d95e8846
...
...
@@ -70,36 +70,31 @@ class Play(object):
def
_load_tasks
(
self
,
ds
,
keyname
):
''' handle task and handler include statements '''
item
s
=
ds
.
get
(
keyname
,
[])
task
s
=
ds
.
get
(
keyname
,
[])
results
=
[]
for
x
in
items
:
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
]
=
v
include_file
=
tokens
[
0
]
data
=
utils
.
parse_yaml_from_file
(
utils
.
path_dwim
(
self
.
playbook
.
basedir
,
include_file
))
for
y
in
data
:
items
=
y
.
get
(
'with_items'
,
None
)
if
items
is
None
:
items
=
[
''
]
for
item
in
items
:
mv
=
self
.
vars
.
copy
()
mv
.
update
(
task_vars
)
mv
[
'item'
]
=
item
results
.
append
(
Task
(
self
,
y
,
module_vars
=
mv
))
elif
type
(
x
)
==
dict
:
items
=
x
.
get
(
'with_items'
,
None
)
data
=
[
x
]
else
:
raise
Exception
(
"unexpected task type"
)
for
y
in
data
:
items
=
y
.
get
(
'with_items'
,
None
)
if
items
is
None
:
items
=
[
''
]
elif
isinstance
(
items
,
basestring
):
items
=
utils
.
varLookup
(
items
,
task_vars
)
for
item
in
items
:
mv
=
self
.
vars
.
copy
()
mv
=
task_
vars
.
copy
()
mv
[
'item'
]
=
item
results
.
append
(
Task
(
self
,
x
,
module_vars
=
mv
))
else
:
raise
Exception
(
"unexpected task type"
)
results
.
append
(
Task
(
self
,
y
,
module_vars
=
mv
))
return
results
# *************************************************
...
...
lib/ansible/utils.py
View file @
d95e8846
...
...
@@ -201,7 +201,7 @@ def parse_json(data):
_LISTRE
=
re
.
compile
(
r"(\w+)\[(\d+)\]"
)
def
varLookup
(
name
,
vars
):
def
_
varLookup
(
name
,
vars
):
''' find the contents of a possibly complex variable in vars. '''
path
=
name
.
split
(
'.'
)
space
=
vars
...
...
@@ -223,6 +223,12 @@ def varLookup(name, vars):
_KEYCRE
=
re
.
compile
(
r"\$(?P<complex>\{){0,1}((?(complex)[\w\.\[\]]+|\w+))(?(complex)\})"
)
# if { -> complex if complex, allow . and need trailing }
def
varLookup
(
varname
,
vars
):
m
=
_KEYCRE
.
search
(
varname
)
if
not
m
:
return
None
return
_varLookup
(
m
.
group
(
2
),
vars
)
def
varReplace
(
raw
,
vars
):
'''Perform variable replacement of $vars
...
...
@@ -245,7 +251,7 @@ def varReplace(raw, vars):
# original)
varname
=
m
.
group
(
2
)
replacement
=
unicode
(
varLookup
(
varname
,
vars
)
or
m
.
group
())
replacement
=
unicode
(
_
varLookup
(
varname
,
vars
)
or
m
.
group
())
start
,
end
=
m
.
span
()
done
.
append
(
raw
[:
start
])
# Keep stuff leading up to token
...
...
test/TestUtils.py
View file @
d95e8846
...
...
@@ -17,7 +17,7 @@ class TestUtils(unittest.TestCase):
}
}
res
=
ansible
.
utils
.
varLookup
(
'data.who'
,
vars
)
res
=
ansible
.
utils
.
_
varLookup
(
'data.who'
,
vars
)
assert
sorted
(
res
)
==
sorted
(
vars
[
'data'
][
'who'
])
...
...
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