0

I want to create a Custom Module call API which will take

  • action: get or search
  • resource: car/truck/plane/ship
  • optional parameters - dynamic parameter based on action and resource

For example if action is get and resource is car, then I want to add wheels, doors as module parameter.

module_args = {
        'action': {'required': True, 'type': 'str', choice:['get','search']},
        'resource': {'required': True, 'type': 'str', choice: ['car', 'truck', 'ship']},
         }

module = AnsibleModule(argument_spec=module_args,
         required_if('resource', 'car', ('wheels', 'doors'))
         supports_check_mode=True
         )

Is there a way to put required_if on two options i.e if action == 'search' and resource =='car' then use different set of arguments?

U880D
  • 8,601
  • 6
  • 24
  • 40
tempuser
  • 1,517
  • 3
  • 11
  • 15
  • 2
    I don't think there's specific support for that, but you can perform whatever addittional validations you want after instantiating your `AnsibleModule`. – larsks Jul 06 '23 at 13:22
  • But you could set `module_args` of `wheels` or `doors` to `optional` upfront and check later on the requirements within the module. Or maybe do the validation with the Ansible playbook or task as in [Set additional Ansible module parameter only if variable is defined](https://stackoverflow.com/questions/36885689/). – U880D Jul 06 '23 at 13:49
  • I am just wondering if documentation example about [argument_spec `required_if`](https://docs.ansible.com/ansible/latest/dev_guide/developing_program_flow_modules.html#argument-spec) isn't what you are looking for. – U880D Jul 06 '23 at 13:55
  • I am looking for a way to avoid writing multiple if else conditions as I have a large set of option, apparently that's the only option. – tempuser Jul 06 '23 at 14:29

0 Answers0