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

Unquoted variable error is ignored #1718

Closed
konstruktoid opened this issue Sep 7, 2021 · 0 comments · Fixed by #2589
Closed

Unquoted variable error is ignored #1718

konstruktoid opened this issue Sep 7, 2021 · 0 comments · Fixed by #2589
Assignees
Labels
Milestone

Comments

@konstruktoid
Copy link
Contributor

https://github.com/ansible/ansible/blob/devel/lib/ansible/errors/__init__.py#L165-L166

$ ansible --version | grep -E 'version|core'
ansible [core 2.11.4]
  python version = 3.8.10 (default, Jun  2 2021, 10:49:15) [GCC 9.4.0]
  jinja version = 3.0.1
$ ansible-lint --version
Failed to guess project directory using git:
ansible-lint 5.1.3.dev22 using ansible 2.11.4
$ ansible-lint issue1681*
Failed to guess project directory using git:
WARNING  Overriding detected file kind 'yaml' with 'playbook' for given positional argument: issue1681-working.yml
WARNING  Overriding detected file kind 'yaml' with 'playbook' for given positional argument: issue1681.yml
WARNING  Listing 1 violation(s) that are fatal
syntax-check: Ansible syntax check failed
issue1681.yml:1 [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! We were unable to read either as JSON nor YAML, these are the errors we got from each:
JSON: Expecting value: line 1 column 1 (char 0)

Syntax Error while loading YAML.
  found unacceptable key (unhashable type: 'AnsibleMapping')

The error appears to be in '/tmp/issue1681.yml': line 21, column 16, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

      stat:
        path: {{ item | quote }}
               ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:

    with_items:
      - {{ foo }}

Should be written as:

    with_items:
      - "{{ foo }}"



Finished with 1 failure(s), 0 warning(s) on 2 files.

$ diff -uNr issue1681.yml  issue1681-working.yml
--- issue1681.yml       2021-09-07 11:28:54.604140602 +0200
+++ issue1681-working.yml       2021-09-07 11:28:27.504140867 +0200
@@ -18,7 +18,7 @@

     - name: stat quote path
       stat:
-        path: {{ item | quote }}
+        path: "{{ item | quote }}"
       with_items:
         - "/tmp/aaa"
 ...
---
- hosts: localhost
  tasks:
    - name: stat with quotes
      stat:
        path: "{{ item }}"
      with_items:
        - "/tmp/aaa"

    - name: command without quotes
      command: cat {{ myfile | quote }}
      register: myoutput
      changed_when: myoutput.rc != 0

    - name: command without quotes
      command: cat {{ myfile }}
      register: myoutput

    - name: stat quote path
      stat:
        path: {{ item | quote }}
      with_items:
        - "/tmp/aaa"
...
$ ansible-lint --version
ansible-lint 5.1.3.dev22 using ansible 2.11.4
$ cat /tmp/issue1681.yml
---
- hosts: all
  tasks:
    - name: command without quotes
      ansible.builtin.command: cat {{ myfile|quote }}
      register: myoutput
      changed_when: myoutput.rc != 0

    - name: command should fail
      ansible.builtin.command: cat {{ myfile|quote }}
...
$ ansible-lint /tmp/issue1681.yml
Loading custom .yamllint config file, this extends our internal yamllint config.
WARNING  Overriding detected file kind 'yaml' with 'playbook' for given positional argument: /tmp/issue1681.yml
WARNING  Listing 1 violation(s) that are fatal
no-changed-when: Commands should not change things if nothing needs doing
../../../../tmp/issue1681.yml:9 Task/Handler: command should fail

You can skip specific rules or tags by adding them to your configuration file:
# .ansible-lint
warn_list:  # or 'skip_list' to silence them completely
  - no-changed-when  # Commands should not change things if nothing needs doing

Finished with 1 failure(s), 0 warning(s) on 1 files.
$

Originally posted by @konstruktoid in #1706 (comment)

@ssbarnea ssbarnea self-assigned this Oct 1, 2022
@ssbarnea ssbarnea added the bug label Oct 1, 2022
@ssbarnea ssbarnea added this to the 6.8.x milestone Oct 1, 2022
ssbarnea added a commit to ssbarnea/ansible-lint that referenced this issue Oct 10, 2022
ssbarnea added a commit that referenced this issue Oct 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants