10

I'm using ansible 2.9.3 and I'm having trouble trying to display the content of a file from the target machine, this is my playbook :

 - name: Display content of resolv.conf hosts: jenkins tasks: - name: Display resolv.conf contents command: cat resolv.conf chdir=/etc register: command_output - name: Print to console debug: msg = "{{command_output.stdout}}" 

And my task Print to console returns:

TASK [Print to console] ************************************************************************************************************************************************************************************ ok: [jenkins] => { "msg": "Hello world!" } 

I wanted to have the content of the file to stdout, what am I missing ? Thx

3 Answers 3

11

Putting the msg on a separate line than debug like this and use a : instead of an = :

- name: Display content of resolv.conf hosts: localhost tasks: - name: Display resolv.conf contents command: cat resolv.conf chdir=/etc register: command_output - name: Print to console debug: msg: "{{command_output.stdout}}" 

This was my output:

TASK [Display resolv.conf contents] ****************************************************************************************************************************** changed: [127.0.0.1]

TASK [Print to console] ****************************************************************************************************************************************** ok: [127.0.0.1] => { "msg": "#\n# macOS Notice\n#\n# This file is not consulted for DNS hostname resolution, address\n# resolution, or the DNS query routing mechanism used by most\n# processes on this system.\n#\n# To view the DNS configuration used by this system, use:\n# scutil --dns\n#\n# SEE ALSO\n# dns-sd(1), scutil(8)\n#\n# This file is automatically generated.\n#\ndomain attlocal.net\nnameserver 192.168.1.254\nnameserver 8.8.8.8\nnameserver 8.8.4.4" }

1
  • 5
    A great answer. Maybe it is an idea to add {{ command_output.stdout_lines }} so the new lines are preserved and not printed as \n. Commented Aug 21, 2021 at 15:49
1

You can use ansible.builtin.file lookup plugin to print content of the file, e.g.

- ansible.builtin.debug: var: lookup('ansible.builtin.file', '/etc/resolv.conf') 

However by default, lookup plugins are always executes on the controller (localhost) (GH-78845), so you've to copy the file first (e.g. using copy module first).

See docs: https://docs.ansible.com/ansible/latest/collections/ansible/builtin/file_lookup.html.

0

A: Remove the spaces around the equal sign "="

debug: msg="{{command_output.stdout}}" 

Note: Today I found out that somebody downvoted this answer. Please, find the below update that proves this simple answer is correct.


Update

Given the file for testing

shell> cat /tmp/ansible/resolv.conf nameserver 127.0.0.53 options edns0 trust-ad search example.com 

read the file and display stdout

 - command: cmd: cat resolv.conf chdir: /tmp/ansible register: out - debug: var: out.stdout 

gives

 out.stdout: |- nameserver 127.0.0.53 options edns0 trust-ad search example.com 

You can use the parameter msg

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

gives the same result

 msg: |- nameserver 127.0.0.53 options edns0 trust-ad search example.com 

You can provide the module debug with the parameter in an INI format. The task below gives the same result

 - debug: msg="{{ out.stdout }}" 

However, if you put spaces around the equal sign " = "

 - debug: msg = "{{ out.stdout }}" 

the task will fail (in Ansible 2.16)

fatal: [localhost]: FAILED! => msg: |- invalid or malformed argument: 'msg = "nameserver 127.0.0.53 options edns0 trust-ad search example.com"' 

Example of a complete playbook for testing

- hosts: localhost tasks: - command: cmd: cat resolv.conf chdir: /tmp/ansible register: out - debug: var: out.stdout - debug: msg: "{{ out.stdout }}" - debug: msg="{{ out.stdout }}" - debug: msg = "{{ out.stdout }}" 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.