$linuxjunkies
>

ansible(1)

Run ad hoc commands and playbooks across remote hosts using SSH without requiring agents.

UbuntuDebianFedoraArch

Synopsis

ansible [HOST-PATTERN] [OPTIONS] -m [MODULE]

Description

Ansible is an agentless automation platform that executes commands and orchestrates configuration management, application deployment, and task execution across multiple remote systems. It uses SSH for transport and Python for execution, requiring no special daemons or agents on target hosts.

The command-line tool allows running ad hoc operations against inventory hosts using modules, while playbooks provide structured, reusable automation workflows. Ansible reads from an inventory file (default: /etc/ansible/hosts) to determine target hosts and their connection details.

Common options

FlagWhat it does
-i INVENTORYspecify inventory file or dynamic inventory script (comma-separated hosts also work)
-m MODULEspecify the module to execute (default: command)
-a ARGSpass arguments to the module
-u USERNAMEconnect as this user (default: current user or ansible_user from inventory)
-kprompt for SSH password
-Kprompt for sudo password when using become
-b, --becomerun operations with become (escalate privileges, usually sudo)
-e VARSset additional variables as key=value or load from JSON file
-f NUMnumber of parallel processes to use (default: 5)
-vincrease verbosity (use -vvv or -vvvv for more details)
--syntax-checkcheck playbook syntax without executing
--checkrun in dry-run mode, no changes are made

Examples

test connectivity to all hosts defined in inventory.ini using the ping module

ansible all -i inventory.ini -m ping

run the uptime command on all hosts in the 'webservers' group

ansible webservers -m command -a 'uptime'

copy a file from the control machine to /tmp/backup/ on localhost

ansible localhost -m copy -a 'src=/tmp/file.txt dest=/tmp/backup/'

restart postgresql service with sudo privileges on all db_servers

ansible db_servers -b -m service -a 'name=postgresql state=restarted'

run disk usage command on a specific IP with SSH password prompt as ubuntu user

ansible 192.168.1.100 -u ubuntu -k -m shell -a 'df -h'

execute playbook in dry-run mode with verbose output to see what would change

ansible-playbook site.yml -i hosts.ini -v --check

gather and display OS family facts for all hosts

ansible all -m setup -a 'filter=ansible_os_family'

install nginx package in parallel (20 processes) on all hosts matching prod_* with sudo

ansible prod_* -f 20 -m apt -a 'name=nginx state=present' -b

Related commands