创建值中包含双引号的文件

创建值中包含双引号的文件

我需要创建一个在值中使用双引号的文件。

剧本:

- 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"

相关内容