无法在 Ansible lineinfile 模块中的变量之间添加字符串

Unable to add string between variables in Ansible lineinfile module

我希望我的 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

输出错误:

The offending line appears to be: local_action: lineinfile line={{ inventory_hostname }}': '{{ > vars['fdetails_' + Layer].results|map(attribute='stdout')|list > }} path={{ playbook_dir }}/vars/filedets.yaml ^ here We could be wrong, but this one looks like it might be an issue with missing quotes. Always quote template expression brackets when they start a value. For instance:

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

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

你能建议我如何在行中的变量之间插入冒号和 space ':' 吗?[​​=13=]

只需将要插入的字符串包裹在 {{ }}

中的变量之间
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码。