'Preserve format of the file while printing in Ansible

Below is my playbook to print the file.

I used couple of approaches but the file is not printed as is i.e. the new line formatting is gone when ansible prints the file contents.

  - name: List startup files
    shell: cat /tmp/test.txt
    register: readws

  - debug:
      msg: "/tmp/test.txt on {{ inventory_hostname }} is: {{ readws.stdout_lines }}"

  - debug:
      msg: "/tmp/test.txt on {{ inventory_hostname }} is: {{ lookup('file', '/tmp/test.txt') }}"

cat /tmp/test.txt 
i
m
good

Expected Ansible output:

TASK [debug] *****************************************************************************************
ok: [localhost] => {
    "msg": "/tmp/test.txt on localhost is: 
i
m
good
"
}

Ansible output:

TASK [List startup files] ******************************************************************
changed: [localhost]

TASK [debug] *****************************************************************************************
ok: [localhost] => {
    "msg": "/tmp/test.txt on localhost is: [u'i', u'm ', u'good']"
}

TASK [debug] *****************************************************************************************
ok: [localhost] => {
    "msg": "/tmp/test.txt on localhost is: i\nm \ngood"
}

Can you please suggest ?



Solution 1:[1]

This might help who might come looking for simpler way to display a file in ansible.

stdout_lines prints the file with reasonable formatting.


    - name: display the file needed
      shell: cat /tmp/test.txt
      register: test_file

    - debug:
        var: test_file.stdout_lines

Solution 2:[2]

Replace the parameters block with this:

"Parameters": {
    "QueueName.$": "$.transformInputs.sqsParameters.queueName",
    "Attributes": {"FifoQueue": true}
  },

(I ran into this today, the docs aren't good...)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 AndyNZ