Mastering Common Ansible Modules: File, Copy, Yum, Service, and More
This guide walks through the most frequently used Ansible modules—including file, copy, yum_repository, yum, service, systemd, user, group, fetch, get_url, and setup—explaining their key options and demonstrating ad‑hoc command usage with complete code examples for Linux automation.
Common Ansible Modules
These are the most frequently used Ansible modules, demonstrated with ad‑hoc commands; playbook usage will be covered later.
file
copy
yum_repository
yum
service
systemd
user
group
fetch
get_url
setup
1. File Module
Options
path (required): file or directory path
state : file, touch, absent, directory, link, hard
owner : file owner
group : file group
mode : permissions
src : source for link
dest : destination for link
Usage Examples
ansible all -m file -a 'path=/tmp/file1 state=touch owner=ansible group=root mode=666'
ansible all -m shell -a 'ls -l /tmp/file1'
ansible all -m file -a 'path=/tmp/dire1 state=directory'
ansible all -m file -a 'path=/tmp/dire1 state=absent'
ansible all -m file -a 'path=/tmp/file1 state=absent'2. Copy Module
Options
src (required): local source file or directory
dest (required): remote destination path
force : overwrite if true (default yes)
backup : create backup before overwriting
content : inline file content
remote_src : treat src as remote path
Usage Examples
ansible all -m copy -a 'src=./ansible.cfg dest=/tmp/ansible.cfg'
ansible all -m copy -a "content='hello,world' dest=/tmp/hello"
ansible all -m shell -a 'cat /tmp/hello'3. yum_repository Module
Options
file : repository file name (without .repo)
name : repository name
description : description text
baseurl : URL of the repo
enabled : 1 to enable, 0 to disable
gpgcheck : 1 to enable GPG check, 0 to disable
Usage Example
ansible all -m yum_repository -a "file=ansible name=AppStream baseurl=http://test.com enabled=1 gpgcheck=0 description='this is test repo file'"4. Yum Module
Options
name : package name
state : present, latest, absent
Usage Examples
# Remove nginx
ansible all -m yum -a 'name=nginx state=absent'
# Install nginx
ansible all -m yum -a 'name=nginx state=present'5. Service Module
Options
name (required): service name
state : started, stopped, restarted, reloaded
enabled : yes/no for boot‑time start
Usage Example
ansible all -m service -a 'name=nginx state=started enabled=yes'6. systemd Module
Options
name : service name
state : started, stopped, restarted, reloaded
daemon_reload : reload daemon if config changed
enabled : yes/no for boot‑time start
Usage Example
ansible all -m systemd -a 'name=nginx state=stopped enabled=no'7. User Module
Options
name : username
state : present or absent
uid : user ID
group : primary group
groups : supplementary groups
comment : description
create_home : yes/no
home : home directory path
shell : login shell
password : hashed password
remove : delete home when removing user
Usage Examples
# Create user (warning shown for plain password)
ansible all -m user -a "name=natasha uid=1234 groups=root shell=/sbin/nologin password=123"
# Delete user and its home directory
ansible all -m user -a 'name=natasha state=absent remove=yes'8. Group Module
Options
gid : group ID
name : group name
state : present or absent
Usage Examples
# Create group
ansible all -m group -a 'name=test gid=2024 state=present'
# Delete group
ansible all -m group -a 'name=test gid=2024 state=absent'9. Fetch Module
Options
src : remote file path (file only)
dest : local destination directory
flat : yes to omit host‑based subdirectory
Usage Examples
# Collect a file preserving host directory
ansible all -m fetch -a 'src=/tmp/hello dest=./'
# Collect a file without host subdirectory
ansible all -m fetch -a 'src=/tmp/hello dest=./ flat=yes'10. get_url Module
Options
url : source URL
dest : remote destination path
mode : file permissions
owner : file owner
group : file group
url_username / url_password : for authenticated downloads
Usage Example
ansible all -m get_url -a "url=https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo dest=/tmp"11. Setup Module
The setup module gathers facts about the remote host; it requires no parameters and is useful for building dynamic playbooks. ansible all -m setup > host_info.yaml Sample output includes IP addresses, architecture, BIOS information, and more.
Link: https://www.cnblogs.com/fsdstudy/p/18258735 (© original author, removed if infringing)
Signed-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.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
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.
