Ansible Ad hoc Command Introduction

Ansible Ad hoc command is very easy to understand the basic on Ansible. Ad hoc command helps you to perform small tasks on group of servers. Tasks like reboot, restart any service, change file permission etc on bunch of servers. To perform any action using Ad hoc you need to use ‘/usr/bin/ansible’ command.

Ad hoc Command


ansible  [-m ] -a <"arguments"> -u  [--become]

See Also:

Hosts: Here hosts mean bunch of servers which we will define in Ansible inventory file. If we wan to perform and action to all hosts from inventory, use all or ‘*’.
module_name: There are many modules available in Ansible. To list all the available modules execute mentoin command.

# ansible-doc -l

arguments: We need to pass the value according to your module and the value will be change as per your module.
username: We will specifies the ansible user from which we want to execute the command.
become: When we want to execute any operation with sudo privileges then we use become. By default value of become is false.

Note: If you will use -c option, then Ansible will do a dry run of the command. It will not execute or apply on the client nodes.

Some Ansible module example with output:

ping module:

# ansible ansible-host -m ping | SUCCESS => {
"changed": false,
"ping": "pong"

command module:

# ansible ansible-host -m command -a "df -h" | CHANGED | rc=0 >>
Filesystem Size Used Avail Use% Mounted on
18G 1.1G 16G 7% /
tmpfs 499M 0 499M 0% /dev/shm
/dev/sda1 477M 52M 400M 12% /boot

Note: Use can also define the variables in command.

# ansible ansible-host -m command -a "reboot" -u ansible --become | CHANGED | rc=0 >>

yum module:

# ansible ansible-host -m yum -a "name=telnet state=present update_cache=true" -u ansible --become | CHANGED => {
"ansible_facts": {
"pkg_mgr": "yum"
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Loaded plugins: fastestmirror\nSetting up Install Process\nLoading mirror speeds from cached hostfile\n * base:\n * extras:\n * updates:\nResolving Dependencies\n--> Running transaction check\n---> Package telnet.x86_64 1:0.17-48.el6 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n telnet x86_64 1:0.17-48.el6 base 58 k\n\nTransaction Summary\n================================================================================\nInstall 1 Package(s)\n\nTotal download size: 58 k\nInstalled size: 109 k\nDownloading Packages:\nRunning rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\n\r Installing : 1:telnet-0.17-48.el6.x86_64 1/1 \n\r Verifying : 1:telnet-0.17-48.el6.x86_64 1/1 \n\nInstalled:\n telnet.x86_64 1:0.17-48.el6 \n\nComplete!\n"

service module:

# ansible ansible-host -m service -a "name=httpd state=started" -u ansible --become | CHANGED => {
"changed": true,
"name": "httpd",
"state": "started"

lineinfile module: This module ensures a particular line is in a file, and create or replace an existing line using a back-referenced regular expression.

# ansible ansible-host -m lineinfile -a "path=/tmp/ansiblefile line='' create=yes" -u ansible --become | CHANGED => {
"backup": "",
"changed": true,
"msg": "line added"

copy module: The `copy’ module copies a file from the local or remote machine.

# ansible ansible-host -m copy -a "src=/etc/ansible/ansible.cfg dest=/etc/ansible.cfg mode=644" -u ansible --become | CHANGED => {
"changed": true,
"checksum": "cab8254a5f8846081a4791e6dc798e06e36ad1e7",
"dest": "/etc/ansible.cfg",
"gid": 0,
"group": "root",
"md5sum": "389ac708a8764a828171e30caaac75c4",
"mode": "0644",
"owner": "root",
"size": 20276,
"src": "/home/ansible/.ansible/tmp/ansible-tmp-1551952500.63-13664888994034/source",
"state": "file",
"uid": 0

file module: Sets attributes of files, symlinks, and directories, or removes files/symlinks/directories.

# ansible ansible-host -m file -a "dest=/home/ansi mode=775 state=directory" -u ansible --become | CHANGED => {
"changed": true,
"gid": 0,
"group": "root",
"mode": "0775",
"owner": "root",
"path": "/home/ansi",
"size": 4096,
"state": "directory",
"uid": 0

To know more about modules, you can check ansible module documentation.

Enjoy it!

No Responses

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.