Software Description
Ansible is an automated operation and maintenance tool, developed based on Python, which integrates the advantages of many operation and maintenance tools (puppet, chef, func, fabric), and realizes functions such as batch system configuration, batch program deployment, and batch operation commands.
Features
-
Deployment is simple, just deploy the Ansible environment on the master control side, and the controlled side does not need to do anything
-
By default, the SSH protocol is used to manage the device
-
There are a large number of routine operation and maintenance operation modules, which can realize most of the daily operations
-
Simple configuration, powerful functions and strong scalability
-
Supports API and custom modules, and can be easily extended through Python
-
Customize powerful configuration and state management through Playbooks
infrastructure
-
Ansible : Ansible core program.
-
HostInventory : Record host information managed by Ansible, including port, password, ip, etc.
-
Playbooks : YAML format files, multiple tasks are defined in one file, which defines which modules the host needs to call to complete the functions.
-
CoreModules : Core modules, the main operation is to complete management tasks by calling core modules.
-
CustomModules : Custom modules, complete functions that cannot be completed by core modules, and support multiple languages.
-
ConnectionPlugins : connection plug-ins, used by Ansible and Host for communication
task execution
The Ansible system can be divided into two types by the control host to the managed node, namely adhoc and playbook
-
The ad-hoc mode (point-to-point mode) uses a single module and supports batch execution of a single command. The ad-hoc command is a command that can be entered quickly and does not need to be saved, which is equivalent to a shell command in bash.
-
Playbook mode (script mode) Ansible's main management method is also the key to Ansible's powerful functions. The playbook completes a class of functions through multiple task sets, such as installation and deployment of Web services, batch backup of database servers, and so on. A playbook can be simply understood as a configuration file that combines multiple ad-hoc operations.
Batch combat
Environmental preparation
Software Installation
Log in to ansible01 and execute the installation command
1 |
|
main program
-
/usr/bin/ansible main program
-
/usr/bin/ansible-doc configuration documentation
-
/usr/bin/ansible-playbook Custom automation tasks, scripting tools
-
/usr/bin/ansible-pull A tool for remotely executing commands
-
/usr/bin/ansible-vault file encryption tool
main configuration file
-
/etc/ansible/ansible.cfg main configuration file
-
/etc/ansible/hosts host list (put managed hosts into this file)
-
/etc/ansible/roles/ Directory for storing roles
Password-free login
Log in to ansible01, generate a secret key, the default path is /root/.ssh/id_rsa, /root/.ssh/id_rsa.pub
1 |
|
The secret key is distributed to the nodes that need to be managed
1 2 3 |
|
Modify the host list file /etc/ansible/hosts, add group name and host ip
1 2 3 4 5 |
|
verify connectivity
1 |
|
Common modules
Shell module
The Shell module can call the shell interpreter to run commands on the remote host, and supports various functions of the shell, such as pipelines, etc.
-
View current user id
1 |
|
-
View users currently logged in to the system
1 |
|
Copy module
This module is used to copy files to remote hosts, and supports generating files and modifying permissions for given content, etc.
-
Copy the ansible.cfg file to the remote host, and specify the permission as "read and write" -rw-rw-rw-
1 |
|
View remote host ansible.cfg file
1 |
|
-
Specify content and generate files
1 |
|
View remote hosts file
1 |
|
File module
This module is used to set the properties of the file, such as creating a file, creating a link file, deleting a file, etc.
-
Create app directory under /tmp
1 |
|
View the /tmp directory
1 |
|
-
Delete the ansible.cfg file copied from ansible01 before
1 |
|
Fetch module
This module is used to obtain (copy) files from a remote host to the local
-
Pull the remote host /tmp/hello file to the /root directory
1 |
|
In the /root directory, you can see two new directories (the remote host ip is the directory name)
1 2 3 |
|
Observation cloud application
batch install
Use the shell module to install datakit (be careful to modify the corresponding token)
1 |
|
Check to see if the process has started
1 |
|
batch configuration
-
Enable the netstat plugin
Use the shell module to copy the file netstat.conf.sample to netstat.conf
1 |
|
Batch restart datakit
1 |
|
batch upgrade
Create a new datakit upgrade yaml file, /etc/ansible/dk_upgrade.yaml
1 2 3 4 5 6 7 8 9 |
|
run playbook
1 |
|
Check that the datakit version is the latest
1 |
|
Add a scheduled task crontab -e (perform batch upgrade at 02:02 every day)
1 |
|