0

I'm trying to create a role with argument validation via the meta/arguments_spec.yml file, and I'd like a user to be able to pass in a tree of arbitrary depth as one of the arguments. In other words, a valid family_tree argument might look like:

family_tree:
  name: Sarah
  children:
    - name: John
    - name: Jimmy
    - name: Billy

But it may also look like:

family_tree:
  name: Bob
  children:
    - name: Bob, Jr.
      children:
        - name: Bob, III
          children:
            - name: Bob, IV
              children:
                # and so on...

This would require a recursive reference of some kind, however, I can't seem to find anything that supports this in the native argument_spec options. I know how to create recursive references via JSON Schema, and I'm aware the ansible.utils.validate supports JSON Schema as well, but as far as I can tell, this would mean manually adding a task to the beginning of my role's tasks list, which defeats the purpose of a dedicated argument_spec file and makes things harder to reason about, so not my preferred option.

user3781737
  • 932
  • 7
  • 17
  • sorry but i dont understant your problem....could you explain with a sample what you want to do? the result waited.... – Frenchy Aug 04 '22 at 08:17
  • @Frenchy I want to create an `argument_specs` definition to validate any arguments a user passes into a role. I know Ansible already supports this, but it seems like I can only create `argument_specs` for parameters of fixed depth, e.g. a spec for a dict-of-dicts-of-dicts has to be explicitly defined down to the inner-most dict. However, in a tree-like parameter like in my question, the depth is arbitrary. The deepest node may only be the dict at the top level (e.g. `family_tree.name`), or it may be the dict at the `N-th` level (e.g. `family_tree.children[0].children[0]....children[0].name`). – user3781737 Aug 04 '22 at 13:48
  • @Frenchy In other words, both of the two `family_tree` vars in my initial question should be considered valid for the role, but one that looks like `family_tree: { "name": "Bob", "children": [ { "name": "Bob, Jr.", "children": [false] } ] }` should be considered invalid by the role because in that example, `family_tree.children[0].children[0]` is a boolean instead of a dict. – user3781737 Aug 04 '22 at 14:08
  • @Frenchy no, not that it's always a dict. That part is easy. It's the fact that the top-level dict should have an optional property `children`, which should be a list of dicts. But every dict inside that list may _also_ have an optional `children` property, which if provided should be a list of dicts. And each of those may _also_ have an optional `children` property, which if provided should be a list of dicts. And each of those may _also_ have an optional `children` property, which if provided should be a list of dicts. And each of those... and so on. – user3781737 Aug 04 '22 at 14:50
  • yes ok i understant...so why not rewrite your module? its not so complex to adapt the code source of the module – Frenchy Aug 04 '22 at 14:53
  • @Frenchy I'm not sure I understand. The `argspec_validate.py` file you linked is what defines the options available for an `argument_spec`, right? Well based on that, I don't see how with those options, I could create something that correctly validates a nested dict of arbitrary depth. – user3781737 Aug 04 '22 at 15:04
  • 1
    you have to adapt the code of def validate(self): to your problem...could you write a code in python wich validate or invalidate the arg? – Frenchy Aug 04 '22 at 15:30

0 Answers0