gpt4 book ai didi

Ansible:如何修复 to_nice_yaml 输出引用和换行符?

转载 作者:行者123 更新时间:2023-12-04 12:23:16 25 4
gpt4 key购买 nike

我有这个 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的值(value)报价。
我不明白 weirdest 的换行符的值(value)。
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) }}

来源: https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#formatting-data-yaml-and-json

然后,在文档中的这个解释之后,他们还指出了一个事实:

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语法被称为 explicit typing在 YAML 中,并在链接的文档中进行了解释。

关于Ansible:如何修复 to_nice_yaml 输出引用和换行符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65139989/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com