pondelok 7. februára 2022

ansible - migrating crontab

Every admin on the Earth loves God for discovering electricity and Cron. In this case we mean Cronos, god of times. How migrate cron jobs to other servers via ansible? Easy. 


You know this. You are administrator who has a lot of servers. In every server has monitoring tool, backup tool, audit tool, etc. Every tool has a task in cron for automation. What we know about crontab? 


1) Cron has service

- crond

- systemctl status crond 

2) crontab is tool for working of cron

crontab -l = list

crontab -e = edit

crontab -r = remove


-for good life > crontab -l > /root/backup_cron.txt 

(Check where is e and r on the keyboard!) 



3)  /var/spool/cron

- cron is located on /var/spool/cron

Migration of cron

scp /var/spool/cron to other servers 

Use ansible: 

- create playbook!

[root@localhost ~]# cat /etc/ansible/cron.yaml
---
- hosts: 10.0.2.5:10.0.2.6:10.0.2.7

  tasks:
  - name: migrating cron
    copy: src=/var/spool/cron/ dest=/var/spool/cron/  mode=600 owner=root group=root

[root@localhost ~]#



--- --> every yaml document start with ---

-hosts: destination (which servers) - when you need work with domains use /etc/hosts

-task 

-name: similar like echo: 

copy: src =source on starting server dest = hosts  mode, owner, group = chmod, chown, chgrp


Output of our migration 

[root@localhost ~]# ansible-playbook /etc/ansible/cron.yaml

PLAY [10.0.2.5:10.0.2.6:10.0.2.7] ***********************************************************************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************************************************************************************
ok: [10.0.2.7]
ok: [10.0.2.6]
ok: [10.0.2.5]

TASK [migrating cron] ***********************************************************************************************************************************************************************************************************************
changed: [10.0.2.5]
changed: [10.0.2.7]
changed: [10.0.2.6]

PLAY RECAP **********************************************************************************************************************************************************************************************************************************
10.0.2.5                   : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
10.0.2.6                   : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
10.0.2.7                   : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0



Check on the 10.0.2.7 server




2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether mi:lu:ju:ma:lu:ju brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.7/24 brd 10.0.2.255 scope global noprefixroute dynamic enp0s3
       valid_lft 586sec preferred_lft 586sec


[root@localhost ~]# crontab -l
40 23 * * 2 yum update -y

50 23 * * 2 tail -f /var/log/yum.log

59 23 * * * ifconfig > /root/monitoring_siete.txt

21 21 * * 1 crontab -l > /root/backup_cron.txt





Žiadne komentáre:

Zverejnenie komentára