Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ansible-lint --tags includes yaml rules no matter which tags are given #3896

Closed
krichter722 opened this issue Nov 14, 2023 · 2 comments · Fixed by #4107
Closed

ansible-lint --tags includes yaml rules no matter which tags are given #3896

krichter722 opened this issue Nov 14, 2023 · 2 comments · Fixed by #4107
Labels

Comments

@krichter722
Copy link

Summary

ansible-lint --tags key-order reports key-order and yaml violations

Issue Type
  • Bug Report
OS / ENVIRONMENT
$ ansible-lint --version
ansible-lint 6.22.0 using ansible-core:2.16.0 ansible-compat:4.1.10 ruamel-yaml:0.18.5 ruamel-yaml-clib:0.2.8
  • ansible installation method: pip in venv
  • ansible-lint installation method: pip in venv
STEPS TO REPRODUCE

run ansible-lint --tags key-order on a rule having a key-order and a yaml violation

Desired Behavior

Selecting tags is for fixing violations for a specific rule. Unwanted yaml rule violations make the output hard to read.

Actual Behavior
$ cat roles/hello/tasks/main.yml 
- debug:
    msg: "Hello, world!"
  name: I should be on top
  tags: [ yaml_bracket_violation ]

$ venv/bin/ansible-lint -vvvv --tags key-order roles/hello/tasks/main.yml 
WARNING: PATH altered to include /home/richter/examples/ansible-lint-yaml-tags/venv/bin :: This is usually a sign of broken local setup, which can cause unexpected behaviors.
DEBUG    Logging initialized to level 10
INFO     Identified / as project root due file system root.
DEBUG    Options: Options(_skip_ansible_syntax_check=False, cache_dir=PosixPath('/home/richter/.cache/ansible-compat/e3b0c4'), colored=True, configured=True, cwd=PosixPath('/home/richter/examples/ansible-lint-yaml-tags'), display_relative_path=True, exclude_paths=['.cache', '.git', '.hg', '.svn', '.tox'], format=None, lintables=['roles/hello/tasks/main.yml'], list_rules=False, list_tags=False, write_list=[], parseable=False, quiet=0, rulesdirs=[PosixPath('/home/richter/examples/ansible-lint-yaml-tags/venv/lib/python3.11/site-packages/ansiblelint/rules')], skip_list=[], tags=['key-order'], verbosity=4, warn_list=['experimental', 'jinja', 'fqcn'], mock_filters=[], mock_modules=[], mock_roles=[], loop_var_prefix=None, only_builtins_allow_collections=[], only_builtins_allow_modules=[], var_naming_pattern=None, offline=None, project_dir='/', extra_vars=None, enable_list=[], skip_action_validation=True, strict=False, rules={}, profile=None, task_name_prefix='{stem} | ', sarif_file=None, config_file=None, generate_ignore=False, rulesdir=[], use_default_rules=False, version=False, list_profiles=False, ignore_file=None, max_tasks=100, max_block_depth=20)
DEBUG    CWD: /home/richter/examples/ansible-lint-yaml-tags
INFO     Set ANSIBLE_LIBRARY=/home/richter/.cache/ansible-compat/74a661/modules:/home/richter/.ansible/plugins/modules:/usr/share/ansible/plugins/modules
INFO     Set ANSIBLE_COLLECTIONS_PATH=/home/richter/.cache/ansible-compat/74a661/collections:/home/richter/.ansible/collections:/usr/share/ansible/collections
INFO     Set ANSIBLE_ROLES_PATH=/home/richter/.cache/ansible-compat/74a661/roles:roles:/home/richter/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles
DEBUG    Effective yamllint rules used: {'anchors': {'level': 'error', 'forbid-undeclared-aliases': True, 'forbid-duplicated-anchors': False, 'forbid-unused-anchors': False}, 'braces': {'level': 'error', 'forbid': False, 'min-spaces-inside': 0, 'max-spaces-inside': 1, 'min-spaces-inside-empty': -1, 'max-spaces-inside-empty': -1}, 'brackets': {'level': 'error', 'forbid': False, 'min-spaces-inside': 0, 'max-spaces-inside': 0, 'min-spaces-inside-empty': -1, 'max-spaces-inside-empty': -1}, 'colons': {'level': 'error', 'max-spaces-before': 0, 'max-spaces-after': 1}, 'commas': {'level': 'error', 'max-spaces-before': 0, 'min-spaces-after': 1, 'max-spaces-after': 1}, 'comments': {'level': 'warning', 'require-starting-space': True, 'ignore-shebangs': True, 'min-spaces-from-content': 1}, 'comments-indentation': False, 'document-end': False, 'document-start': False, 'empty-lines': {'level': 'error', 'max': 2, 'max-start': 0, 'max-end': 0}, 'empty-values': False, 'float-values': False, 'hyphens': {'level': 'error', 'max-spaces-after': 1}, 'indentation': {'level': 'error', 'spaces': 'consistent', 'indent-sequences': True, 'check-multi-line-strings': False}, 'key-duplicates': {'level': 'error'}, 'key-ordering': False, 'line-length': {'level': 'error', 'max': 160, 'allow-non-breakable-words': True, 'allow-non-breakable-inline-mappings': False}, 'new-line-at-end-of-file': {'level': 'error'}, 'new-lines': {'level': 'error', 'type': 'unix'}, 'octal-values': {'forbid-implicit-octal': True, 'forbid-explicit-octal': True, 'level': 'error'}, 'quoted-strings': False, 'trailing-spaces': {'level': 'error'}, 'truthy': {'level': 'warning', 'allowed-values': ['true', 'false'], 'check-keys': True}}
INFO     Set ANSIBLE_LIBRARY=/home/richter/.cache/ansible-compat/74a661/modules:/home/richter/.ansible/plugins/modules:/usr/share/ansible/plugins/modules
INFO     Set ANSIBLE_COLLECTIONS_PATH=/home/richter/.cache/ansible-compat/74a661/collections:/home/richter/.ansible/collections:/usr/share/ansible/collections
INFO     Set ANSIBLE_ROLES_PATH=/home/richter/.cache/ansible-compat/74a661/roles:roles:/home/richter/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles
DEBUG    Examining roles/hello/tasks/main.yml of type tasks
DEBUG    Skipping rule internal-error
DEBUG    Skipping rule load-failure
DEBUG    Skipping rule parser-error
DEBUG    Skipping rule warning
DEBUG    Running rule yaml
DEBUG    Skipping rule args
DEBUG    Skipping rule avoid-implicit
DEBUG    Skipping rule command-instead-of-module
DEBUG    Skipping rule command-instead-of-shell
DEBUG    Skipping rule complexity
DEBUG    Skipping rule deprecated-bare-vars
DEBUG    Skipping rule deprecated-local-action
DEBUG    Skipping rule deprecated-module
DEBUG    Skipping rule fqcn
DEBUG    Skipping rule galaxy
DEBUG    Skipping rule ignore-errors
DEBUG    Skipping rule inline-env-var
DEBUG    Skipping rule jinja
DEBUG    Running rule key-order
DEBUG    Skipping rule latest
DEBUG    Skipping rule literal-compare
DEBUG    Skipping rule loop-var-prefix
DEBUG    Skipping rule meta-incorrect
DEBUG    Skipping rule meta-no-tags
DEBUG    Skipping rule meta-runtime
DEBUG    Skipping rule meta-video-links
DEBUG    Skipping rule name
DEBUG    Skipping rule no-changed-when
DEBUG    Skipping rule no-free-form
DEBUG    Skipping rule no-handler
DEBUG    Skipping rule no-jinja-when
DEBUG    Skipping rule no-relative-paths
DEBUG    Skipping rule no-tabs
DEBUG    Skipping rule package-latest
DEBUG    Skipping rule partial-become
DEBUG    Skipping rule playbook-extension
DEBUG    Skipping rule risky-file-permissions
DEBUG    Skipping rule risky-octal
DEBUG    Skipping rule risky-shell-pipe
DEBUG    Skipping rule role-name
DEBUG    Skipping rule run-once
DEBUG    Skipping rule sanity
DEBUG    Skipping rule schema
DEBUG    Skipping rule var-naming
WARNING  Listing 2 violation(s) that are fatal
key-order[task]: You can improve the task key order to: name, debug, tags
roles/hello/tasks/main.yml:1 Task/Handler: I should be on top

