LinuxCommandLibrary

ansible

Automate configuration management and application deployment

TLDR

List hosts belonging to a group

$ ansible [group] --list-hosts
copy

Ping a group of hosts by invoking the ping module
$ ansible [group] [[-m|--module-name]] ping
copy

Display facts about a group of hosts by invoking the setup module
$ ansible [group] [[-m|--module-name]] setup
copy

Execute a command on a group of hosts by invoking command module with arguments
$ ansible [group] [[-m|--module-name]] command [[-a|--args]] '[my_command]'
copy

Execute a command with administrative privileges
$ ansible [group] [[-b|--become]] --ask-become-pass [[-m|--module-name]] command [[-a|--args]] '[my_command]'
copy

Execute a command using a custom inventory file
$ ansible [group] [[-i|--inventory]] [inventory_file] [[-m|--module-name]] command [[-a|--args]] '[my_command]'
copy

List the groups in an inventory
$ ansible localhost [[-m|--module-name]] debug [[-a|--args]] '[var=groups.keys()]'
copy

SYNOPSIS

ansible [-i INVENTORY] [-m MODULE] [-a MODULE_ARGS] [options] []

PARAMETERS

-a MODULE_ARGS, --args MODULE_ARGS
    Module arguments.

-b, --become
    Run tasks with become (sudo).

-c CONNECTION, --connection CONNECTION
    Connection type (default=ssh).

--check
    Dry-run mode; simulate changes.

-C CONVERGENCE, --convergence CONVERGENCE
    Control node convergence (default=smart).

-f FORKS, --forks FORKS
    Number of parallel processes (default=5).

-i INVENTORY, --inventory INVENTORY
    Inventory file or host list.

--list-hosts
    List matching hosts; no execution.

-m MODULE, --module-name MODULE
    Module to run (default=command).

--one-line
    Concise output for each host.

-t TREE, --tree TREE
    Save facts/output in directory.

-u USER, --user USER
    Remote user (default=current).

-k, --ask-pass
    Prompt for SSH password.

--private-key PRIVATE_KEY_FILE
    Private key for authentication.

-s, --sudo
    Run as sudo (deprecated; use --become).

-K, --ask-become-pass
    Prompt for become password.

-e EXTRA_VARS, --extra-vars EXTRA_VARS
    Set variables.

-v, -vv, -vvv
    Verbose mode (-vvv most verbose).

--syntax-check
    Perform syntax check on module.

-h, --help
    Show help.

--version
    Show version.

DESCRIPTION

Ansible is an open-source platform for automating IT infrastructure, configuration management, application deployment, and orchestration. The ansible command enables ad-hoc execution of modules on remote hosts via SSH, without requiring agents on managed nodes. It uses a simple declarative language based on YAML for defining tasks. Specify target hosts or groups from an inventory file, select a module (e.g., ping, apt, shell), and pass arguments. Supports privilege escalation, dry-run checks, parallel execution, and integration with cloud providers. Ideal for DevOps workflows, it scales from single servers to thousands of nodes, promoting idempotency and human-readable automation. For complex scenarios, use playbooks via ansible-playbook. Requires Python on the control machine and targets.

CAVEATS

Requires SSH access and Python on targets.
Large inventories may need dynamic inventory scripts.
Ad-hoc mode unsuitable for complex, repeatable tasks (use playbooks).
Default inventory: /etc/ansible/hosts.

INVENTORY FORMAT

INI or YAML files defining hosts/groups.
Example: webservers ansible_host=192.168.1.10.

COMMON MODULES

ping: connectivity test.
setup: gather facts.
shell/command: run commands.
apt/yum: package management.
service: manage services.

EXAMPLE USAGE

ansible webservers -m ping
ansible all -m apt -a 'name=nginx state=present' -b

HISTORY

Created by Michael DeHaan in 2012 as open-source project.
Acquired by Red Hat in 2015; now core of Ansible Automation Platform.
Version 2.0 (2016) introduced major refactoring for blocks/handlers.

SEE ALSO

Copied to clipboard