mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-25 20:01:25 -07:00
Create a string parser for varReplace instead of using re
This fixes a few issues, - ${foo}${bar} would be parsed as a variable named foo}${bar, which wouldn't be easily fixed without breaking ${foo.${bar}} - allows escaping . in variable parts so e.g. ${hostvars.{test.example.com}.foo} works This is slower than using re. 3 million templating calls take about about twice as long to complete with this compared to the regexp, from ~65 seconds to ~115 seconds on my laptop.
This commit is contained in:
parent
6506e17eff
commit
9e4fac5ebd
2 changed files with 71 additions and 9 deletions
|
@ -261,6 +261,29 @@ class TestUtils(unittest.TestCase):
|
|||
res = ansible.utils.varReplace(template, vars, do_repr=True)
|
||||
assert res == 'True == 1L'
|
||||
|
||||
def test_varReplace_consecutive_vars(self):
|
||||
vars = {
|
||||
'foo': 'foo',
|
||||
'bar': 'bar',
|
||||
}
|
||||
|
||||
template = '${foo}${bar}'
|
||||
res = ansible.utils.varReplace(template, vars)
|
||||
assert res == 'foobar'
|
||||
|
||||
def test_varReplace_escape_dot(self):
|
||||
vars = {
|
||||
'hostvars': {
|
||||
'test.example.com': {
|
||||
'foo': 'bar',
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
template = '${hostvars.{test.example.com}.foo}'
|
||||
res = ansible.utils.varReplace(template, vars)
|
||||
assert res == 'bar'
|
||||
|
||||
def test_template_varReplace_iterated(self):
|
||||
template = 'hello $who'
|
||||
vars = {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue