ansible - 多行模板 - Ansible 中的字符串转义

标签 ansible jinja2

我试图获得类似于本文的效果:https://gryzli.info/2017/12/21/ansible-debug-print-variables/

这意味着在我的情况下:

- name: Gather facts
  vars:
    msg: |
    "{{ansible_distribution}}"
    "{{ansible_distribution_major_version}}"
    "{{ansible_distribution_release}}"
    "{{ansible_distribution_version}}"

问题是如果没有引号,添加引号会引发错误。使用引号会引发相同的错误:
We could be wrong, but this one looks like it might be an issue with missing quotes. Always quote template expression brackets when theystart a value....

如何正确转义此多行字符串以便 Ansible 可以解析它?或者这种代码不再有效?

最佳答案

Q: "How do I correctly escape this multiline string so that Ansible can parse it?"



A:问题是块的缩进。引自 Example 8.3. Invalid Block Scalar Indentation Indicators

ERROR:

  • A leading all-space line must not have too many spaces.
  • A following text line must not be less indented.
  • The text is less indented than the indicated level.


正确的语法是(带或不带引号)
  vars:
    msg: |
      "{{ansible_distribution}}"
      "{{ansible_distribution_major_version}}"
      "{{ansible_distribution_release}}"
      "{{ansible_distribution_version}}"

关于ansible - 多行模板 - Ansible 中的字符串转义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58889303/

相关文章:

python - 为 flask 投票应用程序输入选项

python - 遍历 Django 模板中的一个列表

mongodb - 通过ansible playbook启用mongo身份验证

azure - 在azure vm上运行ansible时出现库错误

Ansible 用户模块 - 如何告诉它删除过期的用户帐户?

python - Jinja2 扩展模板时出现错误回溯

Django如何添加2个或更多过滤器

linux - 如何使用ansible在后台运行kafka代理启动脚本

dictionary - Ansible 从单独的文件填充字典值

python - 如何重定向到 FastAPI 端点内的动态 URL?