From f0b21dcc0f771d9107ce881610e6cda839cd2cf6 Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Wed, 10 Apr 2013 18:42:54 -0400 Subject: [PATCH] allow references to names of variables in with_items without needing to surround them with Jinja2 '{{' delimeters --- examples/playbooks/upgraded_vars.yml | 19 +++++++++---- lib/ansible/runner/lookup_plugins/items.py | 15 ++++++++--- lib/ansible/runner/lookup_plugins/nested.py | 30 ++++++++++++++++----- lib/ansible/utils/__init__.py | 30 +++++---------------- lib/ansible/utils/template.py | 7 ++++- 5 files changed, 61 insertions(+), 40 deletions(-) diff --git a/examples/playbooks/upgraded_vars.yml b/examples/playbooks/upgraded_vars.yml index e5437b5225e..c5e82522b21 100644 --- a/examples/playbooks/upgraded_vars.yml +++ b/examples/playbooks/upgraded_vars.yml @@ -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' diff --git a/lib/ansible/runner/lookup_plugins/items.py b/lib/ansible/runner/lookup_plugins/items.py index 3f1b6dda54c..eaf41a67197 100644 --- a/lib/ansible/runner/lookup_plugins/items.py +++ b/lib/ansible/runner/lookup_plugins/items.py @@ -16,6 +16,7 @@ # along with Ansible. If not, see . 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) diff --git a/lib/ansible/runner/lookup_plugins/nested.py b/lib/ansible/runner/lookup_plugins/nested.py index 0fb296f7f4a..70f606adbd8 100644 --- a/lib/ansible/runner/lookup_plugins/nested.py +++ b/lib/ansible/runner/lookup_plugins/nested.py @@ -15,7 +15,9 @@ # You should have received a copy of the GNU General Public License # along with Ansible. If not, see . +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, 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) - def run(self, terms, **kwargs): - if '{' or '[' in terms: - # 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 = [] diff --git a/lib/ansible/utils/__init__.py b/lib/ansible/utils/__init__.py index dadf4d57820..1bd08495b36 100644 --- a/lib/ansible/utils/__init__.py +++ b/lib/ansible/utils/__init__.py @@ -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 - return eval(str) + try: + return eval(str) + except Exception, e: + return str + + diff --git a/lib/ansible/utils/template.py b/lib/ansible/utils/template.py index c339df8ac0d..c1ed6d0e3ff 100644 --- a/lib/ansible/utils/template.py +++ b/lib/ansible/utils/template.py @@ -435,7 +435,12 @@ def template_from_string(basedir, data, vars): raise errors.AnsibleError("recursive loop detected in template string: %s" % data) 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: