ZHIXU TOOLSZHIXU TOOLSZHIXU

Ansible AutoMage Tool

A simple, robust and non-agential IT automated platform that allows for deployment, configuration management and task automation

Ansible Introduction

Ansible is an open-source automated tool for configuration management, application deployment and task automation. It uses simple YAML syntax (known as Playbooks) to describe automated tasks without having to install proxy on the remote host and communicate through SSH.

Ansible has the core advantage of simplicity, readability and a powerful modular architecture. It has a wealth of built-in modules to manage a variety of system resources, including documents, software packages, services, users, etc.

# 基本命令格式
ansible <host-pattern> -m <module-name> -a "<module-parameters>"

Introduction to Common Modules

Comand Module

Execute the command on a remote node. This is the default module, and no specific module name is required.

Examples of command lines
Description of parameters
Execute Simple Commands
ansible webservers -m command -a "/usr/bin/make_database.sh"
Execute commands with parameters
ansible dbservers -a "/usr/bin/make_database.sh db_user db_name"
Common parameters
  • cmd: Command to execute
  • chdir: Switch directory before executing command
  • creates: Not execute if file exists
  • removes: Not execute if file does not exist

shell module

Execute orders via shell at remote nodes to support the shell properties such as pipelines, redirection, etc.

Examples of command lines
Description of parameters
Operation with Pipe
ansible all -m shell -a "ps aux | grep java | grep -v grep"
Use redirection
ansible webservers -m shell -a "echo 'Hello World' > /tmp/hello.txt"
Common parameters
  • cmd: shell command to execute
  • chdir: Switch directory before executing command
  • executable: Specified shell

Script Module

Transfers a local script to a remote node and executes it remotely.

Examples of command lines
Description of parameters
Execute local scripts
ansible all -m script -a "/opt/scripts/setup_environment.sh"
Execute Script and Pass Parameters
ansible dbservers -m script -a "/opt/scripts/backup_database.sh --dbname=myapp"
Common parameters
  • cmdScript Path
  • creates: Not execute if file exists
  • removes: Not execute if file does not exist
  • chdir: Switch directory before executing script

Copy Module

To copy files from the control node to the remote host. You can set file privileges, owners, and groups.

Examples of command lines
Description of parameters
Copy file to remote host
ansible webservers -m copy -a "src=/etc/ansible/config.conf dest=/etc/myapp/config.conf owner=root group=root mode=0644"
Copy files and back up original files
ansible dbservers -m copy -a "src=/tmp/database.conf dest=/etc/mysql/my.cnf backup=yes"
Common parameters
  • src: Path to source file (control node)
  • dest: Target file path (remote host)
  • owner: File Owner
  • group: Group of files
  • mode: File permissions
  • backup: Whether existing files are backed up

Fetch Module

Gets files from remote nodes to control nodes.

Examples of command lines
Description of parameters
Fetch remote files
ansible webservers -m fetch -a "src=/var/log/nginx/access.log dest=/tmp/logs/ flat=yes"
Fetch Multiple Files
ansible dbservers -m fetch -a "src=/var/log/mysql/ dest=/tmp/mysql_logs/ flat=yes"
Common parameters
  • src: Remote File Path
  • dest: Local destination directory
  • flat: Whether to keep the filename unchanged
  • validate_checksum: whether to verify checksum

File Module

To set the properties of a file, or to create/delete files, directories, and symbols.

Examples of command lines
Description of parameters
Create Directory
ansible all -m file -a "path=/opt/myapp/logs state=directory owner=myuser group=mygroup mode=0755"
Create Symbolic Links
ansible webservers -m file -a "src=/opt/myapp dest=/usr/local/myapp state=link"
Remove File or Directory
ansible all -m file -a "path=/tmp/oldfile state=absent"
Common parameters
  • path: File / directory path
  • state: Status (file, directory, link, object, touch)
  • owner: File Owner
  • group: Group of files
  • mode: File permissions
  • src: Source Path (for symbolic links)

