Ansible is a powerful automation tool that simplifies complex system deployments to the level of pressing a button. Previously, operations personnel had to manually execute commands, install software, and configure environments on multiple servers, which was time-consuming, labor-intensive, and prone to errors. With Ansible, these operations can be automated, significantly reducing the operational burden and making the deployment process more stable and reliable. Next, let me take you into the world of Ansible and see how it makes operations easier.
What is Ansible?
Ansible is like a “big butler” that commands servers. It connects to servers via SSH and executes various tasks according to the “playbook” we have written in advance, such as installing packages, configuring files, and starting services. Unlike other automation tools, Ansible does not require any client software to be installed on the target servers; as long as the servers are accessible via SSH, the complexity of deployment is greatly reduced.
For example, suppose you want to install the Apache service on multiple servers. If you were to do it manually, you would have to log into each server one by one and run the installation command. But with Ansible, you only need to write a simple playbook that specifies the task of installing Apache, and then run the Ansible command, which will automatically install Apache on each server, saving you time and effort.
# Example Playbook to Install Apache Service
---
- name: Install Apache
hosts: webservers
tasks:
- name: Install Apache package
apt:
name: apache2
state: present
This playbook means that on the server group named “webservers”, the task of installing Apache will be executed. The <span>apt</span>
module is used by Ansible to manage packages, <span>name</span>
specifies the name of the package to be installed, and <span>state: present</span>
indicates that this package should be in an installed state.
Structure and Writing of Playbooks
The playbook is the core of Ansible; it is like a recipe that tells Ansible what to do. A playbook consists of multiple tasks, each with a name and an operation to execute.
The name of the task is for human readability, making it easier to understand what the task does. The operation part tells Ansible what command to execute or which module to call. For example, in the task to install Apache, the name is “Install Apache package”, and the operation is to call the <span>apt</span>
module to install the <span>apache2</span>
package.
When writing a playbook, pay attention to the format. Ansible uses YAML format, which has strict indentation requirements. Each level of indentation must use two spaces, and tabs cannot be used; otherwise, it will result in an error. For example:
---
- name: Configure web server
hosts: webservers
tasks:
- name: Install Apache
apt:
name: apache2
state: present
- name: Start Apache service
service:
name: apache2
state: started
This playbook contains two tasks: one to install Apache and another to start the Apache service. The indentation of each task must be correct; otherwise, Ansible will not understand the hierarchy of the tasks.
Using Variables
In Ansible, variables are like boxes that can store values. You can store commonly used values, such as package names and file paths, in variables and then reference these variables in the playbook. The benefit of this approach is that if you need to change these values later, you only need to modify the variable definitions without searching through the playbook.
For example, if you frequently need to install a package called <span>myapp</span>
, you can define a variable <span>app_name</span>
and then reference it in the task:
---
- name: Install myapp
hosts: servers
vars:
app_name: myapp
tasks:
- name: Install app
apt:
name: "{{ app_name }}"
state: present
<span>vars</span>
section defines the variable <span>app_name</span>
with the value <span>myapp</span>
. In the task, you use <span>{{ app_name }}</span>
to reference this variable. This way, if you need to change the package name later, you only need to modify the <span>vars</span>
section.
Tip: Variable names should be meaningful so that others can easily understand what the variable is used for when they look at your playbook.
Using Libraries and Modules
Ansible has many built-in modules, which are like tools in a toolbox that help us accomplish various tasks. For example, the <span>apt</span>
module is used to manage packages, the <span>service</span>
module is used to manage services, and the <span>copy</span>
module is used to copy files, among others.
When using modules, you only need to specify the module name and parameters in the task. For example, if you want to copy a local configuration file to the server, you can use the <span>copy</span>
module:
---
- name: Copy config file
hosts: servers
tasks:
- name: Copy file to server
copy:
src: /path/to/local/config
dest: /path/to/remote/config
<span>src</span>
parameter specifies the path of the local file, and <span>dest</span>
parameter specifies the path on the server where the file should be copied.
Ansible’s module library is extensive; you can find a list of all modules and their usage in the official Ansible documentation. When learning, take a look at these modules; you might find tools that can solve your problems.
Using Libraries and Modules
Ansible’s module library is like a powerful toolbox filled with various tools that can help us tackle complex tasks. For example, the <span>apt</span>
module can help us install packages, the <span>service</span>
module can control the starting and stopping of services, and the <span>copy</span>
module can easily copy files from local to the server.
For copying files, suppose you have a configuration file <span>config.txt</span>
that you want to place in the <span>/etc</span>
directory on the server. Using the <span>copy</span>
module, you can accomplish this with just a few lines of code:
---
- name: Copy config file
hosts: servers
tasks:
- name: Copy file to server
copy:
src: config.txt
dest: /etc/config.txt
Here, the <span>src</span>
parameter specifies the location of the local file, and the <span>dest</span>
parameter tells Ansible where to copy the file on the server. Ansible’s module library is rich; you can find a list of all modules and their usage in the official documentation. When learning, take a look at these modules; you might find tools that can solve your problems.
Tip: Make sure to write the module parameters correctly; otherwise, Ansible will throw an error. For example, the <span>src</span>
and <span>dest</span>
parameters cannot be reversed; otherwise, the file will not be copied to the correct location.