![创建值中包含双引号的文件](https://linux22.com/image/793908/%E5%88%9B%E5%BB%BA%E5%80%BC%E4%B8%AD%E5%8C%85%E5%90%AB%E5%8F%8C%E5%BC%95%E5%8F%B7%E7%9A%84%E6%96%87%E4%BB%B6.png)
我需要创建一个在值中使用双引号的文件。
剧本:
- hosts: all
vars:
test_var:
- hostname: "hostname"
hardware:
- disk: "HDD 50G"
ram: "5G"
cpu: "4 Cores"
users:
- user: "user"
passwd: "sdf%^#("
email: "[email protected]"
groups: "group1, group2, group3"
tasks:
- name: Creating temporary file
ansible.builtin.copy:
content: |
{{ test_var | to_nice_yaml(indent=2) }}
dest: "./test_file"
delegate_to: localhost
我想要得到:
- hardware:
- cpu: "4 Cores"
disk: "HDD 50G"
ram: "5G"
hostname: "hostname"
users:
- email: "[email protected]"
groups: "group1, group2, group3"
passwd: "sdf%^#("
user: "user"
但我得到:
$ cat test_file
- hardware:
- cpu: 4 Cores
disk: HDD 50G
ram: 5G
hostname: hostname
users:
- email: [email protected]
groups: group1, group2, group3
passwd: sdf%^#(
user: user
如何添加引号?
答案1
过滤器to_nice_yaml
使用 PyYAML 和所有参数传递给dump()
函数,因此您可以使用其所有参数。
default_style='"'
看起来很有希望。
结果to_nice_yaml(indent=2, default_style='"')
:
- "hardware":
- "cpu": "4 Cores"
"disk": "HDD 50G"
"ram": "5G"
"hostname": "hostname"
"users":
- "email": "[email protected]"
"groups": "group1, group2, group3"
"passwd": "sdf%^#("
"user": "user"