With a solid grasp of playbook structure and reusability, it’s time to apply those skills to real-world scenarios. In this part, we’ll explore how to automate common admin tasks using some of Ansible’s most useful built-in modules.
Creating Users
Creating users manually across multiple servers is time-consuming. With Ansible:
- name: Create a local user
user:
name: automation_user
state: present
shell: /bin/bash
Managing Services
Ensure essential services are enabled and running:
- name: Ensure UFW is running
service:
name: ufw
state: started
enabled: true
Scheduling Tasks with Cron
Automate recurring jobs like backups using the cron module:
- name: Schedule daily backup
cron:
name: "daily-backup"
user: root
job: "/usr/local/bin/backup.sh"
minute: "0"
hour: "2"
Managing Files and Configs
You can copy static config files or templates across machines:
- name: Copy NTP config
copy:
src: ntp.conf
dest: /etc/ntp.conf
owner: root
group: root
mode: '0644'
Gathering Facts About Systems
Use Ansible to collect info like OS type, memory, etc.:
- name: Gather system facts
ansible.builtin.setup:
Print specific values with debug:
- name: Show memory info
debug:
msg: "Memory: {{ ansible_memtotal_mb }} MB"
Bringing It All Together
Here’s an example playbook that uses everything above:
---
- name: Automate daily tasks
hosts: home_servers
become: true
tasks:
- name: Create user
user:
name: devops
state: present
- name: Install UFW
apt:
name: ufw
state: present
- name: Enable UFW
service:
name: ufw
state: started
enabled: true
- name: Schedule cleanup
cron:
name: cleanup
user: root
job: "/usr/local/bin/cleanup.sh"
hour: "3"
minute: "0"
Recap
In this post, you learned how to use Ansible to:
- Create local users
- Manage services
- Schedule cron jobs
- Copy config files
- Gather system facts
These are tasks sysadmins perform daily — and now you can automate them confidently.
Coming Up Next
In Part 4, we’ll organize everything into roles, use templates to generate configs, and keep your projects maintainable as they grow.
Let’s keep building your Ansible toolkit!