Skip to content

Instantly share code, notes, and snippets.

@ganeshrn
Created March 15, 2022 07:30
Show Gist options
  • Save ganeshrn/8f085ef84887439ae12b68e5442ed78e to your computer and use it in GitHub Desktop.
Save ganeshrn/8f085ef84887439ae12b68e5442ed78e to your computer and use it in GitHub Desktop.
Case insenstive choice example
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = """
module: sample_choices
short_description: Demo case insensitive choices.
description:
- Demo case insensitive choices.
version_added: 1.0.0
options:
foo:
description:
- Option that take case insensitive choices.
Valid values are one of I(bar), I(bam), I(baz) and are case insensitive.
required: true
"""
from ansible.module_utils.basic import AnsibleModule
FOO_CHOICES = ['bar', 'bam', 'baz']
def case_insensitive_choice(value, **kwargs):
if value.lower() not in FOO_CHOICES:
msg = f"Invalid value {value}. Valid value is one of {', '.join(FOO_CHOICES)} and is case insensitive"
raise ValueError(msg)
def main():
"""entry point for module execution"""
argument_spec = dict(
foo=dict(required=True, type=case_insensitive_choice),
)
result = {"changed": False}
module = AnsibleModule(
argument_spec=argument_spec, supports_check_mode=True
)
module.exit_json(**result)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment