Ansible is an automation tool for operations management based on Python.
Ansible is an automation tool developed in Python that communicates with remote hosts using the SSH protocol, eliminating the need to install clients or agents on the managed end. Its core functionalities include batch system configuration, program deployment, command execution, and multi-task orchestration, supporting complex operational tasks through modular design and Playbooks. Ansible is known for its simplicity in deployment and strong scalability, offering a large number of built-in modules (such as file management, service control, etc.) and supporting custom development. As of 2023, this tool has been acquired by Red Hat and has become one of the most recognized solutions in the field of automation operations.
Ansible is a tool used to automate the management of remote operating systems. It connects to remote hosts via the SSH protocol and executes tasks without the need to install a client on the remote host. The following table summarizes the core steps to quickly get started with Ansible:
|
Step |
Key Content |
Brief Description/Example |
|
1. Installation |
System package manager or pip |
Ubuntu: sudo apt install ansible;CentOS: sudo yum install ansible;pip: python3 -m pip install ansible –user |
|
2. Configure Host Inventory |
Define managed hosts |
Example:[web_servers] 192.168.1.101 ansible_ssh_user=root 192.168.1.102 ansible_ssh_user=root |
|
3. Set Up SSH Passwordless Login |
Key connection between management and controlled machines |
ssh-copy-id user@remote_host |
|
4. Execute Temporary Commands |
ansible ad-hoc commands |
ansible all -i inventory -m ping Test connectivity;ansible web_servers -m apt -a “name=nginx state=present” Install software |
|
5. Write Playbook |
YAML formatted automation script |
See the basic example of Playbook below |
🔧Installation and Verification
First, install Ansible on your control machine (usually your own computer or a server). Depending on your operating system, you can choose different installation methods:
- Install using system package manager (suitable for Linux systems):
bash
# Ubuntu/Debian
sudo apt update && sudo apt install ansible:cite[2]:cite[4]
# CentOS/RHEL
sudo yum install epel-release && sudo yum install ansible:cite[5]
- Install using pip (suitable for more platforms):
bash
python3 -m pip install ansible –user:cite[1]
After installation, you can verify with the following command:
bash
ansible –version:cite[2]
📝Configure Host Inventory and SSH
The host inventory file tells Ansible which machines need to be managed.
- Create a host inventory file: You can create a file named inventory (the filename and location can be customized) and fill in your remote host information in the following format:
ini
[web_servers]
192.168.1.101 ansible_ssh_user=root
192.168.1.102 ansible_ssh_user=root
[db_servers]
192.168.1.103 ansible_ssh_user=root
- [web_servers] is a host group for unified operations on a batch of hosts.
- You can specify connection parameters for hosts, such as ansible_ssh_user (login user).
bash
ssh-copy-id your_user@remote_host_ip
After executing this command and entering the user password for the remote host, subsequent SSH connections (including Ansible operations) will no longer require manual password input.
🚀Execute Temporary Commands
Temporary commands ( ad-hoc commands) are suitable for quickly executing simple tasks, with the basic format: ansible -i < inventory file> < host or group> -m < module> -a “< parameters>”.
- Test host connectivity (using ping module):
bash
ansible -i inventory all -m ping:cite[1]
If successful, you will see a return similar to “ping”: “pong” .
- Execute Shell commands on all hosts (using command module):
bash
ansible -i inventory web_servers -m command -a “free -h”:cite[7]
- Install packages (using apt module, Ubuntu/Debian):
bash
ansible -i inventory web_servers -m apt -a “name=nginx state=present”:cite[8]
📒Using Playbook
Playbook is Ansible’s automation script, written in YAML format, capable of defining complex, multi-step task flows.
A simple example of a Playbook (setup_nginx.yml) is as follows, used to install Nginx on specified host groups:
yaml
—
– hosts: web_servers # Specify which host group to execute tasks on
remote_user: root # Specify remote login user
become: yes # Indicates permission escalation is allowed:cite[2]
tasks: # Define task list
– name: Ensure Nginx is installed # Task description
apt: # Use apt module (for Ubuntu/Debian)
name: nginx # Package name to install
state: present # Ensure the package is installed:cite[2]
– name: Start Nginx service and enable it at boot # Task description
service: # Use service module
name: nginx
state: started # Ensure the service is in a running state
enabled: yes # Ensure the service starts on boot:cite[7]
…
Run this Playbook:
bash
ansible-playbook -i inventory setup_nginx.yml
After executing the Playbook, it will display detailed results and status summaries for each task.
🔍Core Concepts and Techniques
- Modular Design: Ansible executes specific tasks through modules. In addition to the ones used above, such as ping, command, apt, service, there are many other modules like copy (copy files), user (manage users), template (generate configuration files), etc. You can use ansible-doc -l to view all modules.
- Idempotence: This is a very important feature of Ansible. It means that the result of executing a Playbook multiple times is consistent. For example, if the software is already installed, Ansible will not execute the installation operation again.
- Get Detailed Output: When executing a Playbook, you can add -v parameter (-vvv or -vvvv for more detailed information) to see a more detailed execution process, which is very useful for debugging.
🎯Advanced Learning Directions
Once you are familiar with the basic operations, you can further explore the following topics to enhance your Ansible skills:
- Using Variables: Define and use variables in Playbooks (using the vars keyword) to make Playbooks more flexible.
- Using Handlers: Operations triggered by tasks, usually used for service restarts.
- Using Roles: Organize Playbooks into independent directory structures by functionality (such as variables, tasks, files, etc.) for code reuse and organization.
I hope this guide helps you quickly get started with Ansible! If you have further interest in a specific module (such as the template module for configuration files) or more complex Playbook writing, I would be happy to provide a more detailed introduction.
#Ansible
#Automation Tools