Ansible-lint to exclude specific rules

Hi,

I have put different playbooks within several directories in the gitlab repository and would like to execute ansible-lint to validate my playbooks. In one of the playbooks, I have utilized an undefined variable (intended to provide the value as an environmental variable), which caused ansible-lint to fail with the error shown below.

 lint_output='ttps://ansible.readthedocs.io/projects/lint/rules/syntax-check/yntax-check[specific]: The field '\''hosts'\'' has an invalid value, which includes an undefined variable.. '\''target_host'\'' is undefined
playbook.yml:2:3

Despite attempting multiple options to bypass this error, I couldn’t succeed. Could anyone provide any suggestions to resolve this issue?

Last June I opened an issue on the lint GitHub for this: False positive: hosts variable not defined · Issue #4225 · ansible/ansible-lint · GitHub

Yes, i have seen that issue. It is still open. Do we have any workaround where we can ignore this errors while running ansible-lint, because of this i am getting failed error.

Maybe try adding this to your lint config?

extra_vars:
  target_host: foo

Hello Selvi, I am learning ansible on my lab, can you send me your playbook which is generating errors while running ansible-lint. i want to have a look to reproduce the error and try to fix.

Sure, I will do.

---
- name: Running ad-hoc command on selected redhat-servers
  hosts: "{{ wanted_host | default('localhost') }}"
  become: true
  gather_facts: true
  tasks:
    - name: Running ad-hoc command {{ command | default() }} on {{ wanted_host | default('localhost') }} linux-server
      ansible.builtin.command: "{{ command | default() }}"
      register: output
      changed_when: false

    - name: output
      ansible.builtin.debug:
        var: output.stdout_lines

Sure, I will check it.

Thanks