【问题标题】:How do I use {{ item }} in ansible?如何在 ansible 中使用 {{ item }}?
【发布时间】:2020-06-11 18:40:04
【问题描述】:

我正在尝试从 ansible 文档复制第一个 with_item 示例,但我遇到了这个错误:

fatal: [user@my-vm]: FAILED! => {"msg": "The task includes an option with an undefined variable.
The error was: 'item' is undefined\n\nThe error appears to be in 'path/ansible/playbooks/android-dev.yml': line 4,
column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\n
The offending line appears to be:\n\n  tasks:\n    - name: Debugging with items\n      ^ here\n"}

我的代码是:

- hosts: test
  tasks:
    - name: Debugging with items
      debug:
        msg: "An item: {{ item }}"
        with_items:
          - 1
          - 2
          - 3

我正在按照此页面的第一个示例来构建任务:https://docs.ansible.com/ansible/latest/plugins/lookup/items.html

我现在已经开始使用ansible了,这可能是一个很容易解决的问题,但是我真的在网上找不到任何解决方案

【问题讨论】:

  • 您的 with_items 需要处于调试状态,而不是带有 YAML 位置问题的 msg

标签: ansible


【解决方案1】:

缩进错误。修复它

- hosts: test
  tasks:
    - name: Debugging with items
      debug:
        msg: "An item: {{ item }}"
      loop:
        - 1
        - 2
        - 3

使用loop 代替with_items

【讨论】:

  • 请在字符串两边加上引号
  • 你在说哪些字符串?
  • 至少name 但我也认为hosts
  • 为什么要引用?是否有任何参考资料可以支持您的推理?顺便提一句。示例中 msg 的扩展也不必引用。
  • 因为它是避免 YAML 某些问题的廉价方法。参见官方文档docs.ansible.com/ansible/latest/reference_appendices/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多