mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
allow references to names of variables in with_items without needing to surround them with Jinja2 '{{' delimeters
This commit is contained in:
parent
9ac25bb8f6
commit
f0b21dcc0f
5 changed files with 61 additions and 40 deletions
|
@ -7,16 +7,25 @@
|
|||
|
||||
- hosts: all
|
||||
|
||||
tasks:
|
||||
- shell: echo 'hello {{ ansible_hostname.upper() }}'
|
||||
vars:
|
||||
a_list:
|
||||
- a
|
||||
- b
|
||||
- c
|
||||
|
||||
- shell: echo 'match'
|
||||
tasks:
|
||||
- shell: echo hello {{ ansible_hostname.upper() }}
|
||||
|
||||
- shell: echo match
|
||||
when: 2 == 2
|
||||
|
||||
- shell: echo 'no match'
|
||||
- shell: echo no match
|
||||
when: 2 == 2 + 1
|
||||
|
||||
- shell: echo '{{ ansible_os_family }}'
|
||||
- shell: echo {{ ansible_os_family }}
|
||||
|
||||
- shell: echo {{ item }}
|
||||
with_items: a_list
|
||||
|
||||
- shell: echo 'RedHat'
|
||||
when: ansible_os_family == 'RedHat'
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from ansible.utils import safe_eval
|
||||
import ansible.utils.template as template
|
||||
|
||||
def flatten(terms):
|
||||
ret = []
|
||||
|
@ -28,12 +29,20 @@ def flatten(terms):
|
|||
|
||||
class LookupModule(object):
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
pass
|
||||
def __init__(self, basedir=None, **kwargs):
|
||||
self.basedir = basedir
|
||||
|
||||
def run(self, terms, **kwargs):
|
||||
def run(self, terms, inject=None, **kwargs):
|
||||
if isinstance(terms, basestring):
|
||||
# somewhat did:
|
||||
# with_items: alist
|
||||
# OR
|
||||
# with_items: {{ alist }}
|
||||
if not '{' in terms and not '[' in terms:
|
||||
terms = '{{ %s }}' % terms
|
||||
terms = template.template(self.basedir, terms, inject)
|
||||
if '{' or '[' in terms:
|
||||
# Jinja2 already evaluated a variable to a list.
|
||||
# Jinja2-ified list needs to be converted back to a real type
|
||||
# TODO: something a bit less heavy than eval
|
||||
terms = safe_eval(terms)
|
||||
|
|
|
@ -15,7 +15,9 @@
|
|||
# You should have received a copy of the GNU General Public License
|
||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import ansible.utils.template as template
|
||||
from ansible.utils import safe_eval
|
||||
import ansible.errors as errors
|
||||
|
||||
def flatten(terms):
|
||||
ret = []
|
||||
|
@ -37,16 +39,30 @@ def combine(a,b):
|
|||
|
||||
class LookupModule(object):
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
pass
|
||||
def __init__(self, basedir=None, **kwargs):
|
||||
self.basedir = basedir
|
||||
|
||||
def run(self, terms, **kwargs):
|
||||
def run(self, terms, inject=None, **kwargs):
|
||||
|
||||
# this code is common with 'items.py' consider moving to utils if we need it again
|
||||
|
||||
if isinstance(terms, basestring):
|
||||
# someone did:
|
||||
# with_items: alist
|
||||
# OR
|
||||
# with_items: {{ alist }}
|
||||
if not '{' in terms and not '[' in terms:
|
||||
terms = '{{ %s }}' % terms
|
||||
terms = template.template(self.basedir, terms, inject)
|
||||
if '{' or '[' in terms:
|
||||
# Jinja2 already evaluated a variable to a list.
|
||||
# Jinja2-ified list needs to be converted back to a real type
|
||||
# TODO: something a bit less heavy than eval
|
||||
terms = safe_eval(terms)
|
||||
|
||||
if not isinstance(terms, list):
|
||||
raise errors.AnsibleError("a list is required for with_nested")
|
||||
|
||||
my_list = terms[:]
|
||||
my_list.reverse()
|
||||
result = []
|
||||
|
|
|
@ -254,28 +254,6 @@ def parse_json(raw_data):
|
|||
return { "failed" : True, "parsed" : False, "msg" : orig_data }
|
||||
return results
|
||||
|
||||
def preprocess_yaml(data):
|
||||
|
||||
# allow the following casual user error:
|
||||
# bar: {{ baz }}
|
||||
# instead of bar: '{{ baz }}'
|
||||
# (commander-API should auto-quote these in similar ways on save/load)
|
||||
|
||||
new_lines = []
|
||||
lines = data.split("\n")
|
||||
for line in lines:
|
||||
if line.find("{{") != -1 and line.find(":") != -1:
|
||||
tokens = line.split(":",1)
|
||||
if tokens[1].strip().startswith("{{"):
|
||||
new_line = "%s: '%s'" % (tokens[0], tokens[1])
|
||||
else:
|
||||
new_line = line
|
||||
new_lines.append(new_line)
|
||||
else:
|
||||
new_lines.append(line)
|
||||
result = "\n".join(new_lines)
|
||||
return result
|
||||
|
||||
def parse_yaml(data):
|
||||
''' convert a yaml string to a data structure '''
|
||||
return yaml.safe_load(data)
|
||||
|
@ -310,7 +288,6 @@ def parse_yaml_from_file(path):
|
|||
|
||||
try:
|
||||
data = file(path).read()
|
||||
data = preprocess_yaml(data)
|
||||
return parse_yaml(data)
|
||||
except IOError:
|
||||
raise errors.AnsibleError("file not found: %s" % path)
|
||||
|
@ -724,7 +701,12 @@ def safe_eval(str):
|
|||
# do not allow imports
|
||||
if re.search(r'import \w+', str):
|
||||
return str
|
||||
try:
|
||||
return eval(str)
|
||||
except Exception, e:
|
||||
return str
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -436,6 +436,11 @@ def template_from_string(basedir, data, vars):
|
|||
else:
|
||||
return data
|
||||
|
||||
def test_foo():
|
||||
return 'test_foo!'
|
||||
|
||||
t.globals['test_foo'] = test_foo
|
||||
|
||||
res = jinja2.utils.concat(t.root_render_func(t.new_context(_jinja2_vars(basedir, vars, t.globals), shared=True)))
|
||||
return res
|
||||
except jinja2.exceptions.UndefinedError:
|
||||
|
|
Loading…
Reference in a new issue