Operations 13 min read

Master Essential Ansible Modules: Ping, Setup, File, Copy, Service, and More

This guide reviews essential Ansible modules—including ping, setup, file, copy, service, cron, yum, user, group, synchronize, mount, get_url, and command—explaining their purposes, key options, and providing practical command‑line examples for effective agentless automation.

Efficient Ops
Efficient Ops
Efficient Ops
Master Essential Ansible Modules: Ping, Setup, File, Copy, Service, and More

Ansible is a powerful agentless IT automation platform; this article introduces several commonly used modules.

1. ping module

Tests connectivity of a host; usage is simple and requires no parameters.

# ansible 10.212.52.252 -m ping
10.212.52.252 | success >> {
  "changed": false,
  "ping": "pong"
}

2. setup module

Collects host information; often used via the gather_facts parameter in playbooks. Commonly filtered with the filter argument.

# 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/facts

3. file module

Manages files on remote hosts. Options include force for creating or replacing symbolic links.

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. Key options:

backup: backup before overwriting (yes|no)

content: replace src with raw content

dest: required destination path

directory_mode: set directory permissions

force: overwrite existing file if yes

src: local source path (file or directory)

validate: command to validate the file before copying

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. Important options include name, state, enabled, pattern, and others.

# Example action to reload service httpd
- service: name=httpd state=reloaded
# Enable service httpd without changing its running state
- service: name=httpd enabled=yes
# Start service foo based on a running process
- service: name=foo pattern=/usr/bin/foo state=started
# Restart network service for interface eth0
- service: name=network state=restarted args=eth0

6. cron module

Manages scheduled jobs.

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

Manages packages with the yum package manager.

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

Creates, modifies, or removes users and groups.

- 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
- user: name=test generate_ssh_key=yes ssh_key_bits=2048 ssh_key_file=.ssh/id_rsa
Note: When specifying the password parameter, encrypt the password before placing it in the module.
# openssl passwd -1 -salt $(< /dev/urandom tr -dc '[:alnum:]' | head -c 32)
Password: $1$YngB4z8s$atSVltYKnDxJmWZ3s.4/80
# echo "123456" | openssl passwd -1 -salt $(< /dev/urandom tr -dc '[:alnum:]' | head -c 32) -stdin
$1$4P4PlFuE$ur9ObJiT5iHNrb9QnjaIB0
# ansible all -m user -a 'name=foo password="$1$4P4PlFuE$ur9ObJiT5iHNrb9QnjaIB0"'
- group: name=somegroup state=present

9. synchronize module

Synchronizes files using rsync.

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

Manages mount points. Important options:

dumpfstype: required filesystem type

name: required mount point name

opts: options passed to the mount command

src: required source device or file

state: present, absent, mounted, unmounted

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

Downloads files from HTTP/HTTPS/FTP sources, similar to wget.

sha256sum: verify checksum after download

timeout: download timeout (default 10s)

url: source URL

url_username / url_password: credentials for protected URLs

use_proxy: whether to use a proxy defined in the environment

- 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

Executes commands directly without shell processing, enhancing security.

Other useful modules include git, svn, sysctl, authorized_key, apt, zypper, pip, gem, find, template, mysql_db, redis, and url.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

automationConfiguration ManagementDevOpsansibleIT Operations
Efficient Ops
Written by

Efficient Ops

This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.