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 modules written in Python 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 automation tasks including configuration management, application deployment, and orchestration
Main Options
Ansible commands are used to execute specific modules or perform ad-hoc commands on host groups defined in the inventory file.
Basic Execution Options
Information and Debugging
Erzeugter Befehl:
Kombinieren Sie die Befehle.
Beschreibung:
`ansible` Führen Sie den Befehl aus.
Kombinieren Sie diese Optionen und führen Sie die Befehle virtuell zusammen mit der KI aus.
Usage Examples
You can perform various tasks on remote servers using Ansible commands.
Check Connectivity to All Hosts (ping)
ansible all -m ping
Tests the 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 host 'node1'. 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 remote user '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.
- Installation Command: pip install ansible
- Upgrade Command: pip install --upgrade ansible
Ubuntu/Debian
Install using the APT package manager.
- Installation 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
- Installation Command: sudo yum install ansible
Tips & Precautions
Tips and points to note for effective Ansible usage.
Ansible Usage Tips
- **Inventory File Management**: It is important 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 running tasks multiple times results in the same system state. This is called idempotency, and this concept should be utilized 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 managed by encrypting it using Ansible Vault.