Introduction
In modern operations and IT management, the use of automation tools is becoming increasingly widespread. Ansible, as a lightweight and easy-to-use automation tool, has gained favor among many operations engineers and developers.
1. Overview of Ansible
Ansible is an automation tool developed in Python, primarily used for configuration management, application deployment, and task orchestration. It communicates with remote hosts via the SSH protocol, allowing efficient automated management without the need to install a client on the remote host.
1. Main Functions of Ansible include:
- Configuration Management: Ensuring consistency in server and application configurations.
- Application Deployment: Automating the deployment of applications to multiple servers.
- Task Orchestration: Defining and executing complex operational task flows.
- Continuous Delivery: Integrating into Continuous Integration/Continuous Delivery (CI/CD) processes for automated delivery.
2. Applicable Scenarios:
- Batch configuration and management of servers.
- Automated deployment of applications.
- System monitoring and log collection.
- Database management and backup.
- Network device configuration (requires specific modules).
2. Installing Ansible
The installation of Ansible is very simple and supports various installation methods. Here are the common installation methods:
1. Install using a package manager (example for Ubuntu)
1sudo apt update
2sudo apt install ansible
2. Install via pip
1pip install ansible
After installation, you can verify the version of Ansible with the following command:
1ansible --version
3. Editing the Hosts File
The hosts file is a list file used by Ansible to define the hosts or host groups to be managed. By default, the hosts file is located at<span>/etc/ansible/hosts</span>
.
1. Defining Host Groups
In the hosts file, you can define host groups using square brackets<span>[]</span>
, and then list the IP addresses or hostnames of the hosts belonging to that group under the group name.
Example:
1[web_servers]
2192.168.1.101
3192.168.1.102
4
5[db_servers]
6db01.example.com
7db02.example.com
8
9[all:vars]
10ansible_ssh_user=root
11ansible_ssh_pass=your_password
<span>[web_servers]</span>
and<span>[db_servers]</span>
are defined host groups.<span>[all:vars]</span>
is used to define common variables for all hosts, such as SSH username and password.
2. Using a Custom Hosts File
When executing Ansible commands, you can specify a custom hosts file using the<span>-i</span>
parameter:
1ansible -i custom_hosts.ini all -m ping
4. Remote Batch Command Execution
Ansible provides various modules to execute remote commands, commonly used are the<span>command</span>
module and the <span>shell</span>
module. Here are some common examples of remote batch command execution:
1. Testing Host Connectivity
Use the<span>ping</span>
module to test connectivity to all hosts:
1ansible all -m ping
2. Creating Files on All Hosts
Use the<span>command</span>
module to create files on all hosts:
1ansible all -m command -a "touch /root/test-ansible.txt"
2 -m: specifies the module to call
3 -a: parameters passed to the module, which can be omitted if not needed; parameters should be enclosed in quotes
4 -i: specifies the inventory path for this execution, if this parameter is specified, do not add host groups/hosts afterwards
3. Operating on Specific Hosts
Test connectivity for specific hosts:
1ansible slave1,slave2 -m ping
4. Executing Commands on Specific Hosts
Execute commands on specific hosts (for example, to create a file):
1ansible slave1,slave2 -m command -a "touch /root/test-ansible.txt"
With the above commands, you can easily perform batch operations on multiple hosts, whether testing connectivity, creating files, or executing other commands, Ansible provides an efficient and convenient solution.
5. Conclusion
Through this article, you should have grasped the basic concepts of Ansible, installation methods, editing the hosts file, and the steps for remote batch command execution. In practical applications, you can write more complex Playbooks based on specific needs to achieve more efficient automated management.