← Back to list

RHCE Ad-hoc Commands: The Complete Study Guide for System Administrators

Master Ansible ad-hoc commands and ace your RHCE certification

LinuxCert Guru · 2025-12-12 14:32 · 1 claps · 7.5 min read
#rhce #rhce-training #rhce-exam #redhat-linux #adhoc-commands
Open on Medium ↗
Wiki topics: ☁️ · DevOps & Cloud 🔓 · Open Source 🥊 · Combat Sports

RHCE Ad-hoc Commands: The Complete Study Guide for System Administrators

Master Ansible ad-hoc commands and ace your RHCE certification

If you’re preparing for your Red Hat Certified Engineer (RHCE) certification, you’ve probably realized that ad-hoc commands are the Swiss Army knife of system administration. They’re fast, powerful, and can save you hours of work when you need to perform quick tasks across multiple systems.

In this comprehensive guide, I’ll walk you through everything you need to know about Ansible ad-hoc commands, from basic syntax to complex real-world scenarios you’ll encounter on the exam.

What Are Ad-hoc Commands and Why Should You Care?

Ad-hoc commands in Ansible let you execute tasks on remote hosts without writing a playbook. Think of them as one-line solutions to everyday problems. Need to check if a service is running on 50 servers? One command. Want to install a package across your entire infrastructure? One command.

The beauty of ad-hoc commands lies in their simplicity and immediacy. They’re perfect for quick one-time tasks, testing and validation, emergency fixes, system checks and monitoring, and learning Ansible modules before incorporating them into playbooks.

Understanding the Basic Syntax

Every ad-hoc command follows this structure:

ansible [pattern] -m [module] -a "[module arguments]" [options]

Let’s break down the essential options you’ll use constantly:

-i: Specify your inventory file -m: The module name you want to use -a: Arguments for that module -u: Remote user to connect as -b: Become root (use sudo) -K: Ask for the sudo password -v: Verbose output (add more v’s for more verbosity) — check: Dry run mode to test without making changes

Here’s what this looks like in practice:

# Simple ping to all hosts
ansible all -m ping
# Install Apache with sudo privileges
ansible all -m yum -a "name=httpd state=present" -b
# Start a service as a specific user
ansible webservers -m service -a "name=httpd state=started" -u admin -b

Mastering Inventory Patterns

Before you can manage systems, you need to target them correctly. Ansible’s pattern system is incredibly flexible:

# Target all hosts
ansible all -m ping
# Target a specific host
ansible server1.example.com -m ping
# Target multiple hosts
ansible server1,server2 -m ping
# Target a group
ansible webservers -m ping
# Exclude hosts (everything except database servers)
ansible all:!dbservers -m ping
# Intersection (webservers that are also in production)
ansible webservers:&production -m ping
# Using wildcards
ansible 'web*.example.com' -m ping
# Using regular expressions
ansible '~web[0-9]+' -m ping

To see which hosts will be affected before running a command, use:

ansible webservers --list-hosts

Gathering System Information

One of the first things you’ll do in any troubleshooting scenario is gather facts about your systems:

# Gather all facts about a system
ansible all -m setup
# Filter for specific information
ansible all -m setup -a "filter=ansible_os_family"
ansible all -m setup -a "filter=ansible_distribution*"
ansible all -m setup -a "filter=ansible_memory_mb"
# Get network information
ansible all -m setup -a "filter=ansible_default_ipv4"
# Check hardware details
ansible all -m setup -a "filter=ansible_devices"

For quick system checks without the setup module:

# Check uptime
ansible all -m command -a "uptime"
# Check disk space
ansible all -m command -a "df -h"
# Check memory usage
ansible all -m command -a "free -h"
# View running processes
ansible all -m command -a "ps aux"

Package Management Made Easy

Whether you’re installing, updating, or removing packages, Ansible makes it straightforward:

# Install a single package
ansible all -m yum -a "name=httpd state=present" -b
# Install multiple packages
ansible all -m yum -a "name=vim,git,wget state=present" -b
# Install a specific version
ansible all -m yum -a "name=httpd-2.4.6 state=present" -b
# Remove a package
ansible all -m yum -a "name=httpd state=absent" -b
# Update a package to the latest version
ansible all -m yum -a "name=httpd state=latest" -b
# Update all packages (use with caution!)
ansible all -m yum -a "name='*' state=latest" -b

To query package information:

# List all installed packages
ansible all -m command -a "rpm -qa"
# Check if a specific package is installed
ansible all -m command -a "rpm -q httpd"
# List files in a package
ansible all -m command -a "rpm -ql httpd"

Service Management with Systemd

Managing services is a daily task for any system administrator:

# Start a service
ansible all -m service -a "name=httpd state=started" -b
# Stop a service
ansible all -m service -a "name=httpd state=stopped" -b
# Restart a service
ansible all -m service -a "name=httpd state=restarted" -b
# Reload a service configuration
ansible all -m service -a "name=httpd state=reloaded" -b
# Enable a service to start on boot
ansible all -m service -a "name=httpd enabled=yes" -b
# Start and enable in one command
ansible all -m service -a "name=httpd state=started enabled=yes" -b

Check service status:

# Check if a service is running
ansible all -m command -a "systemctl status httpd" -b
# Check if a service is active
ansible all -m command -a "systemctl is-active httpd"
# Check if a service is enabled
ansible all -m command -a "systemctl is-enabled httpd"

File and Directory Operations

File management is crucial for configuration and deployment:

# Create an empty file
ansible all -m file -a "path=/tmp/testfile state=touch" -b
# Create a directory with specific permissions
ansible all -m file -a "path=/tmp/testdir state=directory mode=0755" -b
# Remove a file or directory
ansible all -m file -a "path=/tmp/testfile state=absent" -b
# Create a symbolic link
ansible all -m file -a "src=/etc/hosts dest=/tmp/hosts_link state=link" -b
# Change ownership
ansible all -m file -a "path=/tmp/testfile owner=apache group=apache" -b
# Change permissions
ansible all -m file -a "path=/tmp/testfile mode=0644" -b

For copying and modifying file content:

# Copy a file from local to remote
ansible all -m copy -a "src=/local/file dest=/remote/file" -b
# Copy with specific permissions
ansible all -m copy -a "src=/local/file dest=/remote/file owner=root group=root mode=0644" -b
# Create a file with specific content
ansible all -m copy -a "content='Hello World' dest=/tmp/hello.txt" -b
# Add a line to a file
ansible all -m lineinfile -a "path=/etc/hosts line='192.168.1.100 server1.local'" -b
# Replace a line using regex
ansible all -m lineinfile -a "path=/etc/hosts regexp='^127.0.0.1' line='127.0.0.1 localhost'" -b

User and Group Management

Managing users across multiple systems is a breeze:

# Create a user
ansible all -m user -a "name=testuser state=present" -b
# Create a user with specific UID
ansible all -m user -a "name=testuser uid=1500 state=present" -b
# Create a user with home directory
ansible all -m user -a "name=testuser home=/home/testuser createhome=yes" -b
# Add user to groups
ansible all -m user -a "name=testuser groups=wheel,users append=yes" -b
# Remove a user
ansible all -m user -a "name=testuser state=absent" -b
# Remove user and their home directory
ansible all -m user -a "name=testuser state=absent remove=yes" -b

For groups:

# Create a group
ansible all -m group -a "name=testgroup state=present" -b
# Create a group with specific GID
ansible all -m group -a "name=testgroup gid=1500 state=present" -b
# Remove a group
ansible all -m group -a "name=testgroup state=absent" -b

Network Testing and Connectivity

Verify connectivity and network configuration:

# Basic connectivity test
ansible all -m ping
# Test external connectivity
ansible all -m command -a "ping -c 3 google.com"
# Check listening ports
ansible all -m command -a "ss -tuln"
# Test a specific port
ansible all -m wait_for -a "host=google.com port=80 timeout=5"
# Test HTTP connectivity
ansible all -m uri -a "url=http://example.com method=GET"
# Show network interfaces
ansible all -m command -a "ip addr show"
# Show routing table
ansible all -m command -a "ip route show"

Security: Firewall and SELinux

Managing security is critical in any environment:

# Add HTTP service to firewall
ansible all -m firewalld -a "service=http permanent=yes state=enabled immediate=yes" -b
# Add a custom port
ansible all -m firewalld -a "port=8080/tcp permanent=yes state=enabled immediate=yes" -b
# Remove a service from firewall
ansible all -m firewalld -a "service=http permanent=yes state=disabled immediate=yes" -b
# Check SELinux status
ansible all -m command -a "getenforce"
# Set SELinux mode
ansible all -m selinux -a "policy=targeted state=enforcing" -b
# Set SELinux boolean
ansible all -m seboolean -a "name=httpd_can_network_connect state=yes persistent=yes" -b

Troubleshooting and Debugging

When things don’t work as expected, increase verbosity:

# Basic verbose output
ansible all -m ping -v
# More detailed output
ansible all -m ping -vv
# Maximum verbosity
ansible all -m ping -vvv

