Ansible lint:发现一个裸变量



这是我的可见脚本。

- 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

和我得到这个lint错误的上述代码

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

你知道如何解决这个问题吗?我试着把变量名放在双引号里。

你看到的很可能是一个ansible-lint问题。你应该使用loop而不是with_linesansible-lint没有抱怨

下面的代码
loop: "{{ lookup('file',
role_path ~ '/files/blacklisted_urls.txt').splitlines() }}"

如果你愿意,你也可以使用管道查找插件来代替文件。下面的循环给出了相同的结果

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

例如:playbook

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

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

和文件

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

给(简略)

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

最新更新