Skip to main content

My Top Ansible Modules for Everyday SysAdmin Tasks

·1352 words·7 mins· loading · loading ·
Ronny Roethof
Author
Ronny Roethof
A security-minded sysadmin who fights corporate BS with open source weapons and sarcasm
Table of Contents

Tired of repetitive IT tasks? Ansible modules are your answer. They’re the building blocks for automating infrastructure management, deployments, and all that manual work we sysadmins love to hate.

If you’re in DevOps, system administration, or cloud, mastering these modules is a game-changer. This guide cuts to the chase, showing you what they are, how they work, and why you need them.

So, what are Ansible modules? Think of them as tiny scripts that get work done on your servers. They install software, manage users, tweak network settings, and handle databases. The magic? They’re idempotent. Run a module a million times, and it only makes changes if needed. This keeps your infrastructure consistent and avoids those “oops” moments.

As sysadmins, we’re always fighting for efficiency and consistency. Ansible’s agentless setup, simple YAML, and idempotent nature make it a powerful weapon in that fight.

In this post, I’m sharing the top 8 Ansible modules I use almost every day. These aren’t fancy or obscure, just solid workhorses that make my life easier.

Disclaimer: The examples here are simplified. Always test in non-prod! Use Ansible Vault (or similar) for sensitive data.


1. ansible.builtin.package (or specific like apt/dnf/yum)
#

Why it’s essential: Managing software packages is arguably one of the most frequent tasks for any sysadmin. Whether installing tools, applying security updates, or removing unwanted software, the package module (or its distribution-specific counterparts like apt or dnf) provides a consistent way to handle package management across different Linux distributions.

Simple Example: Ensuring htop and vim are installed.

- name: Ensure essential tools are installed
  ansible.builtin.package:
    name:
      - htop
      - vim
    state: present # Can be 'latest', 'absent', etc.
  become: yes # Usually requires root privileges

Key Parameters/Tips:

  • state: present (installed), latest (latest version), absent (removed).
  • name: Can be a single package name or a list.
  • Use update_cache: yes (especially with apt) when needed before installing.

2. ansible.builtin.service (or ansible.builtin.systemd)
#

Why it’s essential: Ensuring critical services are running (or stopped, or restarted) is fundamental. The service module provides a generic interface, while systemd offers more control for systems using systemd. It’s vital for managing web servers, databases, monitoring agents, and more.

Simple Example: Ensuring the SSH service is started and enabled on boot.

- name: Ensure sshd service is running and enabled
  ansible.builtin.service:
    name: sshd # Or 'ssh' depending on the distro
    state: started
    enabled: yes
  become: yes

Key Parameters/Tips:

  • state: started, stopped, restarted, reloaded.
  • enabled: yes or no to control whether the service starts on boot.
  • Consider ansible.builtin.systemd for finer control (e.g., daemon-reload).

3. ansible.builtin.copy
#

Why it’s essential: Deploying configuration files, scripts, or other necessary files from your control node to managed hosts is a core automation task. The copy module handles this reliably.

Simple Example: Copying a custom motd file to servers.

- name: Deploy custom message of the day
  ansible.builtin.copy:
    src: files/motd # Path to the file on your Ansible control node
    dest: /etc/motd
    owner: root
    group: root
    mode: '0644'
  become: yes

Key Parameters/Tips:

  • src: Source file path on the control node.
  • dest: Destination path on the managed node.
  • owner, group, mode: Essential for setting correct file permissions.
  • Use validate parameter for config files (e.g., sshd -t %s) to check syntax before finalizing the copy.

4. ansible.builtin.template
#

Why it’s essential: Often, configuration files aren’t static; they need variables specific to each host (like IP addresses, hostnames, resource limits). The template module uses the Jinja2 templating engine to render files dynamically before deploying them. This is incredibly powerful for managing configurations across diverse environments.

Simple Example: Creating a basic web server config using host variables.

  • Template file (templates/nginx.conf.j2):
server {
    listen 80;
    server_name {{ ansible_fqdn }}; # Uses Ansible facts

    location / {
        root /var/www/html;
        index index.html index.htm;
    }
}
  • Playbook task:
- name: Deploy Nginx config from template
  ansible.builtin.template:
    src: templates/nginx.conf.j2
    dest: /etc/nginx/sites-available/default
    owner: root
    group: root
    mode: '0644'
  become: yes
  notify: Restart Nginx # Handler to restart service after config change

Key Parameters/Tips:

  • Uses Jinja2 syntax ({{ variable_name }}) within the .j2 template file.
  • Accesses Ansible facts and variables defined in inventory or playbooks.
  • Often used with notify and handlers to restart services after config changes.

5. ansible.builtin.file
#

Why it’s essential: Beyond copying files, you often need to manage file attributes, create directories, set permissions, or ensure files/directories are absent. The file module is the versatile tool for these filesystem operations.

