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
53bde0bf
Commit
53bde0bf
authored
12 years ago
by
Jeroen Hoekx
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Support nested variables in varReplace.
parent
c7c38e9a
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
171 additions
and
3 deletions
+171
-3
lib/ansible/utils.py
+16
-3
test/TestUtils.py
+155
-0
No files found.
lib/ansible/utils.py
View file @
53bde0bf
...
...
@@ -198,7 +198,19 @@ def parse_json(data):
return
{
"failed"
:
True
,
"parsed"
:
False
,
"msg"
:
data
}
return
results
_KEYCRE
=
re
.
compile
(
r"\$(\w+)"
)
def
varLookup
(
name
,
vars
):
''' find the contents of a possibly complex variable in vars. '''
path
=
name
.
split
(
'.'
)
space
=
vars
for
part
in
path
:
if
part
in
space
:
space
=
space
[
part
]
else
:
return
return
space
_KEYCRE
=
re
.
compile
(
r"\$(?P<complex>\{){0,1}((?(complex)[\w\.]+|\w+))(?(complex)\})"
)
# if { -> complex if complex, allow . and need trailing }
def
varReplace
(
raw
,
vars
):
'''Perform variable replacement of $vars
...
...
@@ -220,8 +232,9 @@ def varReplace(raw, vars):
# Determine replacement value (if unknown variable then preserve
# original)
varname
=
m
.
group
(
1
)
.
lower
()
replacement
=
str
(
vars
.
get
(
varname
,
m
.
group
()))
varname
=
m
.
group
(
2
)
.
lower
()
replacement
=
str
(
varLookup
(
varname
,
vars
)
or
m
.
group
())
start
,
end
=
m
.
span
()
done
.
append
(
raw
[:
start
])
# Keep stuff leading up to token
...
...
This diff is collapsed.
Click to expand it.
test/TestUtils.py
0 → 100644
View file @
53bde0bf
import
os
import
unittest
import
ansible.utils
class
TestUtils
(
unittest
.
TestCase
):
#####################################
### varLookup function tests
def
test_varLookup_list
(
self
):
vars
=
{
'data'
:
{
'who'
:
[
'joe'
,
'jack'
,
'jeff'
]
}
}
res
=
ansible
.
utils
.
varLookup
(
'data.who'
,
vars
)
assert
sorted
(
res
)
==
sorted
(
vars
[
'data'
][
'who'
])
#####################################
### varReplace function tests
def
test_varReplace_simple
(
self
):
template
=
'hello $who'
vars
=
{
'who'
:
'world'
,
}
res
=
ansible
.
utils
.
varReplace
(
template
,
vars
)
assert
res
==
'hello world'
def
test_varReplace_multiple
(
self
):
template
=
'$what $who'
vars
=
{
'what'
:
'hello'
,
'who'
:
'world'
,
}
res
=
ansible
.
utils
.
varReplace
(
template
,
vars
)
assert
res
==
'hello world'
def
test_varReplace_middle
(
self
):
template
=
'hello $who!'
vars
=
{
'who'
:
'world'
,
}
res
=
ansible
.
utils
.
varReplace
(
template
,
vars
)
assert
res
==
'hello world!'
def
test_varReplace_alternative
(
self
):
template
=
'hello ${who}'
vars
=
{
'who'
:
'world'
,
}
res
=
ansible
.
utils
.
varReplace
(
template
,
vars
)
assert
res
==
'hello world'
def
test_varReplace_almost_alternative
(
self
):
template
=
'hello $who}'
vars
=
{
'who'
:
'world'
,
}
res
=
ansible
.
utils
.
varReplace
(
template
,
vars
)
assert
res
==
'hello world}'
def
test_varReplace_almost_alternative2
(
self
):
template
=
'hello ${who'
vars
=
{
'who'
:
'world'
,
}
res
=
ansible
.
utils
.
varReplace
(
template
,
vars
)
assert
res
==
template
def
test_varReplace_alternative_greed
(
self
):
template
=
'hello ${who} }'
vars
=
{
'who'
:
'world'
,
}
res
=
ansible
.
utils
.
varReplace
(
template
,
vars
)
assert
res
==
'hello world }'
def
test_varReplace_notcomplex
(
self
):
template
=
'hello $mydata.who'
vars
=
{
'data'
:
{
'who'
:
'world'
,
},
}
res
=
ansible
.
utils
.
varReplace
(
template
,
vars
)
print
res
assert
res
==
template
def
test_varReplace_nested
(
self
):
template
=
'hello ${data.who}'
vars
=
{
'data'
:
{
'who'
:
'world'
},
}
res
=
ansible
.
utils
.
varReplace
(
template
,
vars
)
assert
res
==
'hello world'
def
test_varReplace_nested_int
(
self
):
template
=
'$what ${data.who}'
vars
=
{
'data'
:
{
'who'
:
2
},
'what'
:
'hello'
,
}
res
=
ansible
.
utils
.
varReplace
(
template
,
vars
)
assert
res
==
'hello 2'
#####################################
### Template function tests
def
test_template_basic
(
self
):
template
=
'hello {{ who }}'
vars
=
{
'who'
:
'world'
,
}
res
=
ansible
.
utils
.
template
(
template
,
vars
,
{})
assert
res
==
'hello world'
def
test_template_whitespace
(
self
):
template
=
'hello {{ who }}
\n
'
vars
=
{
'who'
:
'world'
,
}
res
=
ansible
.
utils
.
template
(
template
,
vars
,
{})
assert
res
==
'hello world
\n
'
This diff is collapsed.
Click to expand it.
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