-1

I am trying to fetch the content of foo.txt as it is however when I am doing a lookup the new lines are getting replaced with \n.

How to avoid this and get the content of file as it is?

foo.txt:

This is dummy1
This is dummy2
This is dummy3
This is dummy4
This is dummy5

Playbook:

- name: Gather info on backend servers
  hosts: localhost
  gather_facts: no
  tasks:
    - ansible.builtin.debug:
        msg: "{{ lookup('ansible.builtin.file', '/tmp/foo.txt') }}"

Output:

TASK [ansible.builtin.debug] *******************************************************************************************************************************
ok: [localhost] => {
    "msg": "This is dummy1\nThis is dummy2\nThis is dummy3\nThis is dummy4\nThis is dummy5"
}

I want Ansible to print as below:

TASK [ansible.builtin.debug] *******************************************************************************************************************************
ok: [localhost] => {
    "msg": "This is dummy1
    This is dummy2
    This is dummy3
    This is dummy4
    This is dummy5"

}
U880D
  • 8,601
  • 6
  • 24
  • 40
The Noob
  • 3
  • 2

1 Answers1

-1

To my knowledge, that is currently not possible, though there are some suggestions on how to work around this in this thread on serverfault.

The imho easiest solution would be this:

    - ansible.builtin.debug:
        msg: "{{ lookup('ansible.builtin.file', '/tmp/foo.txt').split('\n') }}"
toydarian
  • 4,246
  • 5
  • 23
  • 35
  • The mentioned thread [contains also the correct solution](https://serverfault.com/a/683275/448950) to address the initial problem. – U880D Mar 22 '23 at 08:05
  • Thanks @toydarian... this is close to what i want to achieve. TASK [ansible.builtin.debug] ******************************************************************************************************************************* ok: [localhost] => { "msg": [ "This is dummy1", "This is dummy2", "This is dummy3", "This is dummy4", "This is dummy5" ] } I don't want the quotes as well. – The Noob Mar 22 '23 at 08:11
  • 1
    The output is JSON formatted, as mentioned in the other answer. If you don't want that, you need to use a different callback plugin, as suggested there. – toydarian Mar 22 '23 at 08:24