'Ansible lint : Found a bare variable

This is my ansible script.

- name: Validate that blacklisted URLs are unreachable
  environment:
    SSL_CERT_FILE: "{{ ssl_cert_path }}"
  ansible.builtin.uri:
    url: "{{ item }}"
    timeout: 10
  register: blacklisted_http_responses
  with_lines: cat {{ role_path }}/files/blacklisted_urls.txt

And i am getting this lint error for the sbove code

Found a bare variable 'cat {{ role_path }}/files/blacklisted_urls.txt' used in a 'with_lines' loop.

any idea how to resolve this ? I tried Putting the variable name in double quotes.



Solution 1:[1]

What you see is very probably an ansible-lint issue. You should use loop instead of with_lines. There are no complaints by ansible-lint about the code below

  loop: "{{ lookup('file',
                   role_path ~ '/files/blacklisted_urls.txt').splitlines() }}"

You can also use the pipe lookup plugin instead of the file if you want to. The loop below gives the same result

  loop: "{{ lookup('pipe',
                   'cat ' ~ role_path ~ '/files/blacklisted_urls.txt').splitlines() }}"

For example, the playbook

shell> cat pb.yml
---
- hosts: localhost
  roles:
    - role_a

the role

shell> cat roles/role_a/tasks/main.yml
---
- name: Debug
  debug:
    var: item
  loop: "{{ lookup('file',
                   role_path ~ '/files/blacklisted_urls.txt').splitlines() }}"

and the file

shell> cat roles/role_a/files/blacklisted_urls.txt 
www1.example.com
www2.example.com

give (abridged)

TASK [role_a : Debug] ****************************************************
ok: [localhost] => (item=www1.example.com) => 
  ansible_loop_var: item
  item: www1.example.com
ok: [localhost] => (item=www2.example.com) => 
  ansible_loop_var: item
  item: www2.example.com

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1