Ansible adhoc commands

Introduction to Ansible Ad Hoc Commands

Ansible ad hoc commands are powerful one-liners that allow you to execute tasks quickly across multiple servers without writing a playbook. These commands are perfect for performing simple, one-time operations such as checking log files, verifying service status, gathering system information, or making quick configuration changes.

Ad hoc commands are essential tools in every system administrator’s arsenal. They provide immediate results and are ideal for troubleshooting, performing rapid deployments, or testing Ansible modules before incorporating them into playbooks.

Common Ansible Modules for Ad Hoc Commands

In this tutorial, we’ll explore some of the most frequently used Ansible modules with practical examples:

  • user – Manage user accounts
  • file – Manage files and directories
  • copy – Copy files from control node to managed nodes
  • service – Manage system services

1. User Module – Creating User Accounts

How to create a user across multiple servers

The user module allows you to create, modify, and delete user accounts on managed nodes. Here’s an example of creating a user named “ramesh” across all managed hosts:

[ansible@rameshmsr11c ~]$ ansible all -b -m user -a "name=ramesh"
rameshmsr13c | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "append": false, 
    "changed": false, 
    "comment": "", 
    "group": 1008, 
    "home": "/home/ramesh", 
    "move_home": false, 
    "name": "ramesh", 
    "shell": "/bin/bash", 
    "state": "present", 
    "uid": 1007
}
rameshmsr12c | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "append": false, 
    "changed": false, 
    "comment": "", 
    "group": 1008, 
    "home": "/home/ramesh", 
    "move_home": false, 
    "name": "ramesh", 
    "shell": "/bin/bash", 
    "state": "present", 
    "uid": 1007
}
[ansible@rameshmsr11c ~]$ 

Command breakdown:

  • ansible all – Target all hosts in the inventory
  • -b – Execute with sudo (become root)
  • -m user – Use the user module
  • -a "name=ramesh" – Module arguments specifying the username

The output shows that the user was successfully created with UID 1007, home directory /home/ramesh, and default bash shell.

2. File Module – Creating Directories

How to create a directory with specific ownership and permissions

The file module is versatile and can create directories, files, symlinks, and manage their attributes. Here’s how to create a directory with specific owner, group, and permissions:

[ansible@rameshmsr11c ~]$ ansible all -b -m file -a "path=/home/ramesh/test state=directory owner=ramesh group=ramesh"
rameshmsr13c | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "gid": 1008, 
    "group": "ramesh", 
    "mode": "0755", 
    "owner": "ramesh", 
    "path": "/home/ramesh/test", 
    "secontext": "unconfined_u:object_r:user_home_t:s0", 
    "size": 6, 
    "state": "directory", 
    "uid": 1007
}
rameshmsr12c | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "gid": 1008, 
    "group": "ramesh", 
    "mode": "0755", 
    "owner": "ramesh", 
    "path": "/home/ramesh/test", 
    "secontext": "unconfined_u:object_r:user_home_t:s0", 
    "size": 6, 
    "state": "directory", 
    "uid": 1007
}

Command breakdown:

  • path=/home/ramesh/test – Specifies the directory path
  • state=directory – Ensures the path is a directory
  • owner=ramesh group=ramesh – Sets ownership

The output shows “changed”: true, indicating the directory was successfully created with mode 0755 (rwxr-xr-x) permissions.

3. Copy Module – Remote File Distribution

How to copy files from control node to managed nodes

The copy module transfers files from the Ansible control node to remote managed nodes. This is useful for distributing configuration files, scripts, or any other files across your infrastructure:

