Ansible

Ansible Stat Module Usage

In Linux, the stat command is a command-line utility that provides detailed information about filesystems or files.

Ansible, considered the most diverse and popular automation tool, provides a module for fetching file and file system information as native Linux stat command.

In this guide, we will understand how to work with the stat module in Ansible playbooks.

Check if File Exists

The stat module will fetch information about a specified file or directory and save it using the register parameter.

In the following example playbook, we check if the file /var/log/alternatives.log exists.

---
- name: ansible stat module
  hosts: all
  become: yes
  tasks:
  - name: check alternatives.log
    stat:
      path: /var/log/alternatives.log
    register: info
  - name: tell if the file is there
    debug:
      msg: file exists
    when: info.stat.exists
  - name: tell if file missing
    debug:
      msg: the file missing
    when: not info.stat.exists

In the example above, we call the stat module to gather info about the file /var/log/alternatives.log from the remote host.

Once we retrieve the file information, we save it to a register file_info.

To ensure we can read the file, we set the become parameter to true.

In the second task, we use the info register to check if the file exists. If true, we display a message indicating the target file exists.

The final task returns a notification if the file does not exist on the remote host. This is facilitated if the info.stat.exist is false.

Save and run the playbook:

ansible-playbook play checkfile.yml

Here is an example output:

The output shows the target file exists on the remote host.

Check if a Directory Exists

The playbook to check if a directory exists using the stat module is similar to the one shown above. However, we provide a path to a target directory as shown below:

---
- name: ansible stat module
  hosts: all
  become: yes
  tasks:
  - name: check log directory
    stat:
      path: /var/log/
    register: dir_info
  - name: tell if directory exists
    debug:
      msg:  target directory exists
    when: dir_info.stat.exists
  - name: tell if dir is missing
    debug:
      msg: directory is missing
    when: not dir_info.stat.exists

Once we run the playbook, we should see an output similar to the one shown below:

ansible-playbook direxists.yml

Check if a user owns a file

The ansible stat module returns a collection of values for the specified file or directory. One such return variable is pw_name; this variable returns the username of the target file or directory owner.

We can create a playbook that returns a message if a specific username owns the specified file. For example:

---
- name: check file ownership
  hosts: all
  gather_facts: no
  become: yes
  tasks:
  - name: get file info
    stat:
      path: /var/log/kern.log
    register: file_info
  - name: owned by ubuntu usert?
    debug:
      msg:  file is owned by the ubuntu user
    when: file_info.stat.pw_name != 'ubuntu'
  - name: not owned by the ubuntu user?
    debug:
      msg: file is not owned by the ubuntu user
    when: not file_info.stat.pw_name != 'ubuntu'

In the example above, we check if the ubuntu user owns the file /var/log/kern.log. If true, we return an appropriate message.

Below is an example output:

Check file type

Another return value of the stat module allows us to check the file type. Using return values such as isreg and isdir, we can check if a file is a directory:

---
- name: check file type
  hosts: all
  become: ye
  tasks:
  - name: get file info
    stat:
      path: /var/log/kern.log
    register: file_info
  - name: regular file?
    debug:
      msg:  specified path is a regular file
    when: file_info.stat.isreg
  - name: is a directory?
    debug:
      msg: specified path is a directory
    when: file_info.stat.isdir

Save and run the playbook as:

Ansible stat return values

The following are the values returned by the ansible stat module:

  • attributes – Returns the attributes of the specified file.
  • executable – Returns true if the invoking user has executed permissions on the target path.
  • exists – Returns true if the specified path exists.
  • gr_name – Returns the name of the group of the file owner.
  • islbk – Returns true if the specified file is a block device
  • ischr – Returns true if the specified file is a character file.
  • isreg – Returns true if the specified file is a regular file
  • isdir – Returns true if the specified file is a directory.
  • islnk – Returns true if the target file is a link
  • mode – Returns the file permission in octal notation

Those are some return information of the ansible stat module. Check the documentation to learn more.

Conclusion

In this guide, we discussed how to use the ansible stat module to gather information about files and file systems.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list