Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add required_if to AnsibleModule #9423

Merged
merged 1 commit into from
May 26, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 17 additions & 1 deletion lib/ansible/module_utils/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,8 @@ class AnsibleModule(object):

def __init__(self, argument_spec, bypass_checks=False, no_log=False,
check_invalid_arguments=True, mutually_exclusive=None, required_together=None,
required_one_of=None, add_file_common_args=False, supports_check_mode=False):
required_one_of=None, add_file_common_args=False, supports_check_mode=False,
required_if=None):

'''
common code for quickly building an ansible module in Python
Expand Down Expand Up @@ -295,6 +296,7 @@ def __init__(self, argument_spec, bypass_checks=False, no_log=False,
self._check_argument_types()
self._check_required_together(required_together)
self._check_required_one_of(required_one_of)
self._check_required_if(required_if)

self._set_defaults(pre=False)
if not self.no_log:
Expand Down Expand Up @@ -852,6 +854,20 @@ def _check_required_arguments(self):
if len(missing) > 0:
self.fail_json(msg="missing required arguments: %s" % ",".join(missing))

def _check_required_if(self, spec):
''' ensure that parameters which conditionally required are present '''
if spec is None:
return
for (key, val, requirements) in spec:
missing = []
if key in self.params and self.params[key] == val:
for check in requirements:
count = self._count_terms(check)
if count == 0:
missing.append(check)
if len(missing) > 0:
self.fail_json(msg="%s is %s but the following are missing: %s" % (key, val, ','.join(missing))

def _check_argument_values(self):
''' ensure all arguments have the requested values, and there are no stray arguments '''
for (k,v) in self.argument_spec.iteritems():
Expand Down