unarchive module

Uncompress files or archive them to the remote host.

Examples of command lines
Description of parameters
Unpress Remote File
ansible all -m unarchive -a "src=/tmp/app.tar.gz dest=/opt/ copy=no"
Copy and unpressure from control node
ansible webservers -m unarchive -a "src=/opt/archives/app.zip dest=/opt/myapp/"
Common parameters
  • src: Source File Path
  • dest: destination directory
  • copy: Whether to copy files from control node
  • owner: Unlock File Owner
  • group: Group of files after decompression
  • mode: Release file permissions

Arcive Module

Creates a compressed archive file.

Examples of command lines
Description of parameters
Create a tar.gz archive
ansible webservers -m archive -a "path=/var/log/nginx dest=/tmp/nginx_logs.tar.gz format=gz"
Create Zip Archive
ansible dbservers -m archive -a "path=/var/lib/mysql dest=/tmp/mysql_backup.zip format=zip"
Common parameters
  • pathPath to archive
  • dest: Path to archived files
  • format: Archive format (gz, bz2, zip, tar)
  • owner: Archived file owner
  • groupGroup of archived files

Hostname Module

Manages the host name of the remote host.

Examples of command lines
Description of parameters
Set Hostname
ansible webserver01 -m hostname -a "name=web01.example.com"
Set and use a new hostname
ansible dbserver01 -m hostname -a "name=db01.example.com use=systemd"
Common parameters
  • nameHostname to set
  • use: tools used (systemd, debian, etc.)

Cron Module

Manages the cron operations, which create, modify and delete timed tasks.

Examples of command lines
Description of parameters
Create Time Tasks
ansible all -m cron -a "name='daily backup' minute=0 hour=2 job='/usr/local/bin/backup.sh'"
Disable Timed Tasks
ansible webservers -m cron -a "name='daily backup' disabled=yes"
Remove Timed Tasks
ansible all -m cron -a "name='daily backup' state=absent"
Common parameters
  • name: Job Name
  • job: Command to execute
  • minute, hour, day, month, weekdayTime Settings
  • state: Status (present, object)
  • disabled: Disable
  • user: Run task user

yum_repository module

Manage the YUM software warehouse based on the RPM system.

Examples of command lines
Description of parameters
Add EPEL Repository
ansible centos_servers -m yum_repository -a "name=epel description=EPEL baseurl=https://download.fedoraproject.org/pub/epel/$releasever/$basearch/ gpgcheck=yes gpgkey=https://download.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-7"
Remove Repository
ansible all -m yum_repository -a "name=epel state=absent"
Common parameters
  • name: Repository Name
  • description: Repository Description
  • baseurl: Repository URL
  • gpgcheck: Whether to use GPG check
  • gpgkey: GPG key URL
  • state: Status (present, object)

dnf module

The dnf package manager is used to manage the software package on RPM-based systems.

Examples of command lines
Description of parameters
Install package
ansible fedora_servers -m dnf -a "name=nginx state=present"
Update All Packages
ansible all -m dnf -a "name=* state=latest"
Remove Package
ansible webservers -m dnf -a "name=apache state=absent"
Common parameters
  • name: package name
  • state: Status (present, adult, latest)
  • update_cache: Whether to update the package cache
  • disable_gpg_check: Disable GPG check

service module

Manage service status, e.g. start, stop, restart and start up.

Examples of command lines
Description of parameters
Start and enable services
ansible webservers -m service -a "name=httpd state=started enabled=yes"
Restart Service
ansible all -m service -a "name=nginx state=restarted"
Stop Service
ansible dbservers -m service -a "name=mysql state=stopped"
Common parameters
  • name: Service name
  • state: status (started, stopped, countered, reduced)
  • enabled: whether to start the self-start
  • sleep: Waiting time on restart (seconds)

Firewalld Module

Manage Firewall Rules.

