在ansible剧本中包含多个变量和字符串

在ansible剧本中包含多个变量和字符串,ansible,concatenation,yaml,Ansible,Concatenation,Yaml,我在为目标部分执行带有_items的时正在尝试多重串联 现在看起来是这样的: - name: create app except+lookup copy: content="" dest="{{ dir.comp ~ '/config/con2dd/' ~ item.name ~ 'File.txt' }}" force=no group=devops owner=devops mode: 0755 with_items: ... 我得到: We could be wrong, but

我在为目标部分执行带有_items的
时正在尝试多重串联

现在看起来是这样的:

- name: create app except+lookup
  copy: content="" dest="{{ dir.comp ~ '/config/con2dd/' ~ item.name ~ 'File.txt' }}" force=no group=devops owner=devops mode: 0755
  with_items:
...
我得到:

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:            

    with_items:
      - {{ foo }}

Should be written as:

    with_items:
      - "{{ foo }}"
尝试了两种方法,但都没有成功


可以用字符串表示变量吗?

您没有引用与键
copy
关联的值。为此,第一个字符必须是双引号(或单引号)。反馈中给出的示例正确地做到了这一点,但没有明确说明。一旦标量以非引号开始(您的以
c
from
content
开始),标量中出现的引号将不再具有特殊意义

由于Ansible使用的解析器中存在一个错误,该标量(
模式:0755
)中的
(冒号空格)会导致问题,您应该对整个标量进行双引号引,并对其中出现的双引号进行转义:

copy: "content=\"\" dest=\"{{ dir.comp ~ '/config/con2dd/' ~ item.name ~ 'File.txt' }}\" force=no group=devops owner=devops mode: 0755"
或者使用单引号(具有不同的转义规则:

copy: 'content="" dest="{{ dir.comp ~ ''/config/con2dd/'' ~ item.name ~ ''File.txt'' }}" force=no group=devops owner=devops mode: 0755'
您可以自己在在线YAML解析器上测试标量,这与Ansible无法正确解析YAML的原因相同


解析器,正确处理标量中的:
,并且不会对输入产生错误(但它还有其他问题)。

不要将纯YAML和key=value语法混合用于参数。对于复杂参数,始终使用YAML语法:

- name: create app except+lookup
  copy:
    content: ""
    dest: "{{ dir.comp }}/config/con2dd/{{ item.name }}File.txt"
    force: no
    group: devops
    owner: devops
    mode: 0755
  with_items:
  ...

最后使用了这个强硬的策略,两个都是正确的。谢谢:)