Implement default omit filter

This commit is contained in:
Victor Lin 2014-07-28 18:21:31 -07:00 committed by James Cammarata
parent b6a30a7331
commit 0b4d7f1574
3 changed files with 34 additions and 10 deletions

View file

@ -49,6 +49,7 @@ from ansible.module_common import ModuleReplacer
from ansible.module_utils.splitter import split_args from ansible.module_utils.splitter import split_args
from ansible.cache import FactCache from ansible.cache import FactCache
from ansible.utils import update_hash from ansible.utils import update_hash
from ansible.utils import OMIT_PLACE_HOLDER
module_replacer = ModuleReplacer(strip_comments=False) module_replacer = ModuleReplacer(strip_comments=False)
@ -740,14 +741,6 @@ class Runner(object):
if self.su_user_var is not None: if self.su_user_var is not None:
self.su_user = template.template(self.basedir, self.su_user_var, inject) 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 may be dynamic (but cannot contain {{ ansible_ssh_user }})
module_name = template.template(self.basedir, module_name, inject) 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): if self._early_needs_tmp_path(module_name, handler):
tmp = self._make_tmp_path(conn) 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 # render module_args and complex_args templates
try: try:
# When templating module_args, we need to be careful to ensure # When templating module_args, we need to be careful to ensure

View file

@ -15,6 +15,7 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
import os
import base64 import base64
import json import json
import os.path import os.path
@ -25,12 +26,14 @@ import glob
import re import re
import collections import collections
import operator as py_operator import operator as py_operator
import hashlib
from ansible import errors from ansible import errors
from ansible.utils import md5s from ansible.utils import md5s, OMIT_PLACE_HOLDER
from distutils.version import LooseVersion, StrictVersion from distutils.version import LooseVersion, StrictVersion
from random import SystemRandom from random import SystemRandom
from jinja2.filters import environmentfilter from jinja2.filters import environmentfilter
def to_nice_yaml(*a, **kw): def to_nice_yaml(*a, **kw):
'''Make verbose, human readable yaml''' '''Make verbose, human readable yaml'''
return yaml.safe_dump(*a, indent=4, allow_unicode=True, default_flow_style=False, **kw) 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: else:
raise errors.AnsibleFilterError('random can only be used on sequences and integers') 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): class FilterModule(object):
''' Ansible core jinja2 filters ''' ''' Ansible core jinja2 filters '''
@ -305,5 +317,6 @@ class FilterModule(object):
# random numbers # random numbers
'random': rand, 'random': rand,
}
'default_omit': default_omit,
}

View file

@ -61,6 +61,7 @@ LOOKUP_REGEX = re.compile(r'lookup\s*\(')
PRINT_CODE_REGEX = re.compile(r'(?:{[{%]|[%}]})') PRINT_CODE_REGEX = re.compile(r'(?:{[{%]|[%}]})')
CODE_REGEX = re.compile(r'(?:{%|%})') CODE_REGEX = re.compile(r'(?:{%|%})')
try: try:
import json import json
except ImportError: except ImportError:
@ -110,6 +111,11 @@ try:
except ImportError: except ImportError:
pass pass
OMIT_PLACE_HOLDER = (
'__omit_place_holder__%s' % _md5(os.urandom(64)).hexdigest()
)
############################################################### ###############################################################
# Abstractions around keyczar # Abstractions around keyczar
############################################################### ###############################################################