L’automatisation est un idéal en IT et DevOps, mais encore faut-il qu’elle soit facile à mettre en place. Plusieurs fois, j’ai écrit un script pour une tâche sans jamais l’automatiser, faute d’infrastructure simple.
Mon outil préféré était cron, fiable et simple, mais il suppose que la machine est allumée 24 h/24. Avec anacron, si l’ordinateur est éteint au moment prévu, la tâche s’exécute au prochain démarrage. Il suffit de déposer un script dans cron.daily, cron.weekly ou cron.monthly.
Configurer avec Ansible
À chaque nouvelle machine, j’installe anacron et je configure l’environnement utilisateur avec un playbook Ansible.
--- - hosts: localhost tasks:
Créer les dossiers
- name: create directory tree
ansible.builtin.file:
path: "{{ item }}"
state: directory
with_items:
- '~/.local/etc/cron.daily'
- '~/.local/etc/cron.weekly'
- '~/.local/etc/cron.monthly'
- '~/.var/spool/anacron'Copier anacrontab
- name: copy anacrontab into place
ansible.builtin.copy:
src: ~/Ansible/data/anacrontab
dest: ~/.local/etc/anacrontab
mode: '0755'Le fichier anacrontab ressemble à :
SHELL=/bin/sh PATH=/sbin:/bin:/usr/sbin:/usr/bin 1 0 cron.day run-parts $HOME/.local/etc/cron.daily/ 7 0 cron.wek run-parts $HOME/.local/etc/cron.weekly/ 30 0 cron.mon run-parts $HOME/.local/etc/cron.monthly/
Lancer anacron à la connexion
- name: add local anacrontab to .profile
ansible.builtin.lineinfile:
path: ~/.profile
regexp: '^/usr/sbin/anacron'
line: '/usr/sbin/anacron -t ~/.local/etc/anacrontab'
create: trueInstaller anacron
- hosts: localhost
become: true
tasks:
- name: install anacron
ansible.builtin.package:
name: anacron
state: presentEnsuite, déposez vos scripts dans ~/.local/etc/cron.daily et laissez anacron les exécuter.