Automating Tasks on FreeBSD with Cron and Ansible
Automation is a key aspect of modern system administration, improving efficiency, consistency, and reliability. FreeBSD offers powerful tools for automation, including cron for scheduling tasks and Ansible for configuration management and automation. This blog post explains how to use cron jobs for task automation and how to manage FreeBSD systems using Ansible, with practical examples and use cases.
Using Cron for Task Automation
Cron is a time-based job scheduler that allows users to schedule scripts or commands to run automatically at specified times and intervals.
Cron Basics
Cron jobs are defined in a crontab file, with each line representing a scheduled task. The format of a cron job entry is:
* * * * * command_to_execute
- - - - -
| | | | |
| | | | +---- Day of the week (0 - 7) (Sunday=0 or 7)
| | | +------ Month (1 - 12)
| | +-------- Day of the month (1 - 31)
| +---------- Hour (0 - 23)
+------------ Minute (0 - 59)
Practical Examples
Automating Backups
Create a script backup.sh
:
#!/bin/sh
tar -czf /backup/home-$(date +\%Y-\%m-\%d).tar.gz /home
Make the script executable:
chmod +x /usr/local/bin/backup.sh
Schedule the script to run daily:
crontab -e
Add the following line:
30 2 * * * /usr/local/bin/backup.sh
System Monitoring
Create a script monitor.sh
to check system disk space:
#!/bin/sh
df -h | mail -s "Disk Space Report" admin@example.com
Make the script executable:
chmod +x /usr/local/bin/monitor.sh
Schedule the script to run every hour:
crontab -e
Add the following line:
0 * * * * /usr/local/bin/monitor.sh
Advanced Cron Usage
Redirecting Output
To redirect the output of a cron job to a file:
0 * * * * /usr/local/bin/monitor.sh >> /var/log/monitor.log 2>&1
Using Environment Variables
Set environment variables in the crontab file:
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
0 2 * * * /usr/local/bin/backup.sh
Managing FreeBSD Systems with Ansible
Ansible is an open-source automation tool used for configuration management, application deployment, and task automation. It uses simple YAML files called playbooks to define automation tasks.
Installing Ansible on FreeBSD
Install Ansible using the package manager:
pkg install ansible
Ansible Basics
Ansible uses playbooks, which are YAML files that define tasks. An inventory file defines the hosts on which tasks are executed.
Ansible Inventory
Create an inventory file hosts
:
[webservers]
webserver1.example.com
webserver2.example.com
[dbservers]
dbserver1.example.com
Writing Ansible Playbooks
Create a playbook site.yml
:
---
- hosts: webservers
become: yes
tasks:
- name: Install Nginx
pkgng:
name: nginx
state: present
- name: Ensure Nginx is running
service:
name: nginx
state: started
enabled: yes
Running Playbooks
Run the playbook using the ansible-playbook
command:
ansible-playbook -i hosts site.yml
Practical Examples
Managing Users
Create a playbook users.yml
to manage users:
---
- hosts: all
become: yes
tasks:
- name: Ensure user 'john' exists
user:
name: john
state: present
groups: wheel
Run the playbook:
ansible-playbook -i hosts users.yml
Deploying Applications
Create a playbook deploy.yml
to deploy an application:
---
- hosts: webservers
become: yes
tasks:
- name: Install application dependencies
pkgng:
name:
- git
- python
state: present
- name: Clone application repository
git:
repo: 'https://github.com/example/app.git'
dest: /var/www/app
- name: Install Python dependencies
pip:
requirements: /var/www/app/requirements.txt
- name: Start the application
command: /var/www/app/start.sh
Run the playbook:
ansible-playbook -i hosts deploy.yml
Advanced Ansible Usage
Using Variables
Define variables in a playbook:
---
- hosts: webservers
become: yes
vars:
app_repo: 'https://github.com/example/app.git'
app_path: /var/www/app
tasks:
- name: Clone application repository
git:
repo: "{{ app_repo }}"
dest: "{{ app_path }}"
Conditional Execution
Use conditions to control task execution:
---
- hosts: webservers
become: yes
tasks:
- name: Install Nginx only if not installed
pkgng:
name: nginx
state: present
when: ansible_facts['pkg_mgr'] == 'pkgng'
Loops
Execute tasks in a loop:
---
- hosts: all
become: yes
tasks:
- name: Create multiple users
user:
name: "{{ item }}"
state: present
groups: wheel
loop:
- john
- jane
- doe
Integrating Cron with Ansible
You can use Ansible to manage cron jobs across multiple systems. Create a playbook cron.yml
:
---
- hosts: all
become: yes
tasks:
- name: Ensure backup cron job is present
cron:
name: "Daily Backup"
minute: "30"
hour: "2"
job: "/usr/local/bin/backup.sh"
Run the playbook:
ansible-playbook -i hosts cron.yml
Practical Use Cases
Automating System Updates
Create a playbook update.yml
to automate system updates:
---
- hosts: all
become: yes
tasks:
- name: Update pkg repository
command: pkg update
- name: Upgrade all packages
command: pkg upgrade -y
Schedule the playbook to run periodically using cron:
0 3 * * * ansible-playbook -i /path/to/hosts /path/to/update.yml
Centralized Backup Management
Create a playbook backup.yml
to manage backups:
---
- hosts: all
become: yes
tasks:
- name: Ensure backup directory exists
file:
path: /backup
state: directory
- name: Perform backup
command: tar -czf /backup/home-{{ ansible_date_time.date }}.tar.gz /home
Schedule the playbook to run daily using cron:
0 2 * * * ansible-playbook -i /path/to/hosts /path/to/backup.yml
Conclusion
Automation is a key aspect of modern system administration, improving efficiency, consistency, and reliability. FreeBSD offers powerful tools like cron for scheduling tasks and Ansible for configuration management and automation. By leveraging these tools, administrators can automate routine tasks, manage multiple systems efficiently, and ensure that their environments are consistent and reliable.
By understanding how to use cron and Ansible, you can take full advantage of these tools to streamline your workflow, reduce errors, and focus on more strategic tasks. Whether you are managing a single server or a fleet of machines, these automation techniques will help you maintain a robust and efficient FreeBSD environment.