yaml[brackets]: Too many spaces inside brackets
roles/hello/tasks/main.yml:4

Read documentation for instructions on how to ignore specific rule violations.

DEBUG    Determined rule-profile order: {'internal-error': (0, 'min'), 'load-failure': (1, 'min'), 'parser-error': (2, 'min'), 'syntax-check': (3, 'min'), 'command-instead-of-module': (4, 'basic'), 'command-instead-of-shell': (5, 'basic'), 'deprecated-bare-vars': (6, 'basic'), 'deprecated-local-action': (7, 'basic'), 'deprecated-module': (8, 'basic'), 'inline-env-var': (9, 'basic'), 'key-order': (10, 'basic'), 'literal-compare': (11, 'basic'), 'jinja': (12, 'basic'), 'no-free-form': (13, 'basic'), 'no-jinja-when': (14, 'basic'), 'no-tabs': (15, 'basic'), 'partial-become': (16, 'basic'), 'playbook-extension': (17, 'basic'), 'role-name': (18, 'basic'), 'schema': (19, 'basic'), 'name': (20, 'basic'), 'var-naming': (21, 'basic'), 'yaml': (22, 'basic'), 'name': (23, 'moderate'), 'name': (24, 'moderate'), 'name': (25, 'moderate'), 'spell-var-name': (26, 'moderate'), 'avoid-implicit': (27, 'safety'), 'latest': (28, 'safety'), 'package-latest': (29, 'safety'), 'risky-file-permissions': (30, 'safety'), 'risky-octal': (31, 'safety'), 'risky-shell-pipe': (32, 'safety'), 'galaxy': (33, 'shared'), 'ignore-errors': (34, 'shared'), 'layout': (35, 'shared'), 'meta-incorrect': (36, 'shared'), 'meta-no-tags': (37, 'shared'), 'meta-video-links': (38, 'shared'), 'meta-version': (39, 'shared'), 'meta-runtime': (40, 'shared'), 'no-changed-when': (41, 'shared'), 'no-changelog': (42, 'shared'), 'no-handler': (43, 'shared'), 'no-relative-paths': (44, 'shared'), 'max-block-depth': (45, 'shared'), 'max-tasks': (46, 'shared'), 'unsafe-loop': (47, 'shared'), 'avoid-dot-notation': (48, 'production'), 'sanity': (49, 'production'), 'fqcn': (50, 'production'), 'import-task-no-when': (51, 'production'), 'meta-no-dependencies': (52, 'production'), 'single-entry-point': (53, 'production'), 'use-loop': (54, 'production')}
               Rule Violation Summary               
 count tag             profile rule associated tags 
     1 key-order[task] basic   formatting           
     1 yaml[brackets]  basic   formatting, yaml     

