-
Notifications
You must be signed in to change notification settings - Fork 635
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
False positive: deprecated-bare-vars on relative path to with_filetree #1621
Comments
I find this is not true in my case, as when I run ansible-lint 5.3.1 I see this:
The task looks like this: - name: Create my directories
file:
path: "{{ temp.path }}/{{ item.path }}"
state: directory
mode: "{{ item.mode }}"
with_community.general.filetree: templates/mydir/
when: item.state == "directory" In my case, I resolved this error by doing this: - name: Create my directories
file:
path: "{{ temp.path }}/{{ item.path }}"
state: directory
mode: "{{ item.mode }}"
loop: >-
{{ lookup('community.general.filetree', 'templates/mydir/') |
selectattr('state', 'equalto', 'directory') | list }} ...which has the added advantage of filtering out the files I don't need for this task instead of littering the stdout with SKIPPED lines. |
It may be, I had already changed all Edit: @philipsd6 I think if you are using a late Ansible version the generator object unrolling is automatic and |
Nice! I hadn't noticed that in any release notes, but I try to write code that people can run with the oldest supported releases anyway, even if I always develop right off the |
|
For anyone else hitting this, I got this error with:
This did not flag the lint error:
|
I have the same false positive. VersionsThe commands $ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04 LTS
Release: 22.04
Codename: jammy
$ ansible --version
ansible [core 2.12.6]
config file = /home/olibre/project/ansible.cfg
configured module search path = ['/home/olibre/project/.ansible/plugins/modules']
ansible python module location = /home/olibre/project/.venv/lib/python3.10/site-packages/ansible
ansible collection location = /home/olibre/project/.ansible/collections
executable location = /home/olibre/project/.venv/bin/ansible
python version = 3.10.4 (main, Apr 2 2022, 09:04:19) [GCC 11.2.0]
jinja version = 3.1.2
libyaml = True
$ ansible-lint --version
ansible-lint 6.2.2 using ansible 2.12.6 Task- name: Create remote directories
ansible.builtin.file:
path: /remote/path/{{ item.path }}
state: directory
mode: a=,u=rX
with_community.general.filetree: templates/
when: item.state == "directory" Warning message$ export PIPENV_VENV_IN_PROJECT="1"
$ export ANSIBLE_CONFIG=ansible.cfg
$ python3 -Wi -m pipenv run ansible-lint -v containers.yml
INFO Running ansible-galaxy role install -vr requirements.yml --roles-path /home/olibre/.cache/ansible-compat/0cb87f/roles
INFO Set ANSIBLE_LIBRARY=/home/olibre/.cache/ansible-compat/0cb87f/modules:/home/olibre/project/.ansible/plugins/modules
INFO Set ANSIBLE_COLLECTIONS_PATH=/home/olibre/.cache/ansible-compat/0cb87f/collections:/home/olibre/project/.ansible/collections
INFO Set ANSIBLE_ROLES_PATH=/home/olibre/.cache/ansible-compat/0cb87f/roles:roles:/home/olibre/project/.ansible/roles
INFO Set ANSIBLE_LIBRARY=/home/olibre/.cache/ansible-compat/0cb87f/modules:/home/olibre/project/.ansible/plugins/modules
INFO Set ANSIBLE_COLLECTIONS_PATH=/home/olibre/.cache/ansible-compat/0cb87f/collections:/home/olibre/project/.ansible/collections
INFO Set ANSIBLE_ROLES_PATH=/home/olibre/.cache/ansible-compat/0cb87f/roles:roles:/home/olibre/project/.ansible/roles
WARNING Overriding detected file kind 'yaml' with 'playbook' for given positional argument: containers.yml
INFO Executing syntax check on containers.yml (0.67s)
WARNING Listing 1 violation(s) that are fatal
deprecated-bare-vars: Found a bare variable 'templates/' used in a 'with_community.general.filetree' loop. You should use the full variable syntax ('{{ templates/ }}')
roles/containers/tasks/main.yml:6 Task/Handler: Create remote directories
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
- deprecated-bare-vars # Using bare variables is deprecated.
Finished with 1 failure(s), 0 warning(s) on 11 files. WorkaroundUse the @philipsd6's idea: - name:Create remote directories
ansible.builtin.file:
path: /remote/path/{{ item.path }}
state: directory
mode: a=,u=rX
loop: >-
{{ lookup('community.general.filetree', 'templates/') |
selectattr('state', 'equalto', 'directory') | list }} Note: I like this workaround because it avoids printing verbose [defaults]
# Avoid verbose "skipping:..." when creating remote directories.
display_skipped_hosts = no |
My impression is that this is not a false positive and can be avoided by converting it from a string value to a list with one string. When it is a string, it must be a templated string, like If confirmed, we should update the message to make it clear. |
Should I also mention that with_flattened was removed in ansible 2.13, so it does not make sense to complain about it. |
Yep @ssbarnea Just successfully tested with - name: Create remote directories
ansible.builtin.file:
path: /remote/path/{{ item.path }}
state: directory
mode: a=,u=rX
with_community.general.filetree: "{{ 'templates/' }}"
when: item.state == "directory" |
The lookup plugin `with_filetree` has been moved to the `community.general` collection. Also, Ansible Lint expects that the value specified for `with_community.general.filetree` must be variable of template. Simply passing setting it to `data/` gives the following error: ``` deprecated-bare-vars: Possible bare variable 'data/' used in a 'with_community.general.filetree' loop. You should use the full variable syntax ('{{ data/ }}') or convert it to a list if that is not really a variable. ``` Converting it to a list also doesn't work, this still gives the same error. This 'problem' is known, but it is not considered a bug: ansible/ansible-lint#1621 The suggested workaround is therefore used: putting the value as a string inside a jinja2 template.
Summary
Giving a relative path that does not end with a slash to
with_filetree
causes false positive fordeprecated-bare-vars
.deprecated-bare-vars: Found a bare variable 'templates/files.d' used in a 'with_filetree' loop. You should use the full variable syntax ('{{ templates/files.d }}')
Issue Type
Ansible and Ansible Lint details
OS / ENVIRONMENT
EL7.9 fully updated
STEPS TO REPRODUCE
Desired Behaviour
It should not complain since this is not a variable it is a path.
Suggest that if
with_filetree
contains slashes (possibly without spaces around the slash? if it is being confused with number division) then it is not treated as a bare var.Actual Behaviour
The relative path is detected as a bare variable.
A fix is to add a slash at the end of the path, for example:
This makes ansible-lint not detect the "bare var". But usually paths do not contain the final slash at the end.
The text was updated successfully, but these errors were encountered: