Skip to content
New issue

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

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

Already on GitHub? # to your account

False positive: hosts variable not defined #3229

Closed
skwde opened this issue Mar 30, 2023 · 1 comment
Closed

False positive: hosts variable not defined #3229

skwde opened this issue Mar 30, 2023 · 1 comment
Labels

Comments

@skwde
Copy link

skwde commented Mar 30, 2023

Summary

False positve for undefined variable in hosts.

Issue Type
  • Bug Report
OS / ENVIRONMENT
$ ansible-lint --version
ansible-lint 6.10.2 using ansible 2.14.3
A new release of ansible-lint is available: 6.10.2 → 6.14.3 Upgrade by running: pip install --upgrade ansible-lint
  • ansible installation method: python (conda)
  • ansible-lint installation method: python (conda)
STEPS TO REPRODUCE

Save the following playbook

---
- name: Test1
  hosts: "{{ target1 }}"
  gather_facts: false
  vars_prompt:

    - name: target1
      prompt: "Specify deployment target(s)"
      private: false
      default: 'localhost'

  tasks:
    - name: Store Variable for cross play visibility
      ansible.builtin.set_fact:
        target: "{{ target1 }}"
      run_once: true
      delegate_facts: true
      delegate_to: localhost

    - name: Output1
      ansible.builtin.debug:
        msg:
          - "{{ target1 }}"
          - "{{ hostvars['localhost']['target'] }}"

- name: Test2
  hosts: "{{ hostvars['localhost']['target'] }}"
  gather_facts: true
  tasks:
    - name: Output2
      ansible.builtin.debug:
        msg:
          - "{{ hostvars['localhost']['target'] }}"

as test.yml.

Run

ansible-lint playbooks/test1.yml

and get

WARNING  Listing 1 violation(s) that are fatal
syntax-check[specific]: The field 'hosts' has an invalid value, which includes an undefined variable. The error was: 'hostvars' is undefined. 'hostvars' is undefined
playbooks/test2.yml:26:3

You can skip specific rules or tags by adding them to your configuration file:
# .config/ansible-lint.yml
warn_list:  # or 'skip_list' to silence them completely
  - syntax-check[specific]  # Ansible syntax check failed.

                  Rule Violation Summary                   
 count tag                    profile rule associated tags 
     1 syntax-check[specific] min     core                 

Failed after : 1 failure(s), 0 warning(s) on 1 files.
A new release of ansible-lint is available: 6.10.2 → 6.14.3 Upgrade by running: pip install --upgrade ansible-lint

Note, also

ansible-playbook --syntax-check test.yml 

gives

[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
ERROR! The field 'hosts' has an invalid value, which includes an undefined variable. The error was: 'hostvars' is undefined. 'hostvars' is undefined

The error appears to be in '<path to test>/test.yml': line 26, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:


- name: Test2
  ^ here
Desired Behavior

No Errors from above playbook

Possible security bugs should be reported via email to security@ansible.com

@skwde skwde added bug new Triage required labels Mar 30, 2023
@ssbarnea
Copy link
Member

ssbarnea commented Apr 4, 2023

Please read the documentation page, it gives an exact fix for your problem and also an explanation. https://ansible-lint.readthedocs.io/rules/syntax-check/

@ssbarnea ssbarnea closed this as completed Apr 4, 2023
@ansible ansible locked as resolved and limited conversation to collaborators Apr 4, 2023
@ssbarnea ssbarnea removed the new Triage required label Apr 4, 2023
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants