This guide covers fetching logs and files from remote machines using Ansible.
fetch to Retrieve Files- name: Collect system logs from remote servers
hosts: all
tasks:
- name: Fetch syslog from each server
fetch:
src: /var/log/syslog # Remote path
dest: ./collected_logs/ # Local directory (will create subdirs per host)
flat: no # Stores file under a directory named after the host
πΉ Key Points:
fetch module copies files from remote hosts to the control node.flat: no option creates per-host directories (collected_logs/hostname/syslog).synchronize for Bulk File Transfers- name: Synchronize log directory from remote to local
hosts: all
tasks:
- name: Sync logs directory
synchronize:
src: /var/log/nginx/ # Remote directory
dest: ./nginx_logs/ # Local directory
mode: pull # Transfers files from remote to local
πΉ Key Points:
synchronize uses rsync for efficient bulk transfer.mode: pull means fetching from remote β local.- name: Archive and download logs
hosts: all
tasks:
- name: Compress logs on remote server
archive:
path: /var/log/nginx/
dest: /tmp/nginx_logs.tar.gz
format: gz
- name: Fetch the compressed log file
fetch:
src: /tmp/nginx_logs.tar.gz
dest: ./collected_logs/
flat: yes
πΉ Key Points:
archive module compresses logs before transfer.fetch downloads the compressed file.β
Use fetch for single files.
β
Use synchronize for directories.
β
Use archive for efficient transfers.
π Happy Automating! π