ansible 循环中的 Jinja 模板

Cod*_*irl 1 jinja2 ansible

我需要根据 CSV 文件的输入运行 ansible 循环。我正在使用以下问题/答案作为参考。但是,我似乎无法弄清楚在哪里实际包含循环的 jinja 部分。

到目前为止,这就是我所拥有的,但它会引发错误:

---
- hosts: localhost
  connection: local
  gather_facts: no

  vars:
    csv_var: "{{ lookup ('file', 'file.csv') }}"

  tasks:
  - debug:
      msg: "{{ item }}"
    with_items:
      - {% set list = csv_var.split(",") %}
Run Code Online (Sandbox Code Playgroud)

file.csv有以下内容:345,1234,1234

理想情况下,该消息应打印出上面的数字。

我得到的语法错误是:

The offending line appears to be:

    with_items:
      - {% set list = csv_var.split(",") %}
         ^ here

exception type: <class 'yaml.scanner.ScannerError'>
exception: while scanning for the next token
found character that cannot start any token
  in "<unicode string>", line 19, column 10
Run Code Online (Sandbox Code Playgroud)

tec*_*raf 5

您应该使用 Jinja2 表达式而不是语句。

您还应该引用 Ansible 中以以下形式开头的任何字符串{

- debug:
    msg: "{{ item }}"
  with_items: "{{ csv_var.split(',') }}"
Run Code Online (Sandbox Code Playgroud)

并且无需将结果列表包装在另一个列表中(元素前破折号),尽管 Ansible 会自动处理此问题。