在 YAML 中,可以通过两种方式将字符串分行显示:使用“|”(Literal Block Scalar)和“>”(Folded Block Scalar)操作符。这两种方式在处理多行文本时的行为有所不同。
1. “|” - Literal Block Scalar
使用“|”操作符时,YAML 会保留字符串中的换行符和空格,将多行文本原样输出。
示例代码:
variable1: |
exactly as you see
will appear these three
lines of poetry
输出结果:
variable1: "exactly as you see\nwill appear these three\nlines of poetry\n"
2. “>” - Folded Block Scalar
使用“>”操作符时,YAML 会将多行文本折叠成单行,将换行符替换为空格。
示例代码:
variable2: >
this is really a
single line of text
despite appearances
输出结果:
variable2: "this is really a single line of text despite appearances\n"
如何去除字符串末尾的换行符
如果不想在字符串末尾保留换行符,可以在操作符后添加“-”(减号)。
示例代码:
variable1: |-
exactly as you see
will appear these three
lines of poetry
variable2: >-
this is really a
single line of text
despite appearances
输出结果:
variable1: "exactly as you see\nwill appear these three\nlines of poetry"
variable2: "this is really a single line of text despite appearances"
示例:在 Ansible Playbook 中使用多行字符串
示例代码
---
- name: debug module demo
hosts: all
vars:
variable1: |
exactly as you see
will appear these three
lines of poetry
variable2: >
this is really a
single line of text
despite appearances
tasks:
- name: print variable1
ansible.builtin.debug:
var: variable1
- name: print variable2
ansible.builtin.debug:
var: variable2
执行结果
$ ansible-playbook -i virtualmachines/demo/inventory print\ text\ variable\ during\ execution/multi-line.yml
PLAY [debug module demo] **********************************
TASK [Gathering Facts] ************************************
ok: [demo.example.com]
TASK [print variable1] ************************************
ok: [demo.example.com] => {
"variable1": "exactly as you see\nwill appear these three\nlines of poetry\n"
}
TASK [print variable2] ************************************
ok: [demo.example.com] => {
"variable2": "this is really a single line of text despite appearances\n"
}
PLAY RECAP *************************************************
demo.example.com : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
示例:去除字符串末尾的换行符
示例代码
---
- name: debug module demo
hosts: all
vars:
variable1: |-
exactly as you see
will appear these three
lines of poetry
variable2: >-
this is really a
single line of text
despite appearances
tasks:
- name: print variable1
ansible.builtin.debug:
var: variable1
- name: print variable2
ansible.builtin.debug:
var: variable2
执行结果
$ ansible-playbook -i virtualmachines/demo/inventory print\ text\ variable\ during\ execution/multi-line.yml
PLAY [debug module demo] **********************************
TASK [Gathering Facts] ************************************
ok: [demo.example.com]
TASK [print variable1] ************************************
ok: [demo.example.com] => {
"variable1": "exactly as you see\nwill appear these three\nlines of poetry"
}
TASK [print variable2] ************************************
ok: [demo.example.com] => {
"variable2": "this is really a single line of text despite appearances"
}
PLAY RECAP *************************************************
demo.example.com : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
示例:将多行字符串拆分为列表
示例代码
---
- name: debug module demo
hosts: all
vars:
variable1: |-
exactly as you see
will appear these three
lines of poetry
variable2: >-
this is really a
single line of text
despite appearances
tasks:
- name: print variable1
ansible.builtin.debug:
msg: "{{ variable1.split('\n') }}"
- name: print variable2
ansible.builtin.debug:
var: variable2
执行结果
$ ansible-playbook -i virtualmachines/demo/inventory print\ text\ variable\ during\ execution/multi-line.yml
PLAY [debug module demo] **********************************
TASK [Gathering Facts] ************************************
ok: [demo.example.com]
TASK [print variable1] ************************************
ok: [demo.example.com] => {
"msg": [
"exactly as you see",
"will appear these three",
"lines of poetry"
]
}
TASK [print variable2] ************************************
ok: [demo.example.com] => {
"variable2": "this is really a single line of text despite appearances"
}
PLAY RECAP *************************************************
demo.example.com : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
总结
- 使用“|”操作符时,多行文本将保留换行符和空格,原样输出。
- 使用“>”操作符时,多行文本将折叠成单行,换行符被替换为空格。
- 如果不想保留末尾的换行符,可以在操作符后添加“-”。
- 可以通过
.split('\n')方法将多行字符串拆分为列表,方便进一步处理。