Ansible Batch Processing in Practice¶
Software Introduction¶
Ansible is an automation operations tool developed in Python. It integrates the advantages of many operations tools (puppet, chef, func, fabric), achieving functionalities such as batch system configuration, batch program deployment, and batch command execution.
Features¶
- Simple deployment; only the Ansible environment needs to be deployed on the control end, while no operation is required on the managed end.
- Uses SSH protocol by default for device management.
- Comes with a large number of regular operations modules that can handle most daily tasks.
- Simple configuration, powerful functions, and highly extensible.
- Supports APIs and custom modules, which can be easily extended through Python.
- Customizes powerful configurations and status management via Playbooks.
Basic Architecture¶
- Ansible: The core program of Ansible.
- HostInventory: Records information about hosts managed by Ansible, including ports, passwords, IPs, etc.
- Playbooks: YAML format files where multiple tasks are defined in one file, specifying which modules should be called to complete the required functions.
- CoreModules: Core modules, primarily used to complete management tasks by calling these modules.
- CustomModules: Custom modules, used to achieve functions that cannot be completed by core modules, supporting various languages.
- ConnectionPlugins: Connection plugins used for communication between Ansible and Hosts.
Task Execution¶
The Ansible system divides the operation methods of control host nodes into two types: adhoc and playbook.
-
ad-hoc mode (point-to-point mode) Uses a single module and supports batch execution of single commands. Ad-hoc commands are quick-to-enter commands that do not need to be saved, similar to a shell command in bash.
-
playbook mode (script mode) The main management method of Ansible and the key to its powerful functionality. Playbooks accomplish a set of functions through a collection of multiple tasks, such as installing and deploying Web services or performing bulk backups of database servers. Playbooks can simply be understood as configuration files that combine multiple ad-hoc operations.
Batch Processing in Practice¶
Environment Preparation¶
IP | System | Hostname | Description |
---|---|---|---|
10.0.0.65 | CentOS 7.8 | ansible01 | Ansible management node (already installed dk) |
10.0.0.66 | CentOS 7.8 | ansible02 | Managed node 1 |
10.0.0.67 | CentOS 7.8 | ansible03 | Managed node 2 |
Software Installation¶
Log in to ansible01 and execute the installation command
Main Programs:
/usr/bin/ansible
Main Program/usr/bin/ansible-doc
Configuration Documentation/usr/bin/ansible-playbook
Customizes automated tasks, orchestration script tool/usr/bin/ansible-pull
Remote command execution tool/usr/bin/ansible-vault
File encryption tool
Main Configuration Files:
/etc/ansible/ansible.cfg
Main Configuration File/etc/ansible/hosts
Host List (place managed hosts here)/etc/ansible/roles/
Directory for storing roles
Passwordless Login¶
Log in to ansible01 and generate a key, default path is /root/.ssh/id_rsa, /root/.ssh/id_rsa.pub
Distribute the key to the nodes that need to be managed
Modify the host list file /etc/ansible/hosts
, add group name and host IPs
Verify connectivity
Common Modules¶
Shell Module¶
The Shell module can invoke the shell interpreter on remote hosts to run commands, supporting various shell features like pipes.
- View current user id
- View users currently logged into the system
Copy Module¶
This module copies files to remote hosts and also supports generating files from given content and modifying permissions.
- Copy
ansible.cfg
file to remote host and specify permission as "read-write"-rw-rw-rw-
View ansible.cfg
file on remote host
- Specify content and generate a file
View file on remote host
File Module¶
This module sets file attributes, such as creating files, creating link files, deleting files, etc.
- Create
app
directory under/tmp
View /tmp
directory
- Delete
ansible.cfg
file previously copied from ansible01
Fetch Module¶
This module retrieves (copies) files from remote hosts to the local machine.
- Pull
/tmp/hello
file from remote host to/root
directory
In /root
directory, you can see two new directories (remote host IPs are used as directory names)
Guance Application¶
Bulk Installation¶
Use Shell module to install DataKit (note to modify the corresponding token)
ansible guance -m shell -a 'DK_DATAWAY="https://openway.guance.com?token=token" bash -c "$(curl -L https://static.guance.com/datakit/install.sh)"'
Check if the process has started
Bulk Configuration¶
- Enable netstat plugin
Use shell module to copy file netstat.conf.sample
as netstat.conf
ansible guance -m shell -a 'cp /usr/local/datakit/conf.d/host/netstat.conf.sample /usr/local/datakit/conf.d/host/netstat.conf'
Bulk restart DataKit
Bulk Upgrade¶
Create a DataKit upgrade yaml file, /etc/ansible/dk_upgrade.yaml
- hosts: guance
remote_user: root
tasks:
- name: dk version check
shell: datakit --version|grep -i upgrade|wc -l
register: version
- name: dk upgrade
when: version.stdout > "0"
shell: DK_UPGRADE=1 bash -c "$(curl -L https://static.guance.com/datakit/install.sh)"
Run playbook
Check if DataKit version is updated
Add scheduled task crontab -e (execute bulk upgrade at 02:02 AM every day)