Ansible batch processing in practice

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

  1. Deployment is simple, just deploy the Ansible environment on the master control side, and the controlled side does not need to do anything

  2. By default, the SSH protocol is used to manage the device

  3. There are a large number of routine operation and maintenance operation modules, which can realize most of the daily operations

  4. Simple configuration, powerful functions and strong scalability

  5. Supports API and custom modules, and can be easily extended through Python

  6. 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

yum install -y ansible

  

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

ssh-keygen

  

The secret key is distributed to the nodes that need to be managed

1

2

3

ssh-copy-id root@10.0.0.66

ssh-copy-id root@10.0.0.67

  

Modify the host list file /etc/ansible/hosts, add group name and host ip

1

2

3

4

5

[guance]

10.0.0.67

10.0.0.66

  

verify connectivity

1

ansible guance -m ping

  

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

ansible guance -m shell -a 'id'

 

 

  • View users currently logged in to the system

1

ansible guance -m shell -a 'who'

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

ansible guance -m copy -a 'src=/etc/ansible/ansible.cfg dest=/tmp/ansible.cfg mode=666'

  

 

View remote host ansible.cfg file

1

ansible guance -m shell -a 'ls -l /tmp/ansible.cfg'

  

 

  • Specify content and generate files

1

ansible guance -m copy -a 'content="hello world" dest=/tmp/hello mode=666'

  

View remote hosts file

1

ansible guance -m shell -a 'cat /tmp/hello'

  

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

ansible guance -m file -a 'path=/tmp/app state=directory'

  

View the /tmp directory

1

ansible guance -m shell -a 'ls -l /tmp'

 

  • Delete the ansible.cfg file copied from ansible01 before

1

ansible guance -m file -a 'path=/tmp/ansible.cfg state=absent'

  

 

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

ansible guance -m fetch -a 'src=/tmp/hello dest=/root'

  

In the /root directory, you can see two new directories (the remote host ip is the directory name)

1

2

3

yum -y install

treetree /root

  

 

Observation cloud application

batch install

Use the shell module to install datakit (be careful to modify the corresponding token)

1

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 to see if the process has started

1

ansible guance -m shell -a 'ps -ef|grep datakit|grep -v grep'

batch configuration

  • Enable the netstat plugin

Use the shell module to copy the file netstat.conf.sample to netstat.conf

1

ansible guance -m shell -a 'cp /usr/local/datakit/conf.d/host/netstat.conf.sample /usr/local/datakit/conf.d/host/netstat.conf'

Batch restart datakit

1

ansible guance -m shell -a 'systemctl restart datakit'

batch upgrade

Create a new datakit upgrade yaml file, /etc/ansible/dk_upgrade.yaml

1

2

3

4

5

6

7

8

9

- hosts: guance 

remote_user: root 

tasks:

   - name: dk versioncheck

     shell: datakit--version|grep -i upgrade|wc -l

     register:version

   -name: dkupgradewhen: version.stdout >"0"

     shell: DK_UPGRADE=1bash -c"$(curl -L

     https://static.guance.com/datakit/install.sh)"

run playbook

1

ansible-playbook /etc/ansible/dk_upgrade.yaml

 

Check that the datakit version is the latest

1

ansibleguance -m shell -a'datakit --version'

Add a scheduled task crontab -e (perform batch upgrade at 02:02 every day)

1

02 02* ** ansible-playbook /etc/ansible/dk_upgrade.yaml

Guess you like

Origin blog.csdn.net/weixin_47367099/article/details/127546320