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 <host-pattern> [-i INVENTORY] [-m MODULE_NAME] [-a MODULE_ARGS] [options]

PARAMETERS

-i INVENTORY, --inventory=INVENTORY
    Specify inventory host file or script.

-m MODULE_NAME, --module-name=MODULE_NAME
    Specify the Ansible module to execute (default: command).

-a MODULE_ARGS, --args=MODULE_ARGS
    Provide arguments to the module.

-u REMOTE_USER, --user=REMOTE_USER
    Connect as this user.

-b, --become
    Run operations with become (e.g., sudo) privileges.

-k, --ask-pass
    Ask for connection password (e.g., SSH password).

-K, --ask-become-pass
    Ask for become (sudo) password.

-e VARS, --extra-vars=VARS
    Set additional variables as key=value or YAML/JSON string.

-l SUBSET, --limit=SUBSET
    Limit selected hosts to a subset specified by a pattern.

-f FORKS, --forks=FORKS
    Specify the number of parallel processes to use (default: 5).

--list-hosts
    List all hosts that match the pattern, without executing any module.

-v, --verbose
    Enable verbose output (use multiple -v for more verbosity).

--version
    Show program's version number and exit.

DESCRIPTION

The ansible command is a fundamental part of the Ansible automation engine, designed for executing quick, one-off tasks against a group of managed hosts. Unlike ansible-playbook, which orchestrates complex, multi-step workflows defined in YAML playbooks, ansible is used for immediate execution of specific modules. It allows administrators to perform actions such as checking host connectivity, gathering facts, managing packages, restarting services, or running shell commands across an entire inventory or a subset of it, all without needing to write a playbook. This makes it ideal for rapid troubleshooting, administrative tasks, or verifying configurations across a large infrastructure. It operates over SSH (by default), requiring no agents on the remote machines, leveraging their existing Python installations.

CAVEATS

  • While powerful for ad-hoc tasks, ansible is not designed for complex, multi-step orchestration or configuration management; for those, ansible-playbook is the appropriate tool.
  • Requires Python (version 2.7 or 3.5+) on the managed nodes to execute modules.
  • Without proper inventory and host patterns, commands might target unintended hosts.

HOST PATTERN

The <host-pattern> argument is crucial, specifying which hosts from your inventory the command should run against. It can be 'all', a specific host, a group name, or a complex pattern (e.g., 'webservers:&production' for hosts in both groups).

MODULES

Ansible's functionality is driven by modules. The -m option allows you to specify which module to use (e.g., ping, shell, apt, yum, service, copy). Modules are designed to be idempotent where applicable, meaning running them multiple times results in the same final state without unintended side effects. The default module if not specified is command.

HISTORY

Ansible was created by Michael DeHaan, the author of Cobbler and Func, and was first released in 2012. It quickly gained popularity due to its simplicity, agentless architecture, and use of human-readable YAML for automation. In 2015, Red Hat acquired Ansible, integrating it into its enterprise offerings and further accelerating its development and adoption within the open-source community and enterprises alike. The ansible command has been a core component since the project's inception, serving as the immediate interface for quick interactions with managed hosts.

SEE ALSO

ansible-playbook(1), ansible-vault(1), ansible-galaxy(1), ansible-config(1)

Copied to clipboard