Introduction to Ansible Batch Management Service
Significance of Ansible Batch Management
- Improves work efficiency.
- Enhances accuracy of operations.
- Reduces maintenance costs.
- Minimizes repetitive tasks.
Key Functionalities
- Perform batch system configuration and operations.
- Deploy software and services in bulk.
- Distribute files and data across multiple hosts.
- Collect system information from remote hosts.
Architecture Overview
- Inventory Configuration: Defines the list of managed hosts.
- Module Library: A collection of reusable modules for various tasks.
- SSH Key Authentication: Connects to hosts using SSH keys for secure, passwordless access.
- SELinux Requirement: SELinux must be disabled on managed hosts (or the
libselinux-pythonpackage installed). - Playbook System: Allows automation through reusable scripts.
Key Features
- No Server on Control Node: Ansible does not require a daemon or server process.
- Minimal Configuration: The control node can work with out a configuration file, but
/etc/ansible/ansible.cfgis available. - No Agent on Managed Nodes: Managed hosts do not need Ansible installed (though
libselinux-pythonis required if SELinux is not disabled). - No Agent Process on Managed Nodes: No background agent runs on the target machines.
- Rich Module Ecosystem: A vast library of modules covers almost all administrative tasks.
- Playbook Automation: YAML-based playbooks enable complex, automated workflows.
Official Documentation
How to Access Help
ansible-doc -l— List all available modules with brief descriptions.ansible-doc -s <module_name>— Show detailed options and parameters for a specific module.ansible-doc <module_name>— Display documentation examples for use in playbooks.
Deploying Ansible
Prerequisites (Control Node)
-
Install Ansible
yum install -y ansible rpm -ql ansible -
Configure Inventory File (
/etc/ansible/hosts)# Add target host IPs or hostnames at the end of the file 172.16.1.41 172.16.1.7 172.16.1.31 -
Test Connectivity
ansible all -a "hostname"
Common Ansible Modules
The general syntax for using a module is:
ansible <host_pattern> -m <module_name> -a "<arguments>"
1. Command Module (Default Module)
Executes a simple command on the remote host. It does not process shell variables, redirects, or pipes.
Basic Usage:
ansible 172.16.1.31 -m command -a "ifconfig"
Common Parameters:
-
chdir: Change to a directory before executing the command.ansible 172.16.1.31 -m command -a "chdir=/tmp touch oldboy.txt" -
creates: If the specified file already exists, the command is not executed.ansible 172.16.1.31 -m command -a "creates=/tmp/hosts touch oldboy.txt" -
removes: If the specified file exists, the command is executed.ansible 172.16.1.31 -m command -a "removes=/tmp/hosts chdir=/tmp touch oldboy.txt" -
free_form(required): A valid Linux command must be provided.
Important Note: The command module cannot handle shell operators like <, >, |, ;, and &.
2. Ping Module
Tests connectivity to the remote host.
ansible all -m ping
3. Shell Module (Universal Module)
Executes commands using the shell, supporting all shell features (pipes, redirects, variables).
Basic Usage:
ansible 172.16.1.31 -m shell -a "hostname"
ansible 172.16.1.31 -m shell -a "echo 123 > haha.txt"
ansible 172.16.1.31 -m shell -a "netstat -anptu | grep LISTEN"
4. Script Module (Universal Module)
Copies and executes a local script on the remote host.
Example:
-
Create a script on the control node (
/root/yum.sh):#!/bin/bash yum install htop -
Run the script on the target host:
ansible 172.16.1.31 -m script -a "chdir=/root/yum.sh" -
Verify installation:
ansible 172.16.1.31 -m shell -a "rpm -qa | grep htop"
Note: The script module accepts similar parameters to the command module.
5. Copy Module
Copies files from the control node to the remote host.
Basic Usage:
ansible 172.16.1.31 -m copy -a "src=/etc/hosts dest=/root/"
Example Output:
172.16.1.31 | CHANGED => {
"changed": true,
"checksum": "6ed7f68a1d6b4b36c1418338b2001e421eeba270",
"dest": "/root/hosts",
"gid": 0,
"group": "root",
"md5sum": "7afd7b74854f0aaab646b3e932f427c0",
"mode": "0644",
"owner": "root",
"size": 401,
"src": "/root/.ansible/tmp/ansible-tmp-1557804498.23-26487341925325/source",
"state": "file",
"uid": 0
}
Advanced Usage:
-
Change owner and group:
ansible 172.16.1.31 -m copy -a "src=/etc/ansible/file/rsync/rsync.password dest=/etc/ owner=oldboy group=oldboy" -
Set permissions:
ansible 172.16.1.31 -m copy -a "src=/etc/ansible/file/rsync/rsync.password dest=/etc/ mode=1777" -
Backup existing file on destination before copying:
ansible 172.16.1.31 -m copy -a "src=/etc/ansible/file/rsync/rsync.password dest=/etc/ backup=yes" -
Create a file with specific content:
ansible 172.16.1.31 -m copy -a "content='zhangbiao' dest=/root/zhangbiao.txt" -
Copy directories:
- If
srcdoes not end with/, the directory itself and its contents are copied. - If
srcdoes end with/, only the contents of the directory are copied.
# Copies /oldboy directory and its contents ansible 172.16.1.31 -m copy -a "src=/oldboy dest=/oldboy" # Copies only the contents of /oldboy ansible 172.16.1.31 -m copy -a "src=/oldboy/ dest=/oldboy" - If
6. File Module
Manages file attributes (owner, group, permissions) and creates/deletes files, directories, or symlinks.
Basic Usage (Set attributes):
ansible 172.16.1.31 -m file -a "dest=/etc/hosts owner=oldboy group=oldboy mode=666"
state Parameter Values:
absent: Delete the file or directory.directory: Create a directory (recursively if needed).file: Check if the file exists. No change if exists, error if not.hard: Create a hard link.link: Create a symbolic link.touch: Create an empty file.
Examples:
-
Create a directory:
ansible 172.16.1.31 -m file -a "dest=/oldboy/ state=directory" ansible 172.16.1.31 -m file -a "dest=/oldboy/oldboy01/oldboy02/ state=directory" -
Create a file:
ansible 172.16.1.31 -m file -a "dest=/oldboy/oldboy.txt state=touch" -
Create links:
ansible 172.16.1.31 -m file -a "src=/oldboy/oldboy.txt dest=/oldboy/oldboy_hard.txt state=hard" ansible 172.16.1.31 -m file -a "src=/oldboy/oldboy.txt dest=/oldboy/oldboy_link.txt state=link" -
Delete files or directories:
ansible 172.16.1.31 -m file -a "dest=/oldboy/oldboy.txt state=absent" ansible 172.16.1.31 -m file -a "dest=/oldboy/ state=absent"
7. Fetch Module
Pulls a file from a remote host to the control node. The file is saved in a directory structure matching the hostname.
ansible 172.16.1.31 -m fetch -a "src=/tmp/oldboy.txt dest=/tmp"
8. Yum Module
Manages packages using the yum package manager.
Parameters:
name: Package name.state: Desired state (installed,present,latestto install;absent,removedto remove).
ansible 172.16.1.31 -m yum -a "name=iotop state=installed"
9. Service Module
Controls services on remote hosts (start, stop, restart, enable at boot).
Parameters:
name: Service name.state:started,restarted,stopped.enabled:yesto enable at boot,noto disable.
Example: Start NFS service and enable it at boot.
ansible 172.16.1.31 -m service -a "name=nfs state=started enabled=yes"
10. Cron Module
Manages cron jobs on remote hosts.
Parameters: minute, hour, day, month, weekday, job.
Basic Usage:
# Schedule time sync at 2:00 AM daily
ansible 172.16.1.31 -m cron -a "minute=0 hour=2 job='/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1'"
Advanced Usage:
-
Add a comment/name to the cron job:
ansible 172.16.1.31 -m cron -a "name='time sync' minute=0 hour=2 job='/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1'" -
Remove a cron job (only works for jobs created by Ansible):
ansible 172.16.1.31 -m cron -a "name='time sync01' state=absent" -
Disable (comment out) a cron job:
ansible 172.16.1.31 -m cron -a "name='time sync' job='/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1' disabled=yes"
11. Mount Module
Manages mount points and fstab entries.
Parameters:
src: Device or remote filesystem.path: Mount point.fstype: Filesystem type.state:present(add to fstab only),mounted(mount and add to fstab),absent(unmount and remove from fstab),unmounted(unmount only).
Example: Mount an NFS share.
ansible 172.16.1.31 -m mount -a 'fstype=nfs src=172.16.1.31:/nfs path=/mnt state=mounted'
12. User Module
Manages user accounts.
Basic Usage (Create a user):
ansible 172.16.1.31 -m user -a "name=oldboy01"
Advanced Usage:
-
Set UID:
ansible 172.16.1.31 -m user -a "name=oldboy02 uid=6666" -
Set primary group or supplementary groups:
# Primary group ansible 172.16.1.31 -m user -a "name=oldboy03 group=oldboy02" # Supplementary groups ansible 172.16.1.31 -m user -a "name=oldboy04 groups=oldboy02" -
Create a system user (no home directory, no login shell):
ansible 172.16.1.31 -m user -a "name=rsync create_home=no shell=/sbin/nologin" -
Set password (password must be a hashed string):
Generate a hashed password:
-
Method 1:
ansible all -i localhost, -m debug -a "msg={{ '123456' | password_hash('sha512', 'oldboy') }}" -
Method 2:
yum install -y python-pip pip install passlib python -c "from passlib.hash import sha512_crypt; import getpass; print(sha512_crypt.using(rounds=5000).hash(getpass.getpass()))"
Apply the hashed password:
ansible 172.16.1.31 -m user -a 'name=oldboy08 password=$6$oldboy$MVd3DevkLcimrBLdMICrBY8HF82Wtau5cI8D2w4Zs6P1cCfMTcnnyAmmJc7mQaE9zuHxk8JFTRgYMGv9uKW7j1' -
Note: This guide covers the most common used modules for routine system administration tasks. Refer to the official documentation for a complete list and advanced usage.