Master the Most Common Ansible Modules: From ping to get_url
This guide introduces the most frequently used Ansible modules—including ping, setup, file, copy, service, cron, yum, user, group, synchronize, mount, and get_url—explaining their purpose, key options, and providing concrete command‑line examples to help you automate system tasks efficiently.
1. ping module
Tests connectivity of a host; usage is simple with no parameters.
# ansible 10.212.52.252 -m ping
10.212.52.252 | success > { "changed": false, "ping": "pong" }2. setup module
Collects host facts; commonly used with the gather_facts parameter. Example commands using the filter option:
# ansible 10.212.52.252 -m setup -a 'filter=ansible_*_mb'
# ansible 10.212.52.252 -m setup -a 'filter=ansible_eth[0-2]'
# ansible all -m setup --tree /tmp/facts3. file module
Manages files on remote hosts. Key options include:
force – create symlinks forcefully (yes|no)
group – set group ownership
mode – set permissions
owner – set owner
path – required, file or directory path
recurse – apply attributes recursively (directories only)
src – source path for links
dest – destination path for links
state – directory, file, link, hard, touch, absent, etc.
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
Copies files to remote hosts. Important options:
backup – backup before overwriting (yes|no)
content – set file content directly
dest – required destination path
directory_mode – permissions for directories
force – overwrite existing files (yes|no)
src – source file or directory
validate – command to validate before copying
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
Manages system services. Main options:
arguments – extra command‑line options
enabled – start on boot (yes|no)
name – required service name
pattern – process pattern for status checks
runlevel – run level
sleep – pause between stop and start when restarting
state – started, stopped, restarted, reloaded, etc.
Examples:
- service: name=httpd state=reloaded
- service: name=httpd enabled=yes
- service: name=foo pattern=/usr/bin/foo state=started
- service: name=network state=restarted args=eth06. cron module
Manages scheduled jobs. Key options include backup, cron_file, day, hour, minute, month, weekday, job, name, special_time, state, and user.
Examples:
# Create a reboot job
ansible test -m cron -a 'name="a job for reboot" special_time=reboot job="/some/job.sh"'
# Daily yum autoupdate at 12:00 on Tuesday
ansible test -m cron -a 'name="yum autoupdate" weekday="2" minute=0 hour=12 user="root"'
# Remove a job
ansible test -m cron -a 'cron_file=ansible_yum-autoupdate state=absent'7. yum module
Manages packages with the yum package manager. Important options:
config_file – yum configuration file
disable_gpg_check – disable GPG check
disablerepo – disable a repository
enablerepo – enable a repository
name – package name, URL, or local rpm path
state – present, absent, latest
Examples:
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 and group modules
Manage users and groups. Examples for user creation, modification, and deletion:
- 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=test generate_ssh_key=yes ssh_key_bits=2048 ssh_key_file=.ssh/id_rsaGroup example:
- group: name=somegroup state=present9. synchronize module
Synchronizes files using rsync. Important options include archive, checksum, compress, copy_links, delete, dest, dest_port, dirs, rsync_opts, set_remote_user, mode (push/pull).
Examples:
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=pull10. mount module
Configures mount points. Core options:
dump
fstype – required filesystem type
name – mount point
opts – mount options
src – source device or file
state – present, absent, mounted, unmounted
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=present11. get_url module
Downloads files from HTTP/HTTPS/FTP sources. Main options:
sha256sum – verify checksum after download
timeout – download timeout (default 10s)
url – source URL
url_username / url_password – authentication
use_proxy – whether to use a proxy
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=b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944cSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
