Skip to content

Ansible ad hoc command examples


Ansible is a powerful automation tool that allows for easy configuration management, application deployment, and task automation. One of the most useful features of Ansible is its ad hoc command capability. Ad hoc commands allow you to quickly run a single command on one or multiple remote hosts without the need for a playbook. In this blog post, we will explore some Ansible ad hoc command examples.


ansible group001 -i hosts.ip -m shell -a "run some shell command" --become -f 1 -v

ansible-playbook -i /path/to/my_inventory_file -u my_connection_user -k -f 3 -T 30 -t my_tag -m /path/to/my_modules -b -K my_playbook.yml

Inventory demo


[nginx] ansible_ssh_user=ubuntu ansible_ssh_port=22 ansible_python_interpreter=/usr/bin/python3

[mysql] ansible_ssh_user=ubuntu ansible_ssh_port=22 ansible_python_interpreter=/usr/bin/python3

Ansible.cfg demo

private_key_file = ~/.ssh/id_rsa
host_key_checking = False
forks = 300
timeout = 40
deprecation_warnings = False
roles_path = ansible/roles
inventory_plugins = ansible/plugins/inventory

ssh_args = -o ForwardAgent=yes -o ControlMaster=auto -o ControlPersist=600s -o StrictHostKeyChecking=no
retries = 5
pipelining = True
control_path = /tmp/ansible-ssh-%%h-%%p-%%r



Back to Table of Contents

  1. License under CC BY-NC 4.0
  2. Copyright issue feedback, replace # with @
  3. Not all the commands and scripts are tested in production environment, use at your own risk
  4. No personal information is collected.