AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Ansible Modules List: 50 Most Used Modules Quick Reference

By Luca Berton · Published 2024-01-01 · Category: installation

The 50 most used Ansible modules with quick reference examples. File management, packages, services, users, commands, cloud, and networking modules.

Ansible Modules List: 50 Most Used Modules Quick Reference

The 30 Most Used Ansible Modules

Ansible has thousands of modules, but you'll use a core set 90% of the time. Here are the 30 most essential modules with practical examples.

See also: Ansible Modules: Complete List & Guide to Built-in Modules (2026)

File Management Modules

1. ansible.builtin.copy

Copy files from control node to managed hosts.
- name: Copy config file
  ansible.builtin.copy:
    src: nginx.conf
    dest: /etc/nginx/nginx.conf
    owner: root
    group: root
    mode: '0644'
    backup: true

2. ansible.builtin.template

Deploy Jinja2 templates with variable substitution.
- name: Deploy virtual host
  ansible.builtin.template:
    src: vhost.conf.j2
    dest: /etc/nginx/sites-available/mysite
    mode: '0644'
  notify: Reload nginx

3. ansible.builtin.file

Manage files, directories, symlinks, and permissions.
- name: Create directory
  ansible.builtin.file:
    path: /opt/myapp/logs
    state: directory
    owner: appuser
    group: appuser
    mode: '0755'

- name: Create symlink ansible.builtin.file: src: /opt/myapp/current dest: /var/www/myapp state: link

- name: Remove a file ansible.builtin.file: path: /tmp/old-file.txt state: absent

4. ansible.builtin.lineinfile

Manage individual lines in files.
- name: Set SSH port
  ansible.builtin.lineinfile:
    path: /etc/ssh/sshd_config
    regexp: '^#?Port '
    line: 'Port 2222'

5. ansible.builtin.blockinfile

Manage multi-line blocks in files.
- name: Add iptables rules
  ansible.builtin.blockinfile:
    path: /etc/iptables/rules.v4
    block: |
      -A INPUT -p tcp --dport 80 -j ACCEPT
      -A INPUT -p tcp --dport 443 -j ACCEPT

6. ansible.builtin.stat

Get file status information.
- name: Check if config exists
  ansible.builtin.stat:
    path: /etc/myapp.conf
  register: config_file

- name: Create default config ansible.builtin.copy: src: default.conf dest: /etc/myapp.conf when: not config_file.stat.exists

7. ansible.builtin.fetch

Download files from remote hosts to control node.
- name: Fetch log file
  ansible.builtin.fetch:
    src: /var/log/myapp.log
    dest: ./logs/{{ inventory_hostname }}/
    flat: true

Package Management Modules

8. ansible.builtin.apt

Manage packages on Debian/Ubuntu.
- name: Install packages
  ansible.builtin.apt:
    name:
      - nginx
      - postgresql
      - redis
    state: present
    update_cache: true
    cache_valid_time: 3600

9. ansible.builtin.dnf

Manage packages on RHEL/CentOS/Fedora.
- name: Install packages
  ansible.builtin.dnf:
    name:
      - httpd
      - mariadb-server
    state: present

10. ansible.builtin.pip

Manage Python packages.
- name: Install Python packages
  ansible.builtin.pip:
    name:
      - flask
      - gunicorn
    virtualenv: /opt/myapp/venv

11. ansible.builtin.package

OS-agnostic package management.
- name: Install git (any OS)
  ansible.builtin.package:
    name: git
    state: present

See also: Ansible Cheat Sheet: Quick Reference Commands & Syntax (2026)

Service Management Modules

12. ansible.builtin.service

Manage system services.
- name: Start and enable nginx
  ansible.builtin.service:
    name: nginx
    state: started
    enabled: true

13. ansible.builtin.systemd

Manage systemd services with additional features.
- name: Reload systemd and start service
  ansible.builtin.systemd:
    name: myapp
    state: started
    enabled: true
    daemon_reload: true

Command Execution Modules

14. ansible.builtin.command

Execute commands without shell processing.
- name: Check disk usage
  ansible.builtin.command: df -h /
  register: disk_usage
  changed_when: false

15. ansible.builtin.shell

Execute commands through the shell.
- name: Find large files
  ansible.builtin.shell: find /var/log -name "*.log" -size +100M | head -10
  register: large_files
  changed_when: false

16. ansible.builtin.raw

Execute commands without Python (for bootstrapping).
- name: Install Python on minimal host
  ansible.builtin.raw: apt-get install -y python3

