Ansible: Ad-hoc Commands

This blog receives a commission from affiliate partners through the affiliate links that we share with you in our posts. Although we receive a commission for using and linking their products, all our opinions and suggestions are unbiased.

Ad-hoc command modules and how to use it?

Ad-hoc Commands:

Ad-hoc commands will perform quick tasks and not save that certain command for a later stage. In the commands below “-a” indicates that the command is an ad-hoc command.

See the list of nodes from the master server.

#ansible ubnt –m command –a ls/

Create a demo file in node2 from the server.

#ansible  cent –m  raw  –a ‘touch  /tmp/demo.txt’

Install Java on node2 from the server.

# ansible cent –m  raw  -a  ‘yum install –y java’


#ansible cent  -m  yum  -a  ‘name=java  state=latest’

Start/manage services.

#ansible  cent  -m  service  -a  ‘name=httpd   state=started/restarted’

Copy a file from ansible server to nodes.

#ansible all –m  copy  -a  “src= ‘/root/devops.txt’  dest=’/tmp/devops.txt’”

Create a file.

#ansible all –m  file  -a “name=/opt/sample.txt  state=touch”

Remove a file.

#ansible all –m  file  -a “name=/opt/sample.txt  state=absent”

Create a directory.

#ansible all –m  file  -a “name=/opt/devops  state=directory”

Venkata Narayana Yarava

Venkata Narayana Yarava is an experienced Cloud DevOps Engineer with exposure to various technologies, he has a experience in DevOps Training and with different IT environments. He has a huge passion for automation technologies, specifically DevOps Tools, OpenStack, AWS, Linux, open source software. If you enjoy our articles please support us and make a small contribution: Buy Us A Coffee