Ansible:如何修复 to_nice_yaml 输出引号和换行符?
Ansible: How to fix to_nice_yaml output quotation and line-breaks?
我有这个 YAML 文件(我将我的问题提炼到最低限度):
scalar: simple_value
empty:
list:
- 1
- 2
- 3
complex:
- first:
one: 1
two: 2
- second:
one: 3
two: 4
weird: "{{ '{{' }} something {{ '}}' }}"
weirder: "{{ '{{' }} 'TTT' if something == 'blah' else 'FFF' {{ '}}' }}"
weirdest: "&lcub2; ansible_date_time.year &rcub2;.&lcub2; ansible_date_time.month &rcub2;.&lcub2; ansible_date_time.day &rcub2;"
和这本剧本:
---
- hosts: localhost
tasks:
- name: Load
include_vars:
file: ./vars.yml
name: object
- name: Write
copy:
content: "{{ object | to_nice_yaml(indent=2) }}"
dest: ./outv.yml
输出文件是这样的:
complex:
- first:
one: 1
two: 2
- second:
one: 3
two: 4
empty: null
list:
- 1
- 2
- 3
scalar: simple_value
weird: '{{ something }}'
weirder: '{{ ''TTT'' if something == ''blah'' else ''FFF'' }}'
weirdest: '&lcub2; ansible_date_time.year &rcub2;.&lcub2; ansible_date_time.month
&rcub2;.&lcub2; ansible_date_time.day &rcub2;'
虽然我认为输出和输入列表缩进都是正确和等效的,并且 Jinja escaping 处理得当,但我不确定 weirder
的价值引用。
而且我不明白 weirdest
值的换行符。
YAMLint 说没问题但实际上恢复了“正常”引号并在语法检查期间重新加入换行符。
有没有办法在过滤器 to_nice_yaml
(或任何其他过滤器)中强制使用双引号?
有没有办法避免换行(或者可能有原因)?
关于您在 weirdest
中观察到的换行符,这在文档中有解释:
The to_yaml
and to_nice_yaml
filters use the PyYAML library which has a default 80 symbol string length limit. That causes unexpected line break after 80th symbol (if there is a space after 80th symbol) To avoid such behaviour and generate long lines, use the width
option. You must use a hardcoded number to define the width, instead of a construction like float("inf")
, because the filter does not support proxying Python functions.
For example:
{{ some_variable | to_yaml(indent=8, width=1337) }}
{{ some_variable | to_nice_yaml(indent=8, width=1337) }}
然后,在文档中的这个解释之后,他们还指出了一个事实:
The filter does support passing through other YAML parameters. For a full list, see the PyYAML documentation.
所以从那里得到双引号字符串:default_style='"'
可以找到更多详细信息 here。
因此,剧本:
- hosts: all
gather_facts: no
tasks:
- copy:
content: "{{ object | to_nice_yaml(indent=2, width=1337, default_style='\"') }}"
dest: ./outv.yml
vars:
object:
scalar: simple_value
empty:
list:
- 1
- 2
- 3
complex:
- first:
one: 1
two: 2
- second:
one: 3
two: 4
weird: "{{ '{{' }} something {{ '}}' }}"
weirder: "{{ '{{' }} 'TTT' if something == 'blah' else 'FFF' {{ '}}' }}"
weirdest: "&lcub2; ansible_date_time.year &rcub2;.&lcub2; ansible_date_time.month &rcub2;.&lcub2; ansible_date_time.day &rcub2;"
给出文件 outv.yml 包含:
"complex":
- "first":
"one": !!int "1"
"two": !!int "2"
- "second":
"one": !!int "3"
"two": !!int "4"
"empty": !!null "null"
"list":
- !!int "1"
- !!int "2"
- !!int "3"
"scalar": "simple_value"
"weird": "{{ something }}"
"weirder": "{{ 'TTT' if something == 'blah' else 'FFF' }}"
"weirdest": "&lcub2; ansible_date_time.year &rcub2;.&lcub2; ansible_date_time.month &rcub2;.&lcub2; ansible_date_time.day &rcub2;"
请注意,!!int
和 !!null
语法在 YAML 中称为 explicit typing,并在链接文档中进行了解释。
我有这个 YAML 文件(我将我的问题提炼到最低限度):
scalar: simple_value
empty:
list:
- 1
- 2
- 3
complex:
- first:
one: 1
two: 2
- second:
one: 3
two: 4
weird: "{{ '{{' }} something {{ '}}' }}"
weirder: "{{ '{{' }} 'TTT' if something == 'blah' else 'FFF' {{ '}}' }}"
weirdest: "&lcub2; ansible_date_time.year &rcub2;.&lcub2; ansible_date_time.month &rcub2;.&lcub2; ansible_date_time.day &rcub2;"
和这本剧本:
---
- hosts: localhost
tasks:
- name: Load
include_vars:
file: ./vars.yml
name: object
- name: Write
copy:
content: "{{ object | to_nice_yaml(indent=2) }}"
dest: ./outv.yml
输出文件是这样的:
complex:
- first:
one: 1
two: 2
- second:
one: 3
two: 4
empty: null
list:
- 1
- 2
- 3
scalar: simple_value
weird: '{{ something }}'
weirder: '{{ ''TTT'' if something == ''blah'' else ''FFF'' }}'
weirdest: '&lcub2; ansible_date_time.year &rcub2;.&lcub2; ansible_date_time.month
&rcub2;.&lcub2; ansible_date_time.day &rcub2;'
虽然我认为输出和输入列表缩进都是正确和等效的,并且 Jinja escaping 处理得当,但我不确定 weirder
的价值引用。
而且我不明白 weirdest
值的换行符。
YAMLint 说没问题但实际上恢复了“正常”引号并在语法检查期间重新加入换行符。
有没有办法在过滤器 to_nice_yaml
(或任何其他过滤器)中强制使用双引号?
有没有办法避免换行(或者可能有原因)?
关于您在 weirdest
中观察到的换行符,这在文档中有解释:
The
to_yaml
andto_nice_yaml
filters use the PyYAML library which has a default 80 symbol string length limit. That causes unexpected line break after 80th symbol (if there is a space after 80th symbol) To avoid such behaviour and generate long lines, use thewidth
option. You must use a hardcoded number to define the width, instead of a construction likefloat("inf")
, because the filter does not support proxying Python functions.
For example:{{ some_variable | to_yaml(indent=8, width=1337) }} {{ some_variable | to_nice_yaml(indent=8, width=1337) }}
然后,在文档中的这个解释之后,他们还指出了一个事实:
The filter does support passing through other YAML parameters. For a full list, see the PyYAML documentation.
所以从那里得到双引号字符串:default_style='"'
可以找到更多详细信息 here。
因此,剧本:
- hosts: all
gather_facts: no
tasks:
- copy:
content: "{{ object | to_nice_yaml(indent=2, width=1337, default_style='\"') }}"
dest: ./outv.yml
vars:
object:
scalar: simple_value
empty:
list:
- 1
- 2
- 3
complex:
- first:
one: 1
two: 2
- second:
one: 3
two: 4
weird: "{{ '{{' }} something {{ '}}' }}"
weirder: "{{ '{{' }} 'TTT' if something == 'blah' else 'FFF' {{ '}}' }}"
weirdest: "&lcub2; ansible_date_time.year &rcub2;.&lcub2; ansible_date_time.month &rcub2;.&lcub2; ansible_date_time.day &rcub2;"
给出文件 outv.yml 包含:
"complex":
- "first":
"one": !!int "1"
"two": !!int "2"
- "second":
"one": !!int "3"
"two": !!int "4"
"empty": !!null "null"
"list":
- !!int "1"
- !!int "2"
- !!int "3"
"scalar": "simple_value"
"weird": "{{ something }}"
"weirder": "{{ 'TTT' if something == 'blah' else 'FFF' }}"
"weirdest": "&lcub2; ansible_date_time.year &rcub2;.&lcub2; ansible_date_time.month &rcub2;.&lcub2; ansible_date_time.day &rcub2;"
请注意,!!int
和 !!null
语法在 YAML 中称为 explicit typing,并在链接文档中进行了解释。