17. ansible.builtin.script

Transfer and execute a local script on remote hosts.
- name: Run setup script
  ansible.builtin.script: scripts/setup.sh
  args:
    creates: /opt/myapp/.installed

See also: Ansible Development: Write Custom Modules, Plugins & Collections

User and Group Modules

18. ansible.builtin.user

Manage user accounts.
- name: Create application user
  ansible.builtin.user:
    name: deploy
    groups: sudo,docker
    shell: /bin/bash
    create_home: true
    generate_ssh_key: true

19. ansible.builtin.group

Manage groups.
- name: Create application group
  ansible.builtin.group:
    name: myapp
    gid: 1050
    state: present

20. ansible.posix.authorized_key

Manage SSH authorized keys.
- name: Add SSH key
  ansible.posix.authorized_key:
    user: deploy
    key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"

System Modules

21. ansible.builtin.cron

Manage cron jobs.
- name: Schedule backup
  ansible.builtin.cron:
    name: "Daily backup"
    minute: "0"
    hour: "2"
    job: "/opt/scripts/backup.sh >> /var/log/backup.log 2>&1"

22. ansible.builtin.hostname

Set the system hostname.
- name: Set hostname
  ansible.builtin.hostname:
    name: "web-{{ inventory_hostname_short }}"

23. ansible.builtin.sysctl

Manage kernel parameters.
- name: Enable IP forwarding
  ansible.builtin.sysctl:
    name: net.ipv4.ip_forward
    value: '1'
    sysctl_set: true
    state: present
    reload: true

24. ansible.builtin.mount

Manage mount points.
- name: Mount NFS share
  ansible.builtin.mount:
    path: /mnt/shared
    src: nfs-server:/exports/shared
    fstype: nfs
    opts: rw,sync
    state: mounted

Network and Web Modules

25. ansible.builtin.get_url

Download files from HTTP/HTTPS/FTP.
- name: Download binary
  ansible.builtin.get_url:
    url: https://example.com/app-v2.0.tar.gz
    dest: /tmp/app.tar.gz
    checksum: sha256:abc123...

26. ansible.builtin.uri

Make HTTP requests (API calls, health checks).
- name: Check API health
  ansible.builtin.uri:
    url: http://localhost:8080/health
    method: GET
    status_code: 200
  register: health_check

Archive and Source Modules

27. ansible.builtin.unarchive

Extract archives on remote hosts.
- name: Extract application
  ansible.builtin.unarchive:
    src: /tmp/app.tar.gz
    dest: /opt/myapp/
    remote_src: true

28. ansible.builtin.git

Clone and manage Git repositories.
- name: Clone application repo
  ansible.builtin.git:
    repo: https://github.com/org/app.git
    dest: /opt/myapp
    version: main
    force: true

Debug and Control Modules

29. ansible.builtin.debug

Print messages and variables.
- name: Show variable
  ansible.builtin.debug:
    msg: "Server {{ inventory_hostname }} has IP {{ ansible_default_ipv4.address }}"

30. ansible.builtin.assert

Validate conditions.
- name: Verify enough disk space
  ansible.builtin.assert:
    that:
      - ansible_mounts | selectattr('mount','equalto','/') | map(attribute='size_available') | first > 1073741824
    fail_msg: "Less than 1GB free on /"
    success_msg: "Disk space OK"

Module Categories Overview

| Category | Key Modules | Count in Ansible | |----------|------------|-----------------| | Files | copy, template, file, lineinfile | 15+ | | Packages | apt, dnf, pip, package | 20+ | | Services | service, systemd | 5+ | | Commands | command, shell, raw, script | 5+ | | Users | user, group, authorized_key | 5+ | | System | cron, hostname, sysctl, mount | 20+ | | Network | uri, get_url, firewalld | 15+ | | Cloud | ec2, azure, gcp modules | 300+ | | Containers | docker_container, k8s | 30+ | | Database | mysql_db, postgresql_db | 15+ |

FAQ

How do I find all available modules?

ansible-doc -l                    # List all modules
ansible-doc -l | grep docker     # Filter by keyword
ansible-doc ansible.builtin.copy # Module documentation

What is FQCN and should I use it?

FQCN (Fully Qualified Collection Name) like ansible.builtin.copy is required in modern Ansible. It prevents naming conflicts and makes dependencies clear.

How do I write a custom module?

Create a Python script in a library/ directory. It must accept JSON input and return JSON with changed, failed, and msg fields.

Conclusion

Master these 30 modules and you'll handle 90% of automation tasks. Each module's full documentation is available via ansible-doc.

