semaphor-dk / dansabel Goto Github PK
View Code? Open in Web Editor NEWPre-flight linter for Jinja2/Ansible repositories with Git pre-commit hooks
License: ISC License
Pre-flight linter for Jinja2/Ansible repositories with Git pre-commit hooks
License: ISC License
For example:
- name: copy template
template:
src: /foo
dest: /bar/
register: did_copy
instead of
- name: copy template
template:
src: /foo
dest: /bar/
register: did_copy
This is on 5964a9f
(py_env-python3.10) [me@example pwd]$
(py_env-python3.10) [me@example pwd]$ jinjalint.py lint-example.yml
WARNING: potentially conflicting modules: {'connection', 'tasks'} at lint-example.yml:0 lines 3-13
(py_env-python3.10) [me@example pwd]$
---
# lint-example.yml
- name: show case for linting
hosts: localhost
gather_facts: no
connection: local
tasks:
- name: example task
debug:
msg: A placeholder.
...
As documented become_user: foo
or become_flags: .....
without become:
has no effect and we should raise it as an issue when encountered.
- name: make some directories
file:
type: directory
dest: "/tmp/directory_{{ item }}"
items:
- a
- b
will throw an error:
ERROR! conflicting action statements: file, items
because it should be with_items:
. It seems like a plausible typo and should be easy to detect that there's more than one key under a name:
when the second key isn't one of the finite set of additional allowed toplevel keys {with_items
, register
, when
, etc}.
It should be "Debian" in uppercase, so either comparison should be case-insensitive, or it should not be compared to "debian".
An easy measure to spot the most common mistakes might be to have a list of various ansible_distribution
and creating an annotation when the other comparison operand "looks like" one that we know.
Hello :)
I wanted to use dansabel on pre-commit.ci but it seems to be importing "too much":
Can this be fixed or is it by design? Thanks! π
what is YAML references / anchors
---
foo: &anchor
K1: "One"
K2: "Two"
<<: &anchor4
K4: "Four"
bar:
<<: *anchor
K2: "I Changed"
K3: "Three"
kilroy: *anchor4
{
"foo": {
"K1": "One",
"K2": "Two",
"K4": "Four"
},
"bar": {
"K1": "One",
"K2": "I Changed",
"K3": "Three",
"K4": "Four",
},
"kilroy": {
"K4": "Four"
}
}
We should be careful to handle cyclic references sensibly.
HI
I noticed this git hook, parses/lexers fine if the file extension is .j2 or without file extension for jinja2 code, but misses out on validating the YAML code.
If I change the file extension to .yml, it validates the YML but complains about jinja2 with the error below.
while scanning for the next token
found character '%' that cannot start any token
in "ansible/roles/createCloudformationStack/templates/cloudformation/cfn-route53.yml", line 15, column 2
YAML parser/lexer exit before end of document.
How can I validate both jinja2 and yml when they're present in the same file?
This bit me today, and should be simple enough to detect too:
https://medium.com/opsops/using-block-for-handlers-in-ansible-a55f45b62a96
The copy
module takes a path:
argument, and the file
module takes a dest:
argument.
This probably ties into a larger issue of mandatory arguments, but for now catching typos in these two as a special case would also be nice.
Below an example of invalid syntax; should be easy enough to add linting of this to the code that generates the "more than one module" warnings in jinjalint.py
- name: do lots of things
loop: "{{ my_collection }}"
block:
- name: foo
debug: msg=foomsg
- name: bar
debug: msg=barmsg
Eric Anderson has a neat workaround that we could suggest to the user: https://ericsysmin.com/2019/06/20/how-to-loop-blocks-of-code-in-ansible/
- name: start something
systemd:
name: myservice
started: True
is wrong, should be state: started
.
I've made this typo a number of times because the other directives are boolean, and it would be nice to catch this.
If there is no abc | split()
filter (on older Ansible versions) it may make sense to suggest (abc).split()
instead.
Trigger in https://github.com/tykling/ansible-roles , thanks to @tykling for the trigger.
(The problem is that we encounter endif
twice and close two scopes, one of them begin the for
scope. When we then try to pop the endfor
scope, we get this exception.
In this case Dansabel should add an annotation and either stop parsing or try to guess what the user meant and continue.
EDIT: It looks like we should also check that what we pop is actually endfor
:-)
I've used main
as rev
:
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
13 {{ hash_files|zip(query("file",*hash_files))|community.general.dict }}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13:18 βββββ variable_begin: {{
13:20 β£ββββββββ whitespace:
13:21 β£ββββββββββββββ name: hash_files
13:31 β£ββββββββββ operator: |
13:32 β£ββββββββββββββ name: zip
13:35 β ββββββββββββββ operator: (
13:36 β β£βββββββββββββββββ name: query
13:41 β β βββββββββββββββββ operator: (
13:42 β β β£ββββββββββββββββββ string: "file"
13:48 β β β£ββββββββββββββββ operator: ,
13:49 β β β£ββββββββββββββββ operator: *
13:50 β β β£ββββββββββββββββββββ name: hash_files
13:60 β β βββββββββββββββββ operator: )
13:61 β ββββββββββββββ operator: )
13:62 β£ββββββββββ operator: |
13:63 β£ββββββββββββββ name: community
β Not a builtin filter? Maybe: comment, countβ
13:72 β£ββββββββββ operator: .
13:73 β£ββββββββββββββ name: general
13:80 β£ββββββββββ operator: .
13:81 β£ββββββββββββββ name: dict
13:85 β£ββββββββ whitespace:
13:86 βββββββ variable_end: }}
There are a lot of namespaces except ansible
:
https://docs.ansible.com/ansible/latest/collections/index_filter.html
Also you can create your own non-public collections.
in addition to --external
it would be great to have --tags
63 {{ composes|ansible.utils.keep_keys(target=["name", "short_id", "service", "image", "status"])|ansible.utils.to_paths }}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
63:23 βββββ variable_begin: {{
63:25 β£ββββββββ whitespace:
63:26 β£ββββββββββββββ name: composes
63:34 β£ββββββββββ operator: |
63:35 β£ββββββββββββββ name: ansible
β Not a builtin filter? Maybe: title, sliceβ
63:42 β£ββββββββββ operator: .
63:43 β£ββββββββββββββ name: utils
63:48 β£ββββββββββ operator: .
63:49 β£ββββββββββββββ name: keep_keys
63:58 β ββββββββββββββ operator: (
63:59 β β£βββββββββββββββββ name: target
63:65 β β£βββββββββββββ operator: =
PS: Adding ansible to additional_dependencies doesn't helps.
The linter fails when I use {% raw %} ... {% endraw %}
in my templated yml files
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
.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. πππ
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google β€οΈ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.