Examples of command lines
Description of parameters
Open HTTP port
ansible webservers -m firewalld -a "service=http permanent=yes state=enabled"
Open a specific port
ansible dbservers -m firewalld -a "port=5432/tcp permanent=yes state=enabled"
Add Source IP White List
ansible all -m firewalld -a "source=192.168.1.0/24 zone=trusted state=enabled"
Common parameters
  • service: Service name
  • port: Port/ Protocol
  • source: Source IP/Network
  • zone: Firewall Area
  • permanent: Permanent or non-permanent
  • state: Status (enabled, disabled)

user module

Manages user accounts, including creating, modifying and deleting users.

Examples of command lines
Description of parameters
Create User
ansible all -m user -a "name=john comment='John Doe' home=/home/john shell=/bin/bash groups=wheel append=yes"
Remove User and its Home Directory
ansible all -m user -a "name=john state=absent remove=yes"
Modify User Properties
ansible webservers -m user -a "name=john shell=/bin/zsh"
Common parameters
  • name: Username
  • state: Status (present, object)
  • comment: User description
  • home: Home Directory Path
  • shell: Login shell
  • groups: user-owned group
  • append: Add to existing group
  • remove: Whether to delete the home directory when deleting a user

Group Module

Manage user groups.

Examples of command lines
Description of parameters
Create user group
ansible all -m group -a "name=developers state=present"
Create a system group
ansible dbservers -m group -a "name=dbadmin system=yes state=present"
Remove user group
ansible all -m group -a "name=oldgroup state=absent"
Common parameters
  • nameGroup name
  • state: Status (present, object)
  • system: Whether to be a system group
  • gidGroup ID

Linefile module

Ensure that there are specific rows in the document, or modify/replace existing rows.

Examples of command lines
Description of parameters
Make sure a line exists.
ansible all -m lineinfile -a "path=/etc/ssh/sshd_config line='PermitRootLogin no'"
Replace matching rows
ansible webservers -m lineinfile -a "path=/etc/nginx/nginx.conf regexp='^worker_processes' line='worker_processes 4;'"
Insert Rows at a Specific Location
ansible all -m lineinfile -a "path=/etc/hosts line='192.168.1.100 server.example.com' insertafter='127.0.0.1'"
Common parameters
  • path: File Path
  • line: Ensure that there is a line
  • regexp: Regular expression for matching
  • state: Status (present, object)
  • insertafter: Insert after matching line
  • insertbefore: Insert before matching line
  • backup: Whether to create a backup

Replace Module

Replaces all instances in a file that match a regular expression.

Examples of command lines
Description of parameters
Replace Text
ansible webservers -m replace -a "path=/etc/nginx/nginx.conf regexp='old.server.com' replace='new.server.com'"
Use backup
ansible all -m replace -a "dest=/etc/config.conf regexp='debug=true' replace='debug=false' backup=yes"
Common parameters
  • path/dest: File Path
  • regexp: Regular expression to match
  • replace: Replace text
  • backup: Whether to create a backup

Setup Module

The collection of system information on remote hosts is one of the most important modules of Ansible.

Examples of command lines
Description of parameters
Collect all system information
ansible all -m setup
Collection of specific information
ansible webservers -m setup -a "filter=ansible_distribution*"
Collection of network information
ansible all -m setup -a "filter=ansible_default_ipv4"
Common parameters
  • filter: Filter conditions, supports wildcards
  • gather_subset: collection of information subsets
  • gather_timeout: Collect timeout
Common Filters
  • ansible_distribution*: Release Information
  • ansible_memory*: Memory Information
  • ansible_processor*: Processor Information
  • ansible_default_ipv4: Default IPv4 address

debug module

Prints debug information during the mission.

Examples of command lines
Description of parameters
Print Simple Messages
ansible all -m debug -a "msg='系统配置完成'"
Print Variables
ansible webservers -m debug -a "var=ansible_distribution"
Common parameters
  • msgMessage to print
  • var: Variable name to print
  • verbosity: Details level

How to use

ZHIXU TOOLS · Back to all tools