[ansible@rameshmsr11c ~]$ ansible all -b -m copy -a "src=/home/ansible/test.txt dest=/home/ramesh/test/ owner=ramesh group=ramesh mode=755"
rameshmsr13c | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "9119d1d849d23913e140395d4970185bf839297d", 
    "dest": "/home/ramesh/test/test.txt", 
    "gid": 1008, 
    "group": "ramesh", 
    "md5sum": "06cf090a7cc22bacaf973d9e29c06ed1", 
    "mode": "0755", 
    "owner": "ramesh", 
    "secontext": "unconfined_u:object_r:user_home_t:s0", 
    "size": 16, 
    "src": "/home/ansible/.ansible/tmp/ansible-tmp-1583840916.11-198029487586843/source", 
    "state": "file", 
    "uid": 1007
}
rameshmsr12c | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "9119d1d849d23913e140395d4970185bf839297d", 
    "dest": "/home/ramesh/test/test.txt", 
    "gid": 1008, 
    "group": "ramesh", 
    "md5sum": "06cf090a7cc22bacaf973d9e29c06ed1", 
    "mode": "0755", 
    "owner": "ramesh", 
    "secontext": "unconfined_u:object_r:user_home_t:s0", 
    "size": 16, 
    "src": "/home/ansible/.ansible/tmp/ansible-tmp-1583840916.1-204290785592887/source", 
    "state": "file", 
    "uid": 1007
}

Command breakdown:

  • src=/home/ansible/test.txt – Source file on control node
  • dest=/home/ramesh/test/ – Destination directory on managed nodes
  • owner=ramesh group=ramesh – Sets file ownership
  • mode=755 – Sets file permissions

The output includes checksums (SHA256 and MD5) to verify file integrity after transfer. The “changed”: true status confirms the file was successfully copied.

4. Service Module – Managing System Services

How to start and enable services

The service module controls system services, allowing you to start, stop, restart, reload, or enable/disable services at boot. Here’s how to start and enable the auditd service:

[ansible@rameshmsr11c ~]$ ansible all -b -m service -a "name=auditd state=started enabled=yes"
rameshmsr13c | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "enabled": true, 
    "name": "auditd", 
    "state": "started", 
    "status": {
        "ActiveState": "active",
        "LoadState": "loaded",
        "SubState": "running"
    }
}

Command breakdown:

  • name=auditd – Specifies the service name
  • state=started – Ensures the service is running
  • enabled=yes – Enables the service to start at boot

The output shows “changed”: false because the service was already running. If the service was stopped, you would see “changed”: true.

Additional Useful Ad Hoc Commands

Gathering system information:

# Check disk space
ansible all -m shell -a "df -h"

# Check memory usage
ansible all -m shell -a "free -m"

# Check uptime
ansible all -m command -a "uptime"

# Reboot servers
ansible all -b -m reboot

# Install packages
ansible all -b -m yum -a "name=httpd state=present"

Best Practices for Ad Hoc Commands

  • Use modules instead of shell/command: Ansible modules are idempotent and provide better error handling
  • Test on a single host first: Use ansible hostname instead of ansible all for testing
  • Use -C for dry runs: The –check flag simulates changes without applying them
  • Leverage host patterns: Target specific groups or use patterns like web*
  • Check syntax: Use --syntax-check to validate your commands

Conclusion

Ansible ad hoc commands provide a quick and efficient way to perform system administration tasks across multiple servers. While playbooks are better for complex, repeatable automation, ad hoc commands excel at rapid troubleshooting and one-time operations.

As you become more comfortable with these basic modules, explore the extensive Ansible module library to discover even more powerful automation capabilities. Remember, the key to mastering Ansible is practice – start with simple commands and gradually work your way up to more complex automation scenarios!

Was this article helpful?

RS

About the Author: Ramesh Sundararamaiah

Red Hat Certified Architect

Ramesh is a Red Hat Certified Architect with extensive experience in enterprise Linux environments. He specializes in system administration, DevOps automation, and cloud infrastructure. Ramesh has helped organizations implement robust Linux solutions and optimize their IT operations for performance and reliability.

Expertise: Red Hat Enterprise Linux, CentOS, Ubuntu, Docker, Ansible, System Administration, DevOps

Add Comment