Practical Use Cases of Ansible Stat
The stat
module in Ansible has a wide range of practical use cases, from simple file existence checks to more complex file management tasks. Here are a few examples:
Verifying File Existence and Properties
One of the most common use cases for the stat
module is to verify the existence and properties of a file or directory. This can be useful for ensuring that a required file or directory is present on the target hosts before executing other tasks. For example:
- name: Check if a configuration file exists
stat:
path: /etc/myapp/config.yml
register: config_file
- name: Fail if the configuration file is missing
fail:
msg: "The configuration file is missing!"
when: not config_file.stat.exists
In this example, the stat
module is used to check if the /etc/myapp/config.yml
file exists. If the file does not exist, the task will fail with an error message.
Comparing File Properties Across Hosts
The stat
module can also be used to compare the properties of a file or directory across different hosts. This can be useful for ensuring consistency in your infrastructure or for identifying differences between environments. For example:
- name: Get information about a log file
stat:
path: /var/log/myapp.log
register: log_file_info
delegate_to: app_server
- name: Get information about the same log file on another host
stat:
path: /var/log/myapp.log
register: log_file_info_backup
delegate_to: backup_server
- name: Compare log file properties
debug:
msg: >
Log file properties differ between app_server and backup_server.
app_server: {{ log_file_info.stat.size }} bytes, {{ log_file_info.stat.mtime }}
backup_server: {{ log_file_info_backup.stat.size }} bytes, {{ log_file_info_backup.stat.mtime }}
when: log_file_info.stat.size != log_file_info_backup.stat.size or
log_file_info.stat.mtime != log_file_info_backup.stat.mtime
In this example, the stat
module is used to gather information about a log file on two different hosts (app_server
and backup_server
). The gathered information is then compared, and a debug message is printed if the file properties differ.
Conditional Execution Based on File Properties
The stat
module can also be used to conditionally execute tasks based on the properties of a file or directory. This can be useful for automating various file management tasks, such as creating backups or triggering alerts. For example:
- name: Check if a log file is too large
stat:
path: /var/log/myapp.log
register: log_file_info
- name: Create a backup of the log file
copy:
src: /var/log/myapp.log
dest: /var/backups/myapp.log.{{ ansible_date_time.iso8601 }}
when: log_file_info.stat.size > 1024 * 1024 * 10 ## 10 MB
In this example, the stat
module is used to check the size of the /var/log/myapp.log
file. If the file size exceeds 10 MB, a backup of the file is created in the /var/backups
directory.
These are just a few examples of the practical use cases for the Ansible stat
module. By understanding how to effectively use this module, you can streamline your infrastructure management and automate a wide range of file-related tasks.