Ansible stdout 行结果没有括号和引号

Ansible stdout lines result without brackets and quotes

我如何 return 从 playbook stdout_lines 中去掉括号和引号?

我读到的任何有关此的信息都不起作用,

这是我的代码:

  - name: Tested unit information
    command: cat "{{ location }}/info.txt"
    register: sys_info
  - debug: 
      msg: "{{ sys_info.stdout_lines }}"

这是我的输出:

    "msg": [
        "Serial Number: Test12345",
        "Technician Name: Team",
        "Custom Serial Number: Test12345",
        "System Part Number: xxxx",
        "Rev: A10",
        "Last Rev: A10",
        "Paka Number: Auto",
        "Ip Address: 1.1.1.1"
    ]
}

这是我想要得到的:

msg:
    Serial Number: Test12345
    Technician Name: Team
    Custom Serial Number: Test12345
    System Part Number: xxxx
    Rev: A10
    Last Rev: A10
    Paka Number: Auto
    Ip Address: 1.1.1.1

如有任何有关如何执行此操作的信息或建议,我将不胜感激

简答

使用stdout代替stdout_lines并使用yaml回调代替默认。那么

    - debug:
        msg: "{{ sys_info.stdout }}"

给你想要的

  msg: |-
    Serial Number: Test12345
    Technician Name: Team
    Custom Serial Number: Test12345
    System Part Number: xxxx
    Rev: A10
    Last Rev: A10
    Paka Number: Auto
    Ip Address: 1.1.1.1

详情


给定文件

shell> cat info.txt
Serial Number: Test12345
Technician Name: Team
Custom Serial Number: Test12345
System Part Number: xxxx
Rev: A10
Last Rev: A10
Paka Number: Auto
Ip Address: 1.1.1.1

和剧本

shell> cat test.yml
- hosts: localhost
  tasks:
    - name: Tested unit information
      command: cat info.txt
      register: sys_info
    - debug:
        msg: "{{ sys_info.stdout_lines }}"

您看到的内容取决于 DEFAULT_STDOUT_CALLBACK默认回调格式为JSON

shell> ANSIBLE_STDOUT_CALLBACK=default ansible-playbook test.yml

TASK [debug] ***************************************************************
ok: [localhost] => {
    "msg": [
        "Serial Number: Test12345",
        "Technician Name: Team",
        "Custom Serial Number: Test12345",
        "System Part Number: xxxx",
        "Rev: A10",
        "Last Rev: A10",
        "Paka Number: Auto",
        "Ip Address: 1.1.1.1"
    ]
}

如果您想从列表中创建纯文本,请使用 Jinja,例如

    - debug:
        msg: |-
          {% for line in sys_info.stdout_lines %}
          {{ line }}
          {% endfor %}

,或者干脆 加入

    - debug:
        msg: "{{ sys_info.stdout_lines|join('\n') }}"

两个选项给出相同的结果

shell> ANSIBLE_STDOUT_CALLBACK=default ansible-playbook test.yml

TASK [debug] ***************************************************************
ok: [localhost] => {
    "msg": "Serial Number: Test12345\nTechnician Name: Team\nCustom Serial Number: Test12345\nSystem Part Number: xxxx\nRev: A10\nLast Rev: A10\nPaka Number: Auto\nIp Address: 1.1.1.1\n"
}

使用回调yaml得到你想要的

shell> ANSIBLE_STDOUT_CALLBACK=yaml ansible-playbook test.yml

TASK [debug] ***************************************************************
ok: [localhost] => 
  msg: |-
    Serial Number: Test12345
    Technician Name: Team
    Custom Serial Number: Test12345
    System Part Number: xxxx
    Rev: A10
    Last Rev: A10
    Paka Number: Auto
    Ip Address: 1.1.1.1