Always test before making changes:

# Dry run (check mode)
ansible all -m yum -a "name=httpd state=present" --check -b
# Show what would change
ansible all -m copy -a "src=file.txt dest=/tmp/file.txt" --check --diff -b

Real-World Exam Scenarios

Scenario 1: Setting Up a Web Server

# Install Apache
ansible webservers -m yum -a "name=httpd state=present" -b
# Start and enable the service
ansible webservers -m service -a "name=httpd state=started enabled=yes" -b
# Configure firewall
ansible webservers -m firewalld -a "service=http permanent=yes state=enabled immediate=yes" -b
# Deploy a simple index page
ansible webservers -m copy -a "content='<h1>Welcome</h1>' dest=/var/www/html/index.html" -b

Scenario 2: Creating an Administrative User

# Create the user with home directory
ansible all -m user -a "name=webadmin home=/home/webadmin createhome=yes shell=/bin/bash" -b
# Add to wheel group for sudo access
ansible all -m user -a "name=webadmin groups=wheel append=yes" -b
# Add SSH key for passwordless login
ansible all -m authorized_key -a "user=webadmin key='ssh-rsa AAAAB...'" -b

Scenario 3: Security Hardening

# Update all packages
ansible all -m yum -a "name='*' state=latest" -b
# Disable root SSH login
ansible all -m lineinfile -a "path=/etc/ssh/sshd_config regexp='^#?PermitRootLogin' line='PermitRootLogin no'" -b
# Disable password authentication
ansible all -m lineinfile -a "path=/etc/ssh/sshd_config regexp='^#?PasswordAuthentication' line='PasswordAuthentication no'" -b
# Restart SSH to apply changes
ansible all -m service -a "name=sshd state=restarted" -b
# Set SELinux to enforcing
ansible all -m selinux -a "policy=targeted state=enforcing" -b

Quick Reference: Your Go-To Commands

Keep these handy for quick access:

# System check
ansible all -m ping
# Gather facts
ansible all -m setup
# Install package
ansible all -m yum -a "name=package state=present" -b
# Start service
ansible all -m service -a "name=service state=started" -b
# Copy file
ansible all -m copy -a "src=local dest=remote" -b
# Create user
ansible all -m user -a "name=user state=present" -b
# Add firewall rule
ansible all -m firewalld -a "service=http state=enabled" -b

Emergency Commands (Use with Caution!)

# Emergency reboot
ansible all -m reboot -b
# Kill a runaway process
ansible all -m command -a "pkill process_name" -b
# Check disk space quickly
ansible all -m command -a "df -h"
# Check system load
ansible all -m command -a "uptime"

Tips for Exam Success

Practice regularly: Set up a lab environment and run these commands daily until they become second nature.

Always test first: Use --check mode before running commands that make changes.

Use verbose mode: When troubleshooting, verbose output can reveal exactly what’s happening.

Understand the modules: Don’t just memorize commands. Understand what each module does and its parameters.

Know your patterns: Being able to target the right hosts quickly is crucial.

Time management: Ad-hoc commands are faster than playbooks for simple tasks, so know when to use each.

Read error messages: Ansible’s error messages are usually quite informative. Don’t skip past them.

Final Thoughts

Ad-hoc commands are an essential skill for any system administrator, and they’re a significant part of the RHCE exam. The key to mastering them is practice and understanding. Don’t just memorize the commands, understand what they do and why. Start with simple commands like ping and setup to verify connectivity and gather information. Build up to more complex operations like installing packages, managing services, and configuring security. Before you know it, you’ll be managing entire infrastructures with single commands.

Remember, with great power comes great responsibility. Always test in a safe environment first, use check mode liberally, and understand the impact of your commands before running them in production.

Good luck with your RHCE certification! With consistent practice and this guide as your reference, you’ll be well-prepared for success.

What’s your experience with Ansible ad-hoc commands? Share your favorite commands or scenarios in the comments below!


메타데이터
post_id
82fe79addbfe
slug
rhce-ad-hoc-commands-the-complete-study-guide-for-system-administrators-82fe79addbfe
url
https://medium.com/@linuxcertguru/rhce-ad-hoc-commands-the-complete-study-guide-for-system-administrators-82fe79addbfe
canonical_url
https://medium.com/@linuxcertguru/rhce-ad-hoc-commands-the-complete-study-guide-for-system-administrators-82fe79addbfe
author_url
https://medium.com/@linuxcertguru
status
ok
fetched_at
2026-07-14 06:31:06