任务中的未定义变量

aoenger

我用分子来测试角色。任务是使用变量的复制模板。
ansible /任务/main.yml

---
- name: copy manifest billing
  template:
    src: templates/service.j2
    dest: "{{ item }}"
    with_items:
      "{{ services }}"

ansible / vars / main.yml

services:
  - billing
  - cart
  - checkout

当我运行“分子收敛”时,出现错误

TASK [ansible : copy manifest billing] *****************************************
fatal: [instance]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'item' is undefined\n\nThe error appears to be in '/home/user/ansible/tasks/main.yml': line 2, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n---\n- name: copy manifest billing\n  ^ here\n"}
弗拉基米尔·博特卡

问:“错误是:'item'未定义”

答:缩进是错误的。with_items不是模块的参数。这是循环模块的指令。正确的语法是

---
- name: copy manifest billing
  template:
    src: templates/service.j2
    dest: "{{ item }}"
  with_items:
    "{{ services }}"

笔记

    with_items: "{{ services }}"
  • 随着Ansible 2.5的发布,建议执行循环的方法是使用new循环关键字而不是with_X样式循环
    loop: "{{ services }}"
  • 命令结果不一致ansible-playbook playbook.yml --check该命令不会抱怨"Invalid options for template: with_items"但是使用模块debug检查可以按预期进行
    - debug:
        var: item
        with_items:
          "{{ services }}"
fatal: [localhost]: FAILED! => {"msg": "Invalid options for debug: with_items"}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章