Failed: 2 failure(s), 0 warning(s) on 1 files. Last profile that met the validation criteria was 'min'.

@krichter722 krichter722 added bug new Triage required labels Nov 14, 2023
@priyamsahoo priyamsahoo removed the new Triage required label Nov 22, 2023
@ssbarnea
Copy link
Member

True, we need to fix this. If anyone can look into raising a PR it would be awesome.

@vajeen
Copy link

vajeen commented Mar 11, 2024

What does has_dynamic_tags mean here

class YamllintRule(AnsibleLintRule, TransformMixin):
    """Violations reported by yamllint."""

    id = "yaml"
    severity = "VERY_LOW"
    tags = ["formatting", "yaml"]
    version_added = "v5.0.0"
    config = load_yamllint_config()
    has_dynamic_tags = True
    link = "https://yamllint.readthedocs.io/en/stable/rules.html"
    # ensure this rule runs before most of other common rules
    _order = 1
    _ids = {
        "yaml[anchors]": "",
        "yaml[braces]": "",
        "yaml[brackets]": "",
        "yaml[colons]": "",
        "yaml[commas]": "",
        "yaml[comments-indentation]": "",
        "yaml[comments]": "",
        "yaml[document-end]": "",
        "yaml[document-start]": "",
        "yaml[empty-lines]": "",
        "yaml[empty-values]": "",
        "yaml[float-values]": "",
        "yaml[hyphens]": "",
        "yaml[indentation]": "",
        "yaml[key-duplicates]": "",
        "yaml[key-ordering]": "",
        "yaml[line-length]": "",
        "yaml[new-line-at-end-of-file]": "",
        "yaml[new-lines]": "",
        "yaml[octal-values]": "",
        "yaml[quoted-strings]": "",
        "yaml[trailing-spaces]": "",
        "yaml[truthy]": "",
    }

Asking because I can see this condition in `RulesCollection`

def run(
...more code here

    for rule in self.rules:
        if rule.id == "syntax-check":
            continue
        if (
            not tags
            or rule.has_dynamic_tags
            or not set(rule.tags).union([rule.id]).isdisjoint(tags)
        ):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants