1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Implement omit for module args, also add tests for it

This commit is contained in:
Victor Lin 2014-08-01 22:11:42 -07:00 committed by James Cammarata
parent bce6642ab6
commit 559c04f324
4 changed files with 46 additions and 21 deletions

View file

@ -624,6 +624,7 @@ class Runner(object):
inject['defaults'] = self.default_vars inject['defaults'] = self.default_vars
inject['environment'] = self.environment inject['environment'] = self.environment
inject['playbook_dir'] = os.path.abspath(self.basedir) inject['playbook_dir'] = os.path.abspath(self.basedir)
inject['omit'] = OMIT_PLACE_HOLDER
# template this one is available, callbacks use this # template this one is available, callbacks use this
delegate_to = self.module_vars.get('delegate_to') delegate_to = self.module_vars.get('delegate_to')
@ -867,11 +868,8 @@ class Runner(object):
# allow module args to work as a dictionary # allow module args to work as a dictionary
# though it is usually a string # though it is usually a string
if type(module_args) == dict: if isinstance(module_args, dict):
new_args = [] module_args = utils.serialize_args(module_args)
for (k, v) in module_args.iteritems():
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:
@ -893,13 +891,16 @@ class Runner(object):
except jinja2.exceptions.UndefinedError, e: except jinja2.exceptions.UndefinedError, e:
raise errors.AnsibleUndefinedVariable("One or more undefined variables: %s" % str(e)) raise errors.AnsibleUndefinedVariable("One or more undefined variables: %s" % str(e))
# filter omitted arguments out def not_omitted(item):
new_complex_args = {} return item[1] != OMIT_PLACE_HOLDER
for key, value in complex_args.iteritems():
if value == OMIT_PLACE_HOLDER: if module_name not in ['shell', 'command']:
continue # filter omitted arguments out from complex_args
new_complex_args[key] = value complex_args = dict(filter(not_omitted, complex_args.iteritems()))
complex_args = new_complex_args # filter omitted arguments out from module_args
module_kv = utils.parse_kv(module_args)
module_kv = dict(filter(not_omitted, module_kv.iteritems()))
module_args = utils.serialize_args(module_kv)
result = handler.run(conn, tmp, module_name, module_args, inject, complex_args) result = handler.run(conn, tmp, module_name, module_args, inject, complex_args)
# Code for do until feature # Code for do until feature

View file

@ -32,7 +32,6 @@ 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
from jinja2.runtime import Undefined
def to_nice_yaml(*a, **kw): def to_nice_yaml(*a, **kw):
@ -239,12 +238,6 @@ def rand(environment, end, start=None, step=None):
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):
if isinstance(a, Undefined):
return OMIT_PLACE_HOLDER
return a
class FilterModule(object): class FilterModule(object):
''' Ansible core jinja2 filters ''' ''' Ansible core jinja2 filters '''
@ -316,6 +309,4 @@ class FilterModule(object):
# random numbers # random numbers
'random': rand, 'random': rand,
'default_omit': default_omit,
} }

View file

@ -787,6 +787,10 @@ def _validate_both_dicts(a, b):
"failed to combine variables, expected dicts but got a '%s' and a '%s'" % (type(a).__name__, type(b).__name__) "failed to combine variables, expected dicts but got a '%s' and a '%s'" % (type(a).__name__, type(b).__name__)
) )
def serialize_args(args):
''' convert a dict to a string of key/value items '''
return ' '.join("%s='%s'" % item for item in args.iteritems())
def merge_hash(a, b): def merge_hash(a, b):
''' recursively merges hash b into a ''' recursively merges hash b into a
keys from b take precedence over keys from a ''' keys from b take precedence over keys from a '''

View file

@ -172,3 +172,32 @@
assert: assert:
that: that:
- nested_include_var is undefined - nested_include_var is undefined
- name: test omit in complex args
set_fact:
foo: bar
spam: "{{ omit }}"
should_not_omit: "prefix{{ omit }}"
- assert:
that:
- foo == 'bar'
- spam is undefined
- should_not_omit == "prefix{{ omit }}"
- name: test omit in module args
set_fact: >
yo=whatsup
eggs="{{ omit }}"
default_omitted="{{ not_exists|default(omit) }}"
should_not_omit_1="prefix{{ omit }}"
should_not_omit_2="{{ omit }}suffix"
- assert:
that:
- yo == 'whatsup'
- eggs is undefined
- default_omitted is undefined
- should_not_omit_1 == "prefix{{ omit }}"
- should_not_omit_2 == "{{ omit }}suffix"