Ansible is a powerful and agentless IT automation platform. This article introduces some commonly used modules. Ansible has robust module capabilities, supporting various functions such as file management, system management, cloud computing (multi-cloud), networking, databases, Windows, source control, infrastructure, packaging, and more. For more details, please refer to the official page. Here, we select some of the most commonly used modules from the official classification for introduction.
1. Ping Module
This module tests whether a host is reachable. The usage is very simple and does not involve parameters:
# ansible 10.212.52.252 -m ping 10.212.52.252 | success >> { "changed": false, "ping": "pong" }
2. Setup Module
The setup module is mainly used to gather information about the host. The parameter gather_facts often used in playbooks is related to this module. A frequently used parameter under the setup module is the filter parameter, with specific usage examples as follows (due to the large output, only the commands are listed without results):
# ansible 10.212.52.252 -m setup -a 'filter=ansible_*_mb' // View memory information of the host # ansible 10.212.52.252 -m setup -a 'filter=ansible_eth[0-2]' // View network card information for interfaces eth0-2 # ansible all -m setup --tree /tmp/facts // Output all host information to the /tmp/facts directory, with each host's information in a file named after the hostname (from /etc/ansible/hosts)
3. File Module
The file module is mainly used for file operations on remote hosts. The file module includes the following options: force: This option needs to be set to force the creation of a symbolic link in two cases: one is when the source file does not exist but will be created later; the other is when the target symbolic link already exists, requiring the previous symlink to be removed before creating a new one. There are two options: yes|no.
Usage examples:
ansible test -m file -a "src=/etc/fstab dest=/tmp/fstab state=link" ansible test -m file -a "path=/tmp/fstab state=absent" ansible test -m file -a "path=/tmp/test state=touch"
4. Copy Module
This module copies files to remote hosts and includes the following options:
- backup: Backs up the original file before overwriting, with the backup file containing timestamp information. There are two options: yes|no.
- content: Used to replace “src”, allowing direct specification of the value of the file.
- dest: Required. The absolute path on the remote host where the source file will be copied. If the source file is a directory, this path must also be a directory.
- directory_mode: Recursively sets the directory permissions, defaulting to the system’s default permissions.
- force: If the target host contains the file but the content is different, setting this to yes will force an overwrite. If set to no, the file will only be copied if it does not already exist at the target location. Default is yes.
- others: All options from the file module can be used here.
- src: The local address of the file to be copied to the remote host, which can be an absolute or relative path. If the path is a directory, it will be copied recursively. In this case, if the path ends with “/”, only the contents of the directory will be copied; if it does not end with “/”, the entire content including the directory will be copied, similar to rsync.
- validate: A validation command to run before the copy is complete. The file path to validate is passed through ‘%s’, which must exist.
Examples:
ansible test -m copy -a "src=/srv/myfiles/foo.conf dest=/etc/foo.conf owner=foo group=foo mode=0644" ansible test -m copy -a "src=/mine/ntp.conf dest=/etc/ntp.conf owner=root group=root mode=644 backup=yes" ansible test -m copy -a "src=/mine/sudoers dest=/etc/sudoers validate='visudo -cf %s'"
5. Service Module
This module is used to manage services and includes the following options:
- arguments: Provides some options for the command line.
- enabled: Whether to start on boot yes|no.
- name: Required. The name of the service.
- pattern: Defines a pattern. If there is no response when checking the service status with the status command, it will search for the process based on this pattern using the ps command. If a match is found, the service is considered to be running.
- runlevel: The run level.
- sleep: If restarted, sleep for a few seconds between stop and start.
- state: Performs actions such as start, stop, restart, or reload on the current service (started, stopped, restarted, reloaded).
Usage examples:
# Example action to reload service httpd, in all cases - service: name=httpd state=reloaded # Example action to enable service httpd, and not touch the running state - service: name=httpd enabled=yes # Example action to start service foo, based on running process /usr/bin/foo - service: name=foo pattern=/usr/bin/foo state=started # Example action to restart network service for interface eth0 - service: name=network state=restarted args=eth0
6. Cron Module
Used to manage scheduled tasks.
Examples:
ansible test -m cron -a 'name="a job for reboot" special_time=reboot job="/some/job.sh"' ansible test -m cron -a 'name="yum autoupdate" weekday="2" minute=0 hour=12 user="root"' ansible 10.212.52.252 -m cron -a 'backup="True" name="test" minute="0" hour="2" job="ls -alh > /dev/null"' ansible test -m cron -a 'cron_file=ansible_yum-autoupdate state=absent'
7. Yum Module
Uses the yum package manager to manage software packages.
ansible test -m yum -a 'name=httpd state=latest' ansible test -m yum -a 'name="@Development tools" state=present' ansible test -m yum -a 'name=http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm state=present'
8. User Module and Group Module
The user module requests the useradd, userdel, and usermod commands, while the group module requests the groupadd, groupdel, and groupmod commands. The specific parameters will not be detailed here; examples will be provided directly.
1. User Module Example:
- user: name=johnd comment="John Doe" uid=1040 group=admin - user: name=james shell=/bin/bash groups=admins,developers append=yes - user: name=johnd state=absent remove=yes - user: name=james18 shell=/bin/zsh groups=developers expires=1422403387 # When generating keys, only public and private key files will be created, similar to using the ssh-keygen command, and no authorized_keys file will be generated. - user: name=test generate_ssh_key=yes ssh_key_bits=2048 ssh_key_file=.ssh/id_rsa
Note: When specifying the password parameter, the following string will be directly transmitted to the /etc/shadow file of the managed host, so the password string needs to be encrypted first. Then, the resulting string can be placed in the password field.
# openssl passwd -1 -salt $(< /dev/urandom tr -dc '[:alnum:]' | head -c 32) Password: $1$YngB4z8s$atSVltYKnDxJmWZ3s.4/80 or # echo "123456" | openssl passwd -1 -salt $(< /dev/urandom tr -dc '[:alnum:]' | head -c 32) -stdin $1$4P4PlFuE$ur9ObJiT5iHNrb9QnjaIB0 # The following generated password string can also be used normally, but it does not conform to the format of /etc/shadow, so it is not recommended to use it. # openssl passwd -salt -1 "123456" -1yEWqqJQLC66 # Create user with the above password #ansible all -m user -a 'name=foo password="$1$4P4PlFuE$ur9ObJiT5iHNrb9QnjaIB0"'
2. Group Example
- group: name=somegroup state=present
9. Synchronize Module
Uses rsync to synchronize files, with the following usages:
src=some/relative/path dest=/some/absolute/path rsync_path="sudo rsync" src=some/relative/path dest=/some/absolute/path archive=no links=yes src=some/relative/path dest=/some/absolute/path checksum=yes times=no src=/tmp/helloworld dest=/var/www/helloword rsync_opts=--no-motd,--exclude=.git mode=pull
10. Mount Module
Configures mount points.
Options:
- dumpfstype: Required. The type of the mounted file.
- name: Required. The mount point.
- opts: Parameters passed to the mount command.
- src: Required. The file to be mounted.
- state: Required. present: only processes the configuration in fstab.
- absent: removes the mount point.
- mounted: automatically creates the mount point and mounts it.
- unmounted: unmounts.
Examples:
name=/mnt/dvd src=/dev/sr0 fstype=iso9660 opts=ro state=present name=/srv/disk src='LABEL=SOME_LABEL' state=present name=/home src='UUID=b3e48f45-f933-4c8e-a700-22a159ec9077' opts=noatime state=present ansible test -a 'dd if=/dev/zero of=/disk.img bs=4k count=1024' ansible test -a 'losetup /dev/loop0 /disk.img' ansible test -m filesystem 'fstype=ext4 force=yes opts=-F dev=/dev/loop0' ansible test -m mount 'name=/mnt src=/dev/loop0 fstype=ext4 state=mounted opts=rw'
11. Get URL Module
This module is mainly used to download files from http, ftp, and https servers (similar to wget), with the following options:
- sha256sum: Performs a sha256 check after downloading.
- timeout: Download timeout, default is 10s.
- url: The URL to download.
- url_password, url_username: Mainly used for situations requiring username and password authentication.
- use_proxy: Indicates whether to use a proxy, which must be defined in the environment variables beforehand.
Examples:
- name: download foo.conf get_url: url=http://example.com/path/file.conf dest=/etc/foo.conf mode=0440 - name: download file with sha256 check get_url: url=http://example.com/path/file.conf dest=/etc/foo.conf sha256sum=b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c
12. Command Module
The command module executes commands directly without shell processing, enhancing security by avoiding shell interpretation of special characters.
This concludes the introduction to the modules. The official documentation provides additional modules that may be useful, including git, svn version control modules, sysctl, authorized_key_module system modules, apt, zypper, pip, gem package management modules, find, template file modules, mysql_db, redis database modules, url network modules, etc. For more details, please refer to the official manual’s module section.
1017 GOPS 2025 Shanghai Station: From Steady State to Chaos, Unveiling the SRE Stability Practices of ICBC, Ctrip, Tencent, and Honor.

Recent Good Articles:
150 Essential Linux Operations Commands: If You’re Not Proficient, Trouble Will Come?
[September 26th Heavyweight Arrival] Registration Channel Officially Opens! Preview of the 2025 Fourth XOps Industry Ecosystem Innovation Development Forum Released.
How to Use Large Models to Achieve Continuous Integration and Monitoring Feedback Loops?
The “Efficient Operations” public account invites technical personnel to submit articles.
Submission Email: [email protected], or add WeChat contact: greatops1118.

Click “View” to ensure a year without downtime.