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

3 Answers 3

9

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
  • 4
    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. 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

Remove the spaces around the equal sign "="

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .