Skip to content

Ansible Batch Operations Guide


Software Introduction

Ansible is an automated operations tool developed in Python. It integrates the advantages of many operations tools (Puppet, Chef, Func, Fabric) and implements batch system configuration, batch program deployment, and batch command execution.

Features

  1. Simple deployment: only the control node needs Ansible environment, no action required on managed nodes
  2. Uses SSH protocol by default for device management
  3. Has a large number of common operations modules covering most daily tasks
  4. Simple configuration, powerful functionality, and strong extensibility
  5. Supports API and custom modules, easily extensible via Python
  6. Uses Playbooks to define powerful configuration and state management

Basic Architecture

image.png

  • Ansible: Core Ansible program.
  • HostInventory: Records host information managed by Ansible, including ports, passwords, IPs, etc.
  • Playbooks: YAML format files that define multiple tasks in one file, specifying which modules to call on hosts to complete functions.
  • CoreModules: Core modules, the main operations are performed by calling core modules to complete management tasks.
  • CustomModules: Custom modules, used to complete functions not covered by core modules, supporting multiple languages.
  • ConnectionPlugins: Connection plugins used for communication between Ansible and hosts.

Task Execution

The Ansible system can be divided into two modes of operation from the control host to managed nodes: ad-hoc and playbook

  • Ad-hoc mode (point-to-point mode) Uses a single module to execute a single command in batch. An ad-hoc command is a quick input command that does not need to be saved, similar to a single shell command in bash.

  • Playbook mode (script mode) The main management method of Ansible and the key to its powerful functionality. A playbook combines multiple tasks to complete a specific function, such as deploying a web service or batch backup of database servers. A playbook can be simply understood as a configuration file that combines multiple ad-hoc operations.

Batch Operations Guide

Environment Preparation

IP System Hostname Description
10.0.0.65 CentOS 7.8 ansible01 Ansible management node (dk already installed)
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:

yum install -y ansible

Main programs:

  • /usr/bin/ansible Main program
  • /usr/bin/ansible-doc Configuration documentation
  • /usr/bin/ansible-playbook Custom automation tasks, playbook orchestration tool
  • /usr/bin/ansible-pull Remote execution tool
  • /usr/bin/ansible-vault File encryption tool

Main configuration files:

  • /etc/ansible/ansible.cfg Main configuration file
  • /etc/ansible/hosts Host inventory (place managed hosts here)
  • /etc/ansible/roles/ Directory for roles

Passwordless SSH Login

Log in to ansible01, generate an SSH key. Default paths: /root/.ssh/id_rsa, /root/.ssh/id_rsa.pub

ssh-keygen

Distribute the public key to the managed nodes:

ssh-copy-id root@10.0.0.66
ssh-copy-id root@10.0.0.67

Modify the host inventory file /etc/ansible/hosts to add the group name and host IPs:

[guance]
10.0.0.67
10.0.0.66

Verify connectivity:

ansible guance -m ping

image.png

Common Modules

Shell Module

The Shell module invokes the shell interpreter on remote hosts to run commands, supporting shell features such as pipes.

  • Check the current user ID
ansible guance -m shell -a 'id'

image.png

  • Check currently logged-in users
ansible guance -m shell -a 'who'

image.png

Copy Module

This module copies files to remote hosts, and also supports generating a file from given content and modifying permissions.

  • Copy the ansible.cfg file to the remote host and set permissions to "read-write" -rw-rw-rw-
ansible guance -m copy -a 'src=/etc/ansible/ansible.cfg dest=/tmp/ansible.cfg mode=666'

image.png

Check the remote host's ansible.cfg file:

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

image.png

  • Generate a file from given content
ansible guance -m copy -a 'content="hello world" dest=/tmp/hello mode=666'

image.png

Check the remote host file:

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

image.png

File Module

This module sets file attributes, such as creating files, creating symbolic links, deleting files, etc.

  • Create the app directory under /tmp
ansible guance -m file -a 'path=/tmp/app state=directory'

image.png

Check the /tmp directory:

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

image.png

  • Delete the ansible.cfg file previously copied from ansible01
ansible guance -m file -a 'path=/tmp/ansible.cfg state=absent'

image.png

Fetch Module

This module fetches (copies) files from a remote host to the local machine.

  • Fetch the remote host's /tmp/hello file to the /root directory
ansible guance -m fetch -a 'src=/tmp/hello dest=/root'

image.png

Two new directories (named after the remote host IPs) appear under /root:

yum -y install tree
tree /root

image.png

Guance Application

Batch Installation

Use the Shell module to install DataKit (note: 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:

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

image.png

Batch Configuration

  • Enable the netstat plugin

Use the Shell module to copy netstat.conf.sample to 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'

Batch restart DataKit:

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

Batch 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 the playbook:

ansible-playbook /etc/ansible/dk_upgrade.yaml

image.png

Check that the DataKit version is now up to date:

ansible guance -m shell -a 'datakit --version'

image.png

Add a scheduled task via crontab -e (run the batch upgrade at 02:02 daily):

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

Feedback

Is this page helpful?