Ansible Display File Content – How to Display File Content to Stdout in Ansible

ansibleansible-playbook

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

Best Answer

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