Search⌘ K
AI Features

Solution: Reading a File with Ansible

Understand how to create and execute an Ansible playbook that reads a file from a remote host using the slurp module, decodes its contents, and displays it for troubleshooting. Explore variable use, task definition, and playbook execution to verify file content retrieval.

We'll cover the following...
---
- name: Exercise
  hosts: all
  vars:
    myfile: "/proc/cpuinfo"
  tasks:
    - name: Get the content of the cpuinfo
      ansible.builtin.slurp:
        src: "{{ myfile }}"
      register: file_details

    - name: Display cpuinfo
      ansible.builtin.debug:
        msg: "{{ file_details.content | b64decode }}"
Code to read the cpuinfo file

Explanation

  • For the read_file.yml file:

    • Line 2: We write the play named Exercise.

    • Line 3: We specify the host all for the target hosts of execution.

    • Lines 4-5: We create a variable named myfile with the ...