semaphor-dk / dansabel

Pre-flight linter for Jinja2/Ansible repositories with Git pre-commit hooks
ISC License
16 stars 1 forks source link

Basic static type inference #3

Open jokjr opened 3 years ago

jokjr commented 3 years ago

While full blown type inference may not be feasible, it should be quite doable to catch at least some basic things, like:

selectattr | length

Where Ansible will complain with something like:

The conditional check 'dbms_postgres_reconfig.results | selectattr('changed'
) | selectattr('restart_required') | length' failed.

The error was: Unexpected templating type error occurred on ({% if dbms_postgres_reconfig.results | selectattr('changed') | selectattr('restart_required') | length %} True {% else %} False {% endif %}
):
object of type 'generator' has no len()

The error appears to be in '/home/vagrant/ansible/roles/dbms/tasks/main.yml': line 124, column
 3, but may\nbe elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:
- name: restart postgres if our changes necessitate it 
  ^ here"

Basically we can infer that the selectattr filter is a generator, and that length will not work on a generator, but needs a list.

jokjr commented 2 years ago

Another blunder that we could spot with this would be

- name: do something
  when: "ansible_distribution == 'Debian' and ansible_distribution_version >= 11"
  ...

which yields

 AnsibleError: Unexpected templating type error occurred on ........ '>=' not supported between instances of 'AnsibleUnsafeText' and 'int'