【问题标题】:Unable to add string between variables in Ansible lineinfile module无法在 Ansible lineinfile 模块中的变量之间添加字符串
【发布时间】:2019-12-28 14:38:41
【问题描述】:

我希望我的filedet.yaml 看起来像

10.9.75.78:/app/tmp/tmp.log、/vars/tmp/test.out 10.9.55.74:/app/tmp/tmp1.log、/vars/tmp/admin.out

以下工作正常并正确记录数据,但是当我添加“:”时,语法中断并出现错误

 - name: Logging the deployment's file details to a Ansible variable file
     local_action: lineinfile line={{ inventory_hostname }}': '{{ vars['fdetails_' +  Layer].results|map(attribute='stdout')|list }} path={{ playbook_dir }}/vars/filedets.yaml

输出错误:

违规行似乎是: local_action: lineinfile line={{ inventory_hostname }}': '{{ > vars['fdetails_' + Layer].results|map(attribute='stdout')|list > }} path={{ playbook_dir }}/vars/文件集.yaml ^ 这里 我们可能是错的,但这个看起来可能是一个问题 缺少引号。总是引用模板表达式括号 开始一个值。例如:

我也试过这段代码,但它也因语法错误而失败:

line="{{ inventory_hostname }}': '{{ vars['fdetails_' +  Layer].results|map(attribute='stdout')|list }}" path="{{ playbook_dir }}/vars/filedets.yaml"

您能否建议我如何在行中的变量之间注入冒号和空格':'?

【问题讨论】:

标签: ansible command output string-formatting


【解决方案1】:

只需将要插入的字符串包装在{{}}中的变量之间

line="{{ inventory_hostname }}{{': '}}{{ vars['fdetails_' +  Layer].results|map(attribute='stdout')|list }}" path="{{ playbook_dir }}/vars/filedets.yaml"

如果: 冒号是个问题,您可以使用以下方法来屏蔽它:

line="{{ inventory_hostname }}{{'%c '%58}}{{ vars['fdetails_' +  Layer].results|map(attribute='stdout')|list }}" path="{{ playbook_dir }}/vars/filedets.yaml"

58 是 : 的 ASCII 码。

【讨论】:

  • 仍然遇到相同的语法错误。我在最新的 Ansible 和 python 2.7.5
  • 问题似乎是由于冒号:如果我用任何字母替换冒号,那么它就可以工作。你能建议吗?
  • 你能用替代的 yml 语法试一试吗? ``` local_action: lineinfile: line: "{{ inventory_hostname }}{{': '}}{{ vars['fdetails_' + Layer].results|map(attribute='stdout')|list }}" 路径: "{{ playbook_dir }}/vars/filedets.yaml" ```
猜你喜欢
  • 2020-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多