Ansible Modules and Collections
Ansible Modules
Ansible executes tasks through modules, such as <span>ansible.builtin.hostname</span>
for setting the hostname.
<span>ansible.builtin</span>
is the namespace for modules. Different modules belong to different namespaces. Generally, module names do not conflict, but as the number of modules increases, the possibility of name conflicts cannot be ruled out. Therefore, different modules are placed in different namespaces for distinction. If there are no name conflicts with the modules installed on the control node, the namespace can be omitted when calling the module, such as:<span>hostname</span>
。
Ansible is written in Python, so most of Ansible’s modules require Python to be installed on the managed hosts, with a few exceptions, such as:
<span>ansible.builtin.raw</span>
。
Here are some commonly used modules.
File and Directory Operation Modules
Module Name | Description |
<span>ansible.builtin.copy</span> |
Copy files to remote hosts |
<span>ansible.builtin.template</span> |
Render Jinja2 templates and transfer them |
<span>ansible.builtin.fetch</span> |
Fetch files from remote hosts |
<span>ansible.builtin.file</span> |
Set file permissions, ownership, etc. |
<span>ansible.builtin.stat</span> |
Get file status |
<span>ansible.builtin.unarchive</span> |
Unarchive files |
<span>ansible.builtin.lineinfile</span> |
Modify a line in a file |
<span>ansible.builtin.blockinfile</span> |
Insert multiple lines into a file |
System Management Modules
Module Name | Description |
<span>ansible.builtin.hostname</span> |
Set the hostname |
<span>ansible.builtin.user</span> |
Create/modify/delete users |
<span>ansible.builtin.group</span> |
Create/modify/delete user groups |
<span>ansible.builtin.service</span> |
Manage system services |
<span>ansible.builtin.systemd</span> |
Manage systemd services |
<span>ansible.builtin.package</span> |
Install/uninstall common packages (automatically selects backend) |
<span>ansible.builtin.yum</span> |
Install packages using yum |
<span>ansible.builtin.apt</span> |
Install packages using apt |
<span>ansible.builtin.dnf</span> |
Install packages using dnf |
Command Execution Modules
Module Name | Description |
<span>ansible.builtin.command</span> |
Run ordinary commands (does not support pipes, redirection) |
<span>ansible.builtin.shell</span> |
Run shell commands (supports pipes, redirection, etc.) |
<span>ansible.builtin.raw</span> |
Raw command execution, does not go through the module system |
<span>ansible.builtin.script</span> |
Upload and execute local scripts |
Archiving and Compression
Module Name | Description |
<span>ansible.builtin.archive</span> |
Create archive files (e.g., tar.gz) |
<span>ansible.builtin.unarchive</span> |
Unarchive files |
Permissions and Authentication
Module Name | Description |
<span>ansible.builtin.seboolean</span> |
Manage SELinux boolean values |
<span>ansible.builtin.selinux</span> |
Set SELinux mode |
<span>ansible.builtin.authorized_key</span> |
Add SSH public key to user <span>~/.ssh/authorized_keys</span> |
Networking and Remote Connections
Module Name | Description |
<span>ansible.builtin.uri</span> |
Send HTTP requests |
<span>ansible.builtin.get_url</span> |
Download files |
<span>ansible.builtin.wait_for</span> |
Wait for port/file status changes |
Control Logic Modules
Module Name | Description |
<span>ansible.builtin.debug</span> |
Output debug information |
<span>ansible.builtin.pause</span> |
Pause execution |
<span>ansible.builtin.assert</span> |
Conditional assertion |
<span>ansible.builtin.set_fact</span> |
Set custom variables |
These modules can be viewed through
<span>ansible-doc <module_name></span>
to see the help documentation. You can quickly understand the usage of a module by searching for<span>EXAMPLE</span>
, and you can also view module options by using<span>ansible-doc -s <module_name></span>
.
For example, the <span>ansible.builtin.systemd</span>
module is used for service management. You can view the module documentation and search for <span>EXAMPLE</span>
using <span>ansible-doc</span>
.
[root@awx-1 ansible]# ansible-doc ansible.builtin.systemd
# Search with /EXAMPLE
EXAMPLES:
- name: Make sure a service unit is running
ansible.builtin.systemd:
state: started
name: httpd
- name: Stop service cron on debian, if running
ansible.builtin.systemd:
name: cron
state: stopped
- name: Restart service cron on centos, in all cases, also issue daemon-reload to pick up config changes
ansible.builtin.systemd:
state: restarted
daemon_reload: true
name: crond
- name: Reload service httpd, in all cases
ansible.builtin.systemd:
name: httpd.service
state: reloaded
- name: Enable service httpd and ensure it is not masked
ansible.builtin.systemd:
name: httpd
enabled: true
masked: no
- name: Enable a timer unit for dnf-automatic
ansible.builtin.systemd:
name: dnf-automatic.timer
state: started
enabled: true
- name: Just force systemd to reread configs (2.4 and above)
ansible.builtin.systemd:
daemon_reload: true
- name: Just force systemd to re-execute itself (2.8 and above)
ansible.builtin.systemd:
daemon_reexec: true
- name: Run a user service when XDG_RUNTIME_DIR is not set on remote login
ansible.builtin.systemd:
name: myservice
state: started
scope: user
environment:
XDG_RUNTIME_DIR: "/run/user/{{ myuid }}"
Many options are self-explanatory, for example, <span>enabled</span>
is used to set whether to start on boot, and <span>state</span>
is used to set the service status.
More modules can be found on the official website: https://docs.ansible.com/ansible/latest/collections/all_plugins.html
You can also search for modules here: https://galaxy.ansible.com/ui/
Regarding modules, I will write separately about commonly used modules and their common options later.
Ansible Collections
Ansible-core comes with a set of modules starting with <span>ansible.builtin</span>
, but these modules are far from sufficient. Additional modules are needed to extend Ansible’s functionality. To facilitate the management of these modules, they are categorized, and a set of modules forms a collection.

# Download collection
ansible-galaxy collection download community.general