Ansible stdout lines result without brackets and quotes

Viewed 404

How can I return from playbook stdout_lines without brackets and quotes?

Any information I read about this isn’t working ,

This is my code:

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

This is my output:

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

This what I want to get:

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

I would appreciate any information or suggestion on how to do this

1 Answers

Short answer

Use stdout instead of stdout_lines and use yaml callback instead of default. Then

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

gives what you want

  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

Details


Given the file

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

and the playbook

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

What you see depends on the DEFAULT_STDOUT_CALLBACK. The default callback format is 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"
    ]
}

If you want to create plain text from the list use Jinja, e.g.

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

, or simply join the lines

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

Both options give the same result

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

Use callback yaml to get what you want

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

Related