A preliminary study on the automatic operation and maintenance tool ansible under Linux

Time:2021-4-14

1、 What kind of situation needs batch deployment

1. Installation of operating system

The common ones are colber, red hat, Satelite (red hat) system.

2. Configuration of operating system

The common ones are Cfengine, puppet, chef and func. Among them, puppet is the most popular

3. Deployment of batch program

4. Run batch command to view status information

2、 Introduction to ansible

The architecture of ansible is as follows

Ansible is a new operation and maintenance tool, which is based on python. It combines the advantages of many old operation and maintenance tools, and realizes batch operation system configuration, batch program deployment, batch operation command and other functions.

Common working modes of operation and maintenance tools
1. Agent mode: Based on SSL. The agent works on the monitored side. Like a puppet.  
2. Agentless mode: service implementation based on SSH works on the monitored end. The monitor side is the SSH client.
Ansible works in agentless mode and is idempotent. Ansible only needs to tell the monitor the expected state on the control side to realize batch deployment.

Noun explanation
Idempotent instructions with the same repeatability are not executed. For example, the software will not be installed repeatedly
The expected state only needs to tell the expected state of the monitored end
Ansible is based on module work. Ansible itself does not have the ability of batch deployment. What really has batch deployment is the module that ansible runs. Ansible only provides a framework. Architecture includes

Connection plugins is responsible for communication with the monitored end.

Host inventory: Specifies the host of the operation. It is the host of monitoring defined in the configuration file

Various modules core module command module custom module

With the help of plug-ins to complete the log mail and other functions

Playbooks: when a script performs multiple tasks. It is not necessary to have a node run multiple tasks at once

3、 Basic usage
 
When managing a cluster, ansible needs to configure the cluster hosts list to facilitate cluster operation

Copy code

The code is as follows:

$ cat /etc/ansible/hosts
[local]
#To configure password login, you need to install sshpass on ansible local machine
192.168.213.135 ansible_ssh_user=root ansible_ssh_pass=root
[zabbix]
#Key login
172.17.0.2:49154 ansible_ssh_user=root
172.17.0.4:49155 ansible_ssh_user=root
[vpn]
172.17.0.10

 
Test whether the connection login is OK

Copy code

The code is as follows:

$ ansible local -m ping
192.168.213.135 | success >> {
“changed”: false,
“ping”: “pong”
}

 
The common usage of ansible is ansible host pattern – M module – a command. Host pattern is similar to a simplified regular expression, and modules can be queried by ansible doc – L command. The following is how to use some common modules:
Installation software:

Copy code

The code is as follows:

ansible local -m apt -a ‘name=gcc state=present’

perhaps

Copy code

The code is as follows:

ansible local -m yum -a “name=nmap state=installed”

Execute command:

Copy code

The code is as follows:

ansible local -m shell -a ‘uptime’

Copy file:

Copy code

The code is as follows:

ansible local -m copy -a “src=/tmp/server dest=/tmp/server”

File properties:

Copy code

The code is as follows:

ansible local -m file -a “dest=/tmp/server mode=755 owner=root group=root”

*
 
playbook
 
PlayBook is a list of one or more “plays”. The main function of play is to dress up the hosts which are merged into a group in advance as the roles defined by tasks in ansible.
 
A simple playbook configuration is as follows:

Copy code

The code is as follows:

– hosts: local
remote_user: root
tasks:
– name: echo hi
shell: echo “hi”

 
Ansible also supports setting handlers, which can be called after the server changes after executing tasks. The usage is as follows:

Copy code

The code is as follows:

# playbook.yml

-Hosts: specified in local # hosts
remote_ User: root # if it is the same as the current user, it does not need to be specified
tasks:
– name: whoami
copy: src=~/hosts dest=~/ hosts.dest #Local copy to remote
Notify: if the copy is finished~/ hosts.dest If the file is sent with a change, execute
-Clear copy # call handler
handlers:
– name: clear copy
shell: ‘mv ~/ hosts.dest hosts.del Pretend to delete

Recommended Today

Deeply analyze the principle and practice of RSA key

1、 Preface After experiencing many dark moments in life, when you read this article, you will regret and even be angry: why didn’t you write this article earlier?! Your darkest moments include: 1. Your project needs to be connected with the bank, and the other party needs you to provide an encryption certificate. You have […]