8

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

dejanualex
  • 185
  • 1
  • 1
  • 7

2 Answers2

8

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

kenlukas
  • 3,101
  • 2
  • 16
  • 26
  • 3
    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`. – Valentin Bajrami Aug 21 '21 at 15:49
0

Remove the spaces around the equal sign "="

debug: msg="{{command_output.stdout}}"
Vladimir Botka
  • 5,138
  • 8
  • 20