For detailed tutorials on every module, visit AnsiblePilot.

File Management

# copy — Copy files to remote
- copy: { src: app.conf, dest: /etc/app.conf, mode: '0644' }

# template — Jinja2 templates - template: { src: config.j2, dest: /etc/app.conf }

# file — File properties / create / delete - file: { path: /opt/myapp, state: directory, mode: '0755' }

# lineinfile — Manage single lines - lineinfile: { path: /etc/hosts, line: "10.0.1.10 db.local" }

# blockinfile — Manage text blocks - blockinfile: { path: /etc/ssh/sshd_config, block: "PermitRootLogin no" }

# get_url — Download files - get_url: { url: "https://example.com/file.tar.gz", dest: /tmp/ }

# unarchive — Extract archives - unarchive: { src: /tmp/app.tar.gz, dest: /opt/, remote_src: true }

# fetch — Download from remote to local - fetch: { src: /var/log/app.log, dest: /tmp/logs/ }

# stat — Check file properties - stat: { path: /opt/myapp }

# find — Search for files - find: { paths: /var/log, patterns: "*.log", age: "30d" }

# synchronize — rsync wrapper - synchronize: { src: /opt/src/, dest: /opt/dest/ }

Packages

# apt — Debian/Ubuntu
- apt: { name: nginx, state: present, update_cache: true }

# yum/dnf — RHEL/Fedora - yum: { name: httpd, state: present } - dnf: { name: httpd, state: present }

# pip — Python packages - pip: { name: flask, state: present, virtualenv: /opt/venv }

# package — OS-agnostic - package: { name: git, state: present }

# apt_repository — Add repos - apt_repository: { repo: "ppa:nginx/stable" }

System

# service/systemd — Manage services
- service: { name: nginx, state: started, enabled: true }
- systemd: { name: nginx, state: restarted, daemon_reload: true }

# user — User accounts - user: { name: deploy, groups: [sudo], shell: /bin/bash }

# group — Groups - group: { name: developers, state: present }

# cron — Scheduled tasks - cron: { name: "backup", minute: "0", hour: "2", job: "/opt/backup.sh" }

# hostname — Set hostname - hostname: { name: web1.example.com }

# sysctl — Kernel parameters - sysctl: { name: net.ipv4.ip_forward, value: "1" }

# mount — Manage mounts - mount: { path: /data, src: /dev/sdb1, fstype: ext4, state: mounted }

Commands

# command — Run command (no shell)
- command: /opt/scripts/deploy.sh

# shell — Run through shell (pipes work) - shell: cat /etc/hosts | grep db

# raw — Raw SSH (no Python needed) - raw: apt-get install -y python3

# script — Run local script remotely - script: scripts/setup.sh

# expect — Interactive commands - expect: { command: passwd user, responses: { "password": "secret" } }

Cloud

# amazon.aws.ec2_instance
- ec2_instance: { name: web1, instance_type: t3.medium, image_id: ami-abc }

# amazon.aws.s3_object - s3_object: { bucket: myapp, object: backup.gz, src: /tmp/backup.gz }

# community.docker.docker_container - docker_container: { name: nginx, image: "nginx:latest", ports: ["80:80"] }

Networking

# uri — HTTP requests
- uri: { url: "https://api.example.com/health", status_code: 200 }

# wait_for — Wait for port/condition - wait_for: { port: 8080, host: localhost, delay: 5, timeout: 60 }

# firewalld — Firewall rules - firewalld: { port: 80/tcp, permanent: true, state: enabled }

Info Gathering

# setup — Gather facts
- setup: { filter: "ansible_*_mb" }

# debug — Print info - debug: { msg: "{{ ansible_hostname }}" }

# assert — Validate conditions - assert: { that: "ansible_memtotal_mb >= 2048", fail_msg: "Need 2GB RAM" }

# set_fact — Set variables - set_fact: { deploy_time: "{{ ansible_date_time.iso8601 }}" }

# include_vars — Load variables - include_vars: { file: "{{ env }}.yml" }

FAQ

How to find the right module?

ansible-doc -l | grep keyword

What about deprecated modules?

Modules like yumansible.builtin.dnf, old docker_containercommunity.docker.docker_container. Check docs for migration paths.

Related Articles

Jinja2 templating in Ansibleusing when in Ansible playbooksorganizing hosts with Ansible inventoryDocker Compose with Ansibleamazon.aws collection walkthrough

Category: installation

Browse all Ansible tutorials · AnsiblePilot Home