Practical Applications
The Ansible Stat module can be used in a variety of practical applications to automate infrastructure management tasks. Here are a few examples:
Verifying File Existence and Permissions
One common use case for the Stat module is to verify the existence and permissions of a file or directory. This can be useful when you need to ensure that a critical file or directory is present and has the correct permissions before proceeding with other tasks. Here's an example:
- hosts: all
tasks:
- name: Check if a file exists
ansible.builtin.stat:
path: /etc/hosts
register: hosts_file
- name: Fail if the file does not exist
fail:
msg: "/etc/hosts file does not exist"
when: not hosts_file.stat.exists
- name: Ensure file permissions
file:
path: /etc/hosts
owner: root
group: root
mode: "0644"
when: hosts_file.stat.exists
This example first checks if the /etc/hosts
file exists using the Stat module. If the file does not exist, the playbook will fail with an error message. If the file exists, the playbook ensures that the file has the correct ownership and permissions.
Conditional Tasks Based on File Attributes
The Stat module can also be used to make decisions based on the attributes of a file or directory. For example, you can use the Stat module to check the modification time of a file and perform different actions based on whether the file has been updated. Here's an example:
- hosts: all
tasks:
- name: Get information about a configuration file
ansible.builtin.stat:
path: /etc/app/config.yml
register: config_file
- name: Update configuration file
template:
src: config.yml.j2
dest: /etc/app/config.yml
when: config_file.stat.mtime < (ansible_date_time.epoch | int - 3600)
In this example, the Stat module is used to retrieve the modification time of the /etc/app/config.yml
file. If the file has not been modified in the last hour (3600 seconds), the playbook updates the configuration file using a Jinja2 template.
Backup and Restore Operations
The Stat module can also be used to perform backup and restore operations. For example, you can use the Stat module to check the size of a file or directory before backing it up, and then use the same information to verify the integrity of the backup. Here's an example:
- hosts: all
tasks:
- name: Get information about a directory
ansible.builtin.stat:
path: /var/www/html
register: web_dir
- name: Backup web directory
archive:
path: /var/www/html
dest: /backups/web_dir.tar.gz
- name: Verify backup integrity
ansible.builtin.stat:
path: /backups/web_dir.tar.gz
register: backup_file
assert:
that:
- backup_file.stat.size == web_dir.stat.size
msg: "Backup file size does not match the original directory size"
In this example, the Stat module is used to retrieve the size of the /var/www/html
directory before creating a backup archive. After the backup is created, the Stat module is used again to verify the size of the backup file, ensuring that the backup process was successful.
By leveraging the Ansible Stat module in your playbooks, you can automate a wide range of infrastructure management tasks, improve the reliability of your systems, and streamline your overall workflow.