Ansible yaml重排导致错误

Ansible yaml rearrangement causes error

my_tags 事实赋值在第二个赋值中起作用。列出的第一个赋值导致失败,但它只是一个简单的 rear运行gement。是的,当我 运行 时,我 commented/uncommented 是正确的。

代码如下:

- name: Set optional tag
  when: machine_type.find('substr') != -1
  set_fact:
    # vvv some quoting error? vvv
    my_tags: {{ my_tags | default('') }}, sbc_type:{{ direction }},
    # vvv works just fine vvv
    #my_tags: sbc_type:{{ direction }}, {{ my_tags | default('') }}

这里是错误:

    my_tags: {{ my_tags | default('') }}, sbc_type:{{ direction }},
                                        ^
We could be wrong, but this one looks like it might be an issue with
missing quotes.

当我从我的终端复制粘贴到此处时,克拉号 (^) 指向 'direction' 变量(如果有任何线索的话),但我没有看到任何制表符被使用。

为什么后运行gement会导致这个错误?

这在 Hey Wait, A YAML gotcha 中有记录:

YAML syntax requires that if you start a value with {{ foo }} you quote the whole line, since it wants to be sure you aren’t trying to start a YAML dictionary.

所以你应该这样写:

my_tags: "{{ my_tags | default('') }}, sbc_type:{{ direction }},"