Ansible Tutorial · Playbooks · Lesson 7 of 20
Ansible Modules
Learn the most commonly used built-in modules.
What is a module?
A module is a small program Ansible copies to the managed node and runs. Each module handles one type of task. Ansible ships with hundreds of modules; here are the ones you will use constantly.
Package management
apt (Debian/Ubuntu)
- name: Install packages
ansible.builtin.apt:
name:
- nginx
- curl
- htop
state: present
update_cache: true
yum / dnf (RHEL/CentOS)
- name: Install packages
ansible.builtin.dnf:
name: httpd
state: latest
Service management
- name: Start and enable nginx
ansible.builtin.service:
name: nginx
state: started
enabled: true
States: started, stopped, restarted, reloaded.
File operations
copy — push a local file
- name: Deploy config
ansible.builtin.copy:
src: files/app.conf
dest: /etc/app/app.conf
owner: root
mode: "0644"
template — push a Jinja2 template
- name: Render nginx config
ansible.builtin.template:
src: templates/nginx.conf.j2
dest: /etc/nginx/nginx.conf
file — create directories, set permissions, symlinks
- name: Create app directory
ansible.builtin.file:
path: /var/www/app
state: directory
owner: www-data
mode: "0755"
lineinfile — edit a single line
- name: Disable root login
ansible.builtin.lineinfile:
path: /etc/ssh/sshd_config
regexp: "^PermitRootLogin"
line: "PermitRootLogin no"
User management
- name: Create deploy user
ansible.builtin.user:
name: deploy
groups: sudo
shell: /bin/bash
create_home: true
Git
- name: Clone the repo
ansible.builtin.git:
repo: https://github.com/org/app.git
dest: /var/www/app
version: main
command vs shell
command runs an executable directly — no pipes, no redirects, no shell variables.
shell runs through /bin/sh, so shell features work:
- name: Redirect output (needs shell)
ansible.builtin.shell: df -h > /tmp/disk_report.txt
Use command when you can; it is safer because there is no risk of shell injection.
Module documentation
ansible-doc apt # full docs for a module
ansible-doc -l # list all available modules
ansible-doc -s apt # short snippet / playbook example
ansible-doc -s ansible.builtin.copy | head -20
Output
- name: Copy files to remote locations
copy:
backup: # Create a backup file including the timestamp.
checksum: # SHA1 checksum of the file being transferred.
content: # When used instead of src, sets the contents of a file.
dest: # (required) Remote absolute path where the file should be copied to.
directory_mode: # Set the access permissions of newly created directories.
follow: # Follow file symlinks.
force: # Force overwrite.
group: # Name of the group that should own the file.
mode: # The permissions of the file.
owner: # Name of the user that should own the file.
remote_src: # If yes, the src is on the remote machine.
src: # Local path to a file to copy to the remote server.Run this on your computer — see “Get Started” for setup.
ansible-doc -l 2>/dev/null | wc -l
Output
7920
Run this on your computer — see “Get Started” for setup.
Test yourself
Which module should you use to edit a single line in a config file?
copytemplatelineinfilereplace
Show answer
Answer: lineinfile
lineinfile ensures a particular line is present (or absent) in a file, identified by a regex. Use template when you need to manage the entire file.