Ansible Common Commands Summary

1. Basic Syntax

ansible <target host or group> -m <module name> -a "<module parameters>"

<span><target host or group></span>: Can be a host, host group in Inventory, or <span>all</span> to indicate all hosts.<span>-m <module name></span>: Specifies the module to use (e.g., <span>ping</span>, <span>shell</span>, <span>yum</span>, etc.).<span>-a "<module parameters>"</span>: Parameters passed to the module.

2. Common Options

Option
Description
<span>-i <Inventory file></span>
Specify a custom Inventory file (default is <span>/etc/ansible/hosts</span>).
<span>-u <username></span>
Specify the login user for the remote host (default is the current user).
<span>-k</span>
Prompt for SSH password (if SSH key is not configured).
<span>--become</span>
Privilege escalation to execute tasks (default uses <span>sudo</span>).
<span>--become-user</span>
Specify the user for privilege escalation (default is <span>root</span>).
<span>-K</span>
Prompt for privilege escalation password (e.g., <span>sudo</span> password).
<span>-f <concurrency></span>
Specify the number of concurrent processes (default is 5).
<span>-v</span>
Display verbose output (<span>-vvv</span> shows more detailed information).
<span>--check</span>
Dry run, simulating execution without actually performing tasks.
<span>--diff</span>
Show differences in files or configurations (usually used with <span>--check</span>).

3. Common Ad-hoc Command Examples

3.1 Check Host Connectivity

ansible all -m ping

3.2 Execute Commands on All Hosts

# Execute uptime command
ansible all -a "uptime"
# Execute shell command
ansible all -m shell -a "df -h"

3.3 Manage Packages

# Install package (using yum module)
ansible webservers -m yum -a "name=httpd state=present"
# Uninstall package
ansible webservers -m yum -a "name=httpd state=absent"

3.4 Manage Services

# Start service
ansible webservers -m service -a "name=httpd state=started"
# Stop service
ansible webservers -m service -a "name=httpd state=stopped"
# Restart service
ansible webservers -m service -a "name=httpd state=restarted"

3.5 File Operations

# Copy file
ansible webservers -m copy -a "src=/local/path/file.txt dest=/remote/path/file.txt"
# Change file permissions
ansible webservers -m file -a "path=/remote/path/file.txt mode=644 owner=root group=root"
# Create directory
ansible webservers -m file -a "path=/remote/path/new_dir state=directory"

3.6 User Management

# Create user
ansible all -m user -a "name=testuser password=123456"
# Delete user
ansible all -m user -a "name=testuser state=absent"

3.7 Cron Job Management

# Add cron job
ansible all -m cron -a "name='backup' minute=0 hour=2 job='/path/to/backup.sh'"
# Delete cron job
ansible all -m cron -a "name='backup' state=absent"

3.8 Privilege Escalation to Execute Tasks

# Execute command using sudo
ansible all --become -a "yum update -y"
# Specify privilege escalation user
ansible all --become --become-user=admin -a "whoami"

3.9 Check Host Information

# Gather host information (using setup module)
ansible all -m setup
# View specific information (e.g., IP address)
ansible all -m setup -a "filter=ansible_default_ipv4"

3.10 Dry Run

# Simulate executing a task
ansible all -m yum -a "name=httpd state=present" --check

4. Advanced Usage

4.1 Using Variables

ansible all -m ping debug -a "msg='Hello, {{ ansible_hostname }}'"

4.2 Limiting Executing Hosts

# Execute only on specific host
ansible web1.example.com -m ping
# Use pattern matching
ansible 'web*' -m ping

4.3 Concurrent Execution

# Specify concurrency to 10
ansible all -m ping -f 10

4.4 Using Tags

# Run tasks with specific tags only
ansible-playbook playbook.yml --tags "apache"

4.5 Using Ansible Vault to Encrypt Data

# Run tasks that require Vault password
ansible all -m debug -a "msg='{{ vault_var }}'" --ask-vault-pass

5. Common Modules

Module Name
Description
<span>ping</span>
Check host connectivity.
<span>shell</span>
Execute shell commands.
<span>command</span>
Execute commands (does not support pipes and redirection).
<span>yum</span>
Manage RPM packages (CentOS/RHEL).
<span>apt</span>
Manage DEB packages (Debian/Ubuntu).
<span>service</span>
Manage system services.
<span>copy</span>
Copy files to remote hosts.
<span>file</span>
Manage files and directories.
<span>user</span>
Manage users.
<span>cron</span>
Manage cron jobs.
<span>setup</span>
Gather host information.
<span>debug</span>
Debug output.

6. Notes

1.Module Selection: Prefer using Ansible modules instead of executing commands directly, as modules have idempotency (consistent results across multiple executions).2.Privilege Operations: If privilege escalation is needed, remember to use <span>--become</span> or <span>-b</span> options.3.Concurrency Control: Control concurrency with <span>-f</span> parameter to avoid overwhelming target hosts.4.Security: Avoid passing sensitive information (like passwords) directly in Ad-hoc commands; use Ansible Vault for encryption.

Editor: HoKoly
Supervisor:HoKoly
Reviewer:HoKoly

Leave a Comment