From 0b4d7f1574736ebe0e936387186093c52d6d3423 Mon Sep 17 00:00:00 2001 From: Victor Lin Date: Mon, 28 Jul 2014 18:21:31 -0700 Subject: [PATCH] Implement default omit filter --- lib/ansible/runner/__init__.py | 21 +++++++++++++-------- lib/ansible/runner/filter_plugins/core.py | 17 +++++++++++++++-- lib/ansible/utils/__init__.py | 6 ++++++ 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/lib/ansible/runner/__init__.py b/lib/ansible/runner/__init__.py index a63bf426533..ea8cbc37a48 100644 --- a/lib/ansible/runner/__init__.py +++ b/lib/ansible/runner/__init__.py @@ -49,6 +49,7 @@ from ansible.module_common import ModuleReplacer from ansible.module_utils.splitter import split_args from ansible.cache import FactCache from ansible.utils import update_hash +from ansible.utils import OMIT_PLACE_HOLDER module_replacer = ModuleReplacer(strip_comments=False) @@ -740,14 +741,6 @@ class Runner(object): if self.su_user_var is not None: self.su_user = template.template(self.basedir, self.su_user_var, inject) - # allow module args to work as a dictionary - # though it is usually a string - new_args = "" - if type(module_args) == dict: - for (k,v) in module_args.iteritems(): - new_args = new_args + "%s='%s' " % (k,v) - module_args = new_args - # module_name may be dynamic (but cannot contain {{ ansible_ssh_user }}) module_name = template.template(self.basedir, module_name, inject) @@ -872,6 +865,18 @@ class Runner(object): if self._early_needs_tmp_path(module_name, handler): tmp = self._make_tmp_path(conn) + # allow module args to work as a dictionary + # though it is usually a string + if type(module_args) == dict: + new_args = [] + for (k, v) in module_args.iteritems(): + # see if the value is OMIT_PLACE_HOLDER, if it is, skip it + arg_value = template.template(self.basedir, v, inject, fail_on_undefined=self.error_on_undefined_vars) + if arg_value.strip() == OMIT_PLACE_HOLDER: + continue + new_args.append("%s='%s'" % (k, v)) + module_args = ' '.join(new_args) + # render module_args and complex_args templates try: # When templating module_args, we need to be careful to ensure diff --git a/lib/ansible/runner/filter_plugins/core.py b/lib/ansible/runner/filter_plugins/core.py index 7ca0f937420..0b08582fb23 100644 --- a/lib/ansible/runner/filter_plugins/core.py +++ b/lib/ansible/runner/filter_plugins/core.py @@ -15,6 +15,7 @@ # You should have received a copy of the GNU General Public License # along with Ansible. If not, see . +import os import base64 import json import os.path @@ -25,12 +26,14 @@ import glob import re import collections import operator as py_operator +import hashlib from ansible import errors -from ansible.utils import md5s +from ansible.utils import md5s, OMIT_PLACE_HOLDER from distutils.version import LooseVersion, StrictVersion from random import SystemRandom from jinja2.filters import environmentfilter + def to_nice_yaml(*a, **kw): '''Make verbose, human readable yaml''' return yaml.safe_dump(*a, indent=4, allow_unicode=True, default_flow_style=False, **kw) @@ -234,6 +237,15 @@ def rand(environment, end, start=None, step=None): else: raise errors.AnsibleFilterError('random can only be used on sequences and integers') +def default_omit(a): + try: + a + except NameError: + return OMIT_PLACE_HOLDER + else: + return a + + class FilterModule(object): ''' Ansible core jinja2 filters ''' @@ -305,5 +317,6 @@ class FilterModule(object): # random numbers 'random': rand, - } + 'default_omit': default_omit, + } diff --git a/lib/ansible/utils/__init__.py b/lib/ansible/utils/__init__.py index 131a3e315df..8030fa6318f 100644 --- a/lib/ansible/utils/__init__.py +++ b/lib/ansible/utils/__init__.py @@ -61,6 +61,7 @@ LOOKUP_REGEX = re.compile(r'lookup\s*\(') PRINT_CODE_REGEX = re.compile(r'(?:{[{%]|[%}]})') CODE_REGEX = re.compile(r'(?:{%|%})') + try: import json except ImportError: @@ -110,6 +111,11 @@ try: except ImportError: pass + +OMIT_PLACE_HOLDER = ( + '__omit_place_holder__%s' % _md5(os.urandom(64)).hexdigest() +) + ############################################################### # Abstractions around keyczar ###############################################################