Simple Example: Ensuring a directory exists with specific permissions.

- name: Ensure web content directory exists
  ansible.builtin.file:
    path: /var/www/html
    state: directory # Can be 'file', 'link', 'hard', 'touch', 'absent'
    owner: www-data
    group: www-data
    mode: '0755'
  become: yes

Key Parameters/Tips:

  • path: The target file or directory path.
  • state: Defines the desired state (directory, file, link, absent, etc.).
  • owner, group, mode: Crucial for managing permissions and ownership.
  • recurse: yes: Can be used with owner/group/mode to apply changes recursively within a directory.

6. ansible.posix.mount
#

Why it’s essential: Managing mount points is a common task, especially when dealing with NFS shares, iSCSI volumes, or specific mount options for security. The ansible.posix.mount module allows you to ensure that filesystems are mounted as expected.

Simple Example: Mounting an NFS share.

- name: Mount NFS share
  ansible.posix.mount:
    path: /mnt/nfs_share
    src: nfs.example.com:/path/to/share
    fstype: nfs
    opts: rw,nosuid
    state: mounted
  become: yes

Key Parameters/Tips:

  • path: The mount point.
  • src: The source device or share.
  • fstype: The filesystem type (e.g., nfs, ext4, xfs).
  • opts: Mount options.
  • state: mounted, unmounted, or absent.

7. ansible.builtin.user
#

Why it’s essential: Managing user accounts is a fundamental sysadmin task. The user module allows you to create, modify, and delete user accounts, set passwords, and manage group memberships.

Simple Example: Creating a new user account.

- name: Create a new user account
  ansible.builtin.user:
    name: john
    comment: John Doe
    uid: 1001
    group: wheel
    password: "$6$some_salt$hashed_password" # Use ansible-vault!
    state: present
  become: yes

Key Parameters/Tips:

  • name: The username.
  • comment: Full name or description.
  • uid: User ID.
  • group: Primary group.
  • groups: List of secondary groups.
  • password: Hashed password (use ansible-vault for security!).
  • state: present (create/modify) or absent (delete).

8. ansible.builtin.cron
#

Why it’s essential: Scheduling tasks to run automatically is crucial for many sysadmin tasks, such as backups, log rotations, and system maintenance. The cron module provides a way to manage cron jobs.

Simple Example: Creating a daily backup cron job.

- name: Create a daily backup cron job
  ansible.builtin.cron:
    name: Daily backup
    job: /usr/local/bin/backup.sh
    hour: "0"
    minute: "0"
    user: root
  become: yes

Key Parameters/Tips:

  • name: A descriptive name for the cron job.
  • job: The command to run.
  • hour, minute, day, month, weekday: Scheduling parameters.
  • user: The user to run the cron job as.
  • state: present (create/modify) or absent (delete).

Error Handling in Ansible
#

Ansible provides mechanisms for handling errors gracefully in playbooks. The block, rescue, and always keywords allow you to define error handling logic.

- name: Example of error handling
  block:
    - name: Attempt a task
      ansible.builtin.command: /usr/bin/some_command
  rescue:
    - name: Handle the error
      ansible.builtin.debug:
        msg: "Command failed, handling the error"
  always:
    - name: Always run this task
      ansible.builtin.debug:
        msg: "This will always run"

Using ansible-lint
#

ansible-lint is a valuable tool for ensuring that your Ansible playbooks follow best practices and are well-written. It can identify potential issues and help you write more robust and maintainable playbooks.

To use ansible-lint, simply install it and run it against your playbook:

pip install ansible-lint
ansible-lint your_playbook.yml

Conclusion
#

These modules (package, service, copy, template, file, mount, user, cron) form a part of the backbone of many of my day-to-day Ansible playbooks. By mastering them, you can automate a significant chunk of routine sysadmin work, leading to more consistent, reliable, and manageable infrastructure.

Of course, Ansible’s power extends far beyond these basics. There are tons of other modules out there for specialized tasks! Think modules for managing:

  • Git (ansible.builtin.git)
  • Windows servers (ansible.windows.*)
  • Docker containers (community.docker.*)
  • Podman containers (community.podman.*)
  • MySQL databases (community.mysql.*)

and many many more.

Starting here provides a solid foundation for tackling common challenges.

What are your go-to Ansible modules for everyday tasks? Did I miss one of your favorites? Would you like to see a follow-up post covering more specialized modules? Share your thoughts and any other tips in the comments below!

Related

Let's Build an Ansible Role for Basic Linux Hardening
·2390 words·12 mins· loading · loading
The Update Obsession: Are We Really Opening the Front Door to Cybercriminals?
·1075 words·6 mins· loading · loading
My Favorite Tech Tools of 2025 (So Far)
·1606 words·8 mins· loading · loading