Check a playbook is passing correct arguments when including a role by using the role argument spec? #3669
-
Hi, I'm trying to see if this is already possible, but I didn't find anything in the documentation, but I was hoping there was a way to have Ansible Lint validate a playbook is passing the correct arguments when including a role, by comparing the arguments to that role's argument spec. For example, imagine you have the following playbook:
and the
I would expect Ansible Lint to report that the playbook failed to validate, because it is passing a role argument Similarly if the playbook contained incorrect values, or missing arguments, those cases should also be handled too. Is such a thing possible? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 5 replies
-
Yes it is possible, see the example here, to implement this create a testvarnames: "{{ q('varnames', '^test_') | sort }}"
testhostvars: "{{ dict(testvarnames | list | zip(q('vars', *testvarnames))) }}" Then add this to - name: Check test_ variables using meta/argument_specs.yml
ansible.builtin.validate_argument_spec:
argument_spec: "{{ (lookup('ansible.builtin.file', 'meta/argument_specs.yml') | from_yaml)['argument_specs']['main']['options'] }}"
provided_arguments: "{{ testhostvars }}" |
Beta Was this translation helpful? Give feedback.
-
Hmm, I'm not quite sure this is what we need, in this case the argument spec already exists and is already tested by Ansible when you attempt to run the playbook, however it would be nice if the same playbook and argument spec could be tested by Ansible Lint so any errors are caught during testing, not during execution of the playbook. We'd like to do this generically for all the playbooks and roles in our repository. Have I misunderstood your answer? |
Beta Was this translation helpful? Give feedback.
-
The answer I posted above results in the variables being checked when the role is run, however it is a more strict check than the one automatically run since any variable that is defined with a This approach could be run using Molecule if you are using that for testing? However I think you are asking for the support that was added for the argument spec to be enhanced? |
Beta Was this translation helpful? Give feedback.
-
No, you have to define it, for example add this to testrolevarnames: "{{ q('varnames', '^testrole_') | sort }}"
testrolehostvars: "{{ dict(testrolevarnames | list | zip(q('vars', *testrolevarnames))) }}" And then use this task to validate all variables starting with - name: Check test_ variables using meta/argument_specs.yml
ansible.builtin.validate_argument_spec:
argument_spec: "{{ (lookup('ansible.builtin.file', 'meta/argument_specs.yml') | from_yaml)['argument_specs']['main']['options'] }}"
provided_arguments: "{{ testrolehostvars }}"
That is correct but it is only the additional check that will catch a |
Beta Was this translation helpful? Give feedback.
-
Ok thanks very much, and sorry for my initial confusion :) |
Beta Was this translation helpful? Give feedback.
No, you have to define it, for example add this to
vars/main.yml
:And then use this task to validate all variables starting with
testrole_
: