Crontab to Compress and Remove Old Messages Log with Ansible and GIT

Steps to be followed if we use GIT as our Version Control System with ansible repo. Assuming that my ansible repo is named as “ansible”.
1. Create local repo in github by clicking “Fork” in the master repo.
2. Clone local repo to your local directory:
    git clone git@github.example.com:sree/ansible.git
3. In my local repo add the new yml for the respective play.
#cd ansible
#mkdir roles/crontab/tasks/messages-cron.yml
4. Add the below Ansible play entries to compress files that are more than 7 days old and delete the compressed logs if they are 28 days old
– name: Cron job to compress old messages log
cron:
name: “Compress old messages log”
hour: “2”
minute: 0
user: root
state: present
job: ‘find /var/log/ -name messages-* ! -name “*.gz” -daystart -mtime +7 -exec gzip {} \; > /dev/null 2>&1’
– name: Cron job to remove old messages log
cron:
name: “Remove old messages log”
hour: “2”
minute: 0
user: root
state: present
job: ‘find /var/log/ -name “messages-*.gz” -daystart -mtime +28 -exec rm {} \; > /dev/null 2>&1’
5. Go to the main.yml file and include the newly added yml file to to the main yml file
#cd roles/crontab/tasks/
# vi main.yml
– name: Include Messages Cron
include: messages-cron.yml
tags:
– compress_messages_log
– remove_messages_log   
6. Commit the changes to the master
#git add roles/crontab/tasks/messages-cron.yml
#git status
#git diff
#git commit -a -m “Adding Crontab to compress and remove old messages log”
7. Go to Github website

a. click on pull request

b. click on commit to master
8. Once the master is updated with the new changes, push the changes across the servers using ansible
eg: #ansible -i <inventory> all crontab.yml –tags compress_messages_log,remove_messages_log 

Leave a comment