Today I’m going to share with you an operation and maintenance tool called Ansible.
Ansible is an open source automated operations tool. What can it do for operations people?
Usage scenarios
Let’s imagine a scenario where today apprentices need to create 10 EC2 instances in AWS, and each of them needs to install vim and Docker basic software. According to the conventional operation, apprentices create 10 EC2 instances, and then remotely access each of the 10 EC2 instances to install the software using the install command. Admittedly, there’s a lot of rework involved that developers (for the record: DevOps is also developers) can’t stand.
Then Ansible comes in handy.
Ansible allows you to batch issue Ansible commands to Linux servers to remotely arrange your servers without remotely accessing the server instance.
The concept is introduced
The basic concepts of Ansible are shown in this simple diagram above:
-
Control Node
The host for issuing Ansible commands.
-
Managed Node
Servers that receive Ansible commands.
-
Inventory
A host list that specifies which Managed nodes to issue Ansible commands to in a single INI file.
-
Module
The command module is used to perform Ansible operations on the Managed Node, such as file, software, and service operations.
Operation demo
Without further ado, the apprentice will show you how to use Ansible directly. The demo is based on a Kubernetes cluster (prepared in advance) containing a Master and two Managed nodes. The apprentice uses the Master as a Control Node and the two nodes as a Managed Node. Using Ansible to install nginx applications on two Managed nodes, this demo simply simulates the operation and maintenance of a batch installation of software.
Step
-
Start by installing Ansible on your Master machine
# Since Ansible is based on Python, you can install it directly with this command
pip install ansible
# apt or yum install
apt install ansible -y
# &
yum install ansible -y
Verify the installation with the following command
anisble --versionCopy the code
-
Configuring the Host List
Create file hosts, configure the Managed Node information in ini format, configure the server IP address in the following format, and log in to the server remotely. Replace the anSIBLE_ssh_private_KEY_file with the anSIBLE_ssh_pass parameter. The configuration of the host list may be described in a separate article.
[Nodes] 10.0.33.118 10.0.32.146 [Nodes: Vars] ansible_ssh_user=admin Ansible_ssh_private_key_file =/home/admin/sshkeyCopy the code
-
You can now issue Ansible commands to hosts in the host list
Test the connection between the Control Node and Managed Node.
ansile nodes -m ping -i hostsCopy the code
If the following information is displayed, the connection is successful.
Next, install the Nginx service.
ansible servers -m apt -a "name=nginx update_cache=yes" -i hostsCopy the code
When you execute the above command, the message returned appears to say that the operation failed.
Don’t worry, this is because when installing a service, you usually need sudo permission. You can simply add -b to the command.
ansible servers -m apt -a "name=nginx update_cache=yes" -i hosts -bCopy the code
If the following information is displayed, the Nginx service has been successfully installed.
You can perform this operation on the Managed Node server.
Bingo!
conclusion
In order to control the amount of text, this article is introduced here.
I’m writing this post to let you know that Ansible, an excellent operation tool, is simple to use but does a lot of the work for us, allowing us to focus on the areas where we need to focus more. It’s getting more powerful with the community’s contributions. I will probably continue to write some Ansible articles, such as Ansible parameter configuration, the use of Ansible Playbook, Ansible principle, etc.