Ansible: `Cat` File – Print/Read File Content

The cat command in Linux is used to print a file’s content to a standard output.

Ansible can also be used to connect to a remote Linux or Windows host and print the content of a remote file or save it to a variable.

This note shows the examples of how to “cat” a file using the Ansible’s shell, win_shell and slurp modules.

Cool Tip: Create an empty file or a file with a content using Ansible! Read more →

“Cat” a File Using Ansible

Linux Target

In the examples below I read a content of a /etc/resolv.conf file on a remote Linux host using the Ansible’s shell module and print it to a console, then save the content of the file to a {{ resolve_conf }} variable and also print it:

- name: "Read a file content"
  shell: |
    cat /etc/resolv.conf
  register: file_content

- name: "Print the file content to a console"
  debug:
    msg: "{{ file_content.stdout }}"

- name: "Save the file content to a '$resolve_conf' variable"
  set_fact:
    resolve_conf: "{{ file_content.stdout }}"

- name: "Print the '$resolve_conf' variable"
  debug:
    msg: "{{ resolve_conf }}"

Sample output:

TASK [telegram-desktop : Read a file content] ****************************************
changed: [127.0.0.1]

TASK [telegram-desktop : Print the file content to a console] ************************
ok: [127.0.0.1] => 
  msg: |-
    # This file is managed by man:systemd-resolved(8). Do not edit.

    nameserver 127.0.0.53
    nameserver 8.8.8.8
    nameserver 1.1.1.1

TASK [telegram-desktop : Save the file content to a '$resolve_conf' variable] ********
ok: [127.0.0.1]

TASK [telegram-desktop : Print the '$resolve_conf' variable] *************************
ok: [127.0.0.1] => 
  msg: |-
    # This file is managed by man:systemd-resolved(8). Do not edit.
  
    nameserver 127.0.0.53
    nameserver 8.8.8.8
    nameserver 1.1.1.1

The outputs of the other examples are quite similar, so I won’t paste them here.

Cool Tip: Windows cat command equivalent in CMD and PowerShell! Read more →

Windows Target

On a Windows target you can “cat” a file using the Ansible’s win_shell module and the PowerShell’s Get-Content command that is an equivalent of the cat command in Linux:

- name: "Read a file content"
  win_shell: |
    Get-Content "C:\Windows\System32\drivers\etc\hosts"
  register: file_content

- name: "Print the file content to a console"
  debug:
    msg: "{{ file_content.stdout }}"

- name: "Save the file content to a '$hosts_file' variable"
  set_fact:
    hosts_file: "{{ file_content.stdout }}"

- name: "Print the '$hosts_file' variable"
  debug:
    msg: "{{ hosts_file }}"

Slurp Module – Works on Linux & Windows

You can also read and print a file’s content both on remote Linux and Windows targets using the Ansible’s slurp module (used for fetching a base64-encoded blob containing the data in a remote file):

- name: "Read a file data"
  slurp:
    src: /etc/resolv.conf
  register: file_data

- name: "Print the file content to a console"
  debug:
    msg: "{{ file_data.content | b64decode}}"

- name: "Save the file content to a '$resolve_conf' variable"
  set_fact:
    resolve_conf: "{{ file_data.content | b64decode }}"

- name: "Print the '$resolve_conf' variable"
  debug:
    msg: "{{ resolve_conf }}"

Cool Tip: How to run a shell command on a remote host using Ansible! Read more →

Leave a Reply