Ansible ad hoc commands can be used when we have to perform a task quickly one a node or a group of nodes. Tasks can be executed in a single line of command without worrying about the YAML syntax.
Ansible modules can be called in the command based on the task we have to perform. Below are few ad hoc command examples.
1$ ansible node -m ping
This command is used to find if the managed node is reachable from control machine. Here we have used ping module.
1$ ansible node -m shell -a uptime
This command is used with shell module to find the up time of the node.
1$ ansible node -m file -a "dest=/tmp/managed_node mode=755 state=directory"
This can be used to create a folder in the node using file module.
1$ ansible node -m copy -a "src=/tmp/index.html dest=/tmp/managed_node/index.html"
This command can be used to copy file from control node to the node using copy module.
1$ ansible node -m yum -a "name=httpd state=present"
This can be used to install httpd server on node using yum module.
1$ ansible node -m service -a "name=httpd state=started"
This can be used to start httpd service using service module.
Similarly, ad hoc commands can be used with any module to perform the required task on a single node or a group of nodes.