
Command Module
ansible all -m command -a “ls /home”: Executes the ls /home command on all remote hosts, directly returning the directory contents.
ansible webservers -m command -a “pwd”: Executes the pwd command on all hosts in the webservers group, displaying the current working directory.
ansible 192.168.1.10 -m command -a “date”: Executes the date command on the host with IP 192.168.1.10, retrieving the current system time.
Shell Module
ansible all -m shell -a “ps aux | grep nginx”: Executes a piped command through the shell on all hosts, filtering for nginx-related processes.
ansible dbservers -m shell -a “echo ‘test’ > /tmp/shell.txt”: Creates /tmp/shell.txt on hosts in the dbservers group using shell redirection and writes content to it.
ansible all -m shell -a “cat /etc/passwd | wc -l”: Counts the number of users in the /etc/passwd file on all hosts, utilizing the shell’s piping functionality.
Cron Module
ansible all -m cron -a “name=’daily backup’ minute=0 hour=2 job=’/scripts/backup.sh'”: Adds a backup task to run daily at 2 AM on all hosts, named daily backup.
ansible appservers -m cron -a “name=’clean logs’ weekday=6 minute=30 hour=1 job=’/scripts/clean_logs.sh'”: Sets a log cleaning script to run at 1:30 AM every Saturday on hosts in the appservers group.
ansible all -m cron -a “name=’old task’ state=absent”: Deletes the scheduled task named old task on all hosts.
User Module
ansible all -m user -a “name=testuser state=present”: Creates the user testuser on all hosts.
ansible all -m user -a “name=olduser state=absent remove=yes”: Deletes the user olduser from all hosts and removes their home directory.
ansible webservers -m user -a “name=webuser groups=www-data append=yes”: Adds the user webuser in the webservers group to the www-data group, preserving their original group membership.
Group Module
ansible all -m group -a “name=devgroup state=present”: Creates the user group devgroup on all hosts.
ansible dbservers -m group -a “name=dbgroup gid=1005 state=present”: Creates the dbgroup group with gid 1005 on hosts in the dbservers group.
ansible all -m group -a “name=oldgroup state=absent”: Deletes the oldgroup user group on all hosts.
Copy Module
ansible all -m copy -a “src=./localfile dest=/remote/path”: Copies the local file localfile to the /remote/path on all hosts.
ansible webservers -m copy -a “src=./nginx.conf dest=/etc/nginx/nginx.conf mode=0644”: Copies the local nginx.conf to the corresponding path on hosts in the webservers group, setting permissions to 0644.
ansible all -m copy -a “content=’hello’ dest=/tmp/content.txt”: Creates content.txt in the /tmp directory on all hosts with the content hello.
File Module
ansible all -m file -a “path=/tmp/testdir state=directory mode=0755”: Creates the /tmp/testdir directory on all hosts with permissions set to 0755.
ansible dbservers -m file -a “path=/tmp/oldfile state=absent”: Deletes the /tmp/oldfile file or directory from all hosts in the dbservers group.
ansible all -m file -a “path=/tmp/link state=link src=/etc/hosts”: Creates a symbolic link /tmp/link pointing to /etc/hosts on all hosts.
Hostname Module
ansible web01 -m hostname -a “name=webserver01”: Changes the hostname of host web01 to webserver01.
ansible db02 -m hostname -a “name=dbserver02.example.com”: Sets the hostname of host db02 to dbserver02.example.com with domain.
Ping Module
ansible all -m ping: Tests connectivity to all hosts.
ansible appservers -m ping: Checks if all hosts in the appservers group are online.
ansible 192.168.1.20 -m ping: Tests if the host with IP 192.168.1.20 is reachable.
Yum Module
ansible all -m yum -a “name=httpd state=present”: Installs the httpd service on all RPM-based hosts.
ansible webservers -m yum -a “name=nginx state=latest”: Installs the latest version of nginx on hosts in the webservers group.
ansible all -m yum -a “name=php state=absent”: Uninstalls the php software from all hosts.
Service/Systemd Module
ansible all -m service -a “name=httpd state=started enabled=yes”: Starts the httpd service on all hosts and sets it to start on boot.
ansible webservers -m systemd -a “name=nginx state=restarted”: Restarts the nginx service on hosts in the webservers group (managed by systemd).
ansible dbservers -m service -a “name=mysql state=stopped”: Stops the mysql service on hosts in the dbservers group.
Script Module
ansible all -m script -a “./deploy.sh”: Executes the local deploy.sh script on all hosts.
ansible appservers -m script -a “./init.sh arg1 arg2”: Runs the local init.sh script on hosts in the appservers group, passing arg1 and arg2 as parameters.
Template Module
ansible all -m template -a “src=./templates/config.j2 dest=/etc/app/config.conf”: Generates the /etc/app/config.conf configuration file on all hosts using the local config.j2 template.
ansible webservers -m template -a “src=./templates/vhost.j2 dest=/etc/nginx/conf.d/vhost.conf”: Generates nginx virtual host configuration on hosts in the webservers group based on the vhost.j2 template.
Archive Module
ansible all -m archive -a “path=/var/log/nginx dest=/backup/nginx_logs.tar.gz format=gz”: Compresses the /var/log/nginx directory from all hosts into /backup/nginx_logs.tar.gz (gzip format).
ansible dbservers -m archive -a “path=/data/mysql dest=/backup/mysql_data.zip format=zip exclude=*.log”: Compresses the /data/mysql directory from hosts in the dbservers group into zip format, excluding .log files.
Replace Module
ansible all -m replace -a “path=/etc/sysctl.conf regexp=’^net.ipv4.ip_forward = 0′ replace=’net.ipv4.ip_forward = 1′”: Changes the IP forwarding configuration from 0 to 1 in the sysctl.conf on all hosts.
ansible webservers -m replace -a “path=/etc/nginx/nginx.conf regexp=’worker_processes auto;’ replace=’worker_processes 4;'”: Modifies the number of worker processes to 4 in the nginx.conf on hosts in the webservers group.
Setup Module
ansible all -m setup: Collects system information (such as hardware, network, operating system, etc.) from all hosts.
ansible webservers -m setup -a “filter=ansible_distribution”: Collects only the operating system distribution information from hosts in the webservers group.
Mount Module
ansible all -m mount -a “path=/data src=/dev/sdb1 fstype=ext4 state=mounted”: Mounts the /dev/sdb1 partition to the /data directory on all hosts (ext4 format) and configures it to auto-mount on boot.
ansible dbservers -m mount -a “path=/mnt/nfs src=192.168.1.100:/nfs_share fstype=nfs state=mounted”: Mounts the NFS shared directory to /mnt/nfs on hosts in the dbservers group.
Previous Recommendations:
[Linux Learning] Three Steps to Mount a USB Drive on the Server
[Linux Learning] Performance Optimization, Detailed Explanation of the iostat Command
[Linux Learning] CentOS 7/8 Firewall Configuration Tutorial~1
[Linux Learning] Ubuntu Firewall Configuration Tutorial
[Linux Learning] Disk Partitioning: fdisk or parted?
[Linux Learning] Unable to Enter System, What is journalctl -xb?
[Linux Learning] Load Monitoring Commands: top, vmstat, iostat Detailed Explanation
[Linux Learning] Forgot CentOS 7.8 Password!
[Linux Learning] Detailed Guide to Configuring bond1 on CentOS 7