Skip to content

Ansible ad hoc command examples

homepage-banner

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.

Command

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

[demo:children]
nginx
mysql

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

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

Ansible.cfg demo

[defaults]
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
allow_world_readable_tmpfiles=true

[ssh_connection]
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

Reference

  • https://docs.ansible.com/ansible/latest/index.html
  • https://docs.ansible.com/ansible/latest/command_guide/cheatsheet.html
Leave a message