Home > Package & System Management > ansible

ansible: Remote Server Management and Automation

Ansible is an open-source automation engine that performs IT automation tasks such as software provisioning, configuration management, application deployment, and orchestration. It connects to remote servers via SSH without needing agents and executes tasks using YAML-based playbooks.

Overview

Ansible operates in an agentless manner, meaning no separate software needs to be installed on the managed servers. It executes commands via SSH connections and uses Python-based modules to perform various tasks. Managed hosts are defined through an inventory file, and tasks can be automated using ad-hoc commands or playbooks.

Key Features

  • Agentless operation (no agents required on managed servers)
  • SSH-based communication (leverages existing infrastructure)
  • Uses YAML playbooks (concise and readable automation scripts)
  • Various IT automations including configuration management, application deployment, and orchestration

Key Options

Ansible commands are used to execute specific modules or perform ad-hoc commands on groups of hosts defined in an inventory file.

Basic Execution Options

Information and Debugging

Generated command:

Try combining the commands.

Description:

`ansible` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

You can perform various tasks on remote servers using Ansible commands.

Check Connectivity to All Hosts (ping)

ansible all -m ping

Tests connectivity to all hosts defined in the inventory.

Execute Command on Web Server Group

ansible webservers -a "uptime"

Executes the 'uptime' command on hosts belonging to the 'webservers' group in the inventory.

Install Package on Specific Host (using sudo)

ansible node1 -b -m apt -a "name=nginx state=present"

Installs the 'nginx' package on the node1 host. Uses sudo privileges with the -b option.

Use Custom Inventory File

ansible all -i my_inventory.ini -a "df -h"

Executes the 'df -h' command on all hosts using 'my_inventory.ini' instead of the default inventory file.

Copy File Specifying Remote User

ansible webservers -u deploy_user -m copy -a "src=./index.html dest=/var/www/html/"

Copies the local 'index.html' file to the '/var/www/html/' path on the remote server, connecting as the 'deploy_user'.

Installation

Ansible is not included by default in most Linux distributions, so it needs to be installed via a package manager or Python pip. Installation via Python pip is the most recommended method.

Installation using Python pip (Recommended)

If Python is installed, you can install Ansible using pip.

  • Install Command: pip install ansible
  • Upgrade Command: pip install --upgrade ansible

Ubuntu/Debian

Install using the APT package manager.

  • Install Command: sudo apt update && sudo apt install ansible

CentOS/RHEL

Install using the YUM/DNF package manager. The EPEL repository may be required.

  • Install EPEL Repository: sudo yum install epel-release
  • Install Command: sudo yum install ansible

Tips & Precautions

Tips and points to consider for effective Ansible usage.

Ansible Usage Tips

  • **Inventory File Management**: It is crucial to systematically group and define managed hosts. You can use `ini` or `yaml` formats.
  • **Playbook Recommendation**: For anything beyond simple ad-hoc commands, it is recommended to use YAML-based playbooks (`ansible-playbook`) for reusability, readability, and version control.
  • **Understand Idempotency**: Ansible is designed so that executing a task multiple times results in the same system state. This is called idempotency, and this concept should be leveraged when writing automation scripts.
  • **SSH Key-Based Authentication**: It is recommended to enhance security and streamline the automation process by using SSH keys instead of passwords.
  • **Use Vault**: Sensitive information (passwords, API keys, etc.) should be encrypted and managed using Ansible Vault.

Same category commands