A simple, robust and non-agential IT automated platform that allows for deployment, configuration management and task automation
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>"
Execute the command on a remote node. This is the default module, and no specific module name is required.
ansible webservers -m command -a "/usr/bin/make_database.sh"
ansible dbservers -a "/usr/bin/make_database.sh db_user db_name"
Execute orders via shell at remote nodes to support the shell properties such as pipelines, redirection, etc.
ansible all -m shell -a "ps aux | grep java | grep -v grep"
ansible webservers -m shell -a "echo 'Hello World' > /tmp/hello.txt"
Transfers a local script to a remote node and executes it remotely.
ansible all -m script -a "/opt/scripts/setup_environment.sh"
ansible dbservers -m script -a "/opt/scripts/backup_database.sh --dbname=myapp"
To copy files from the control node to the remote host. You can set file privileges, owners, and groups.
ansible webservers -m copy -a "src=/etc/ansible/config.conf dest=/etc/myapp/config.conf owner=root group=root mode=0644"
ansible dbservers -m copy -a "src=/tmp/database.conf dest=/etc/mysql/my.cnf backup=yes"
Gets files from remote nodes to control nodes.
ansible webservers -m fetch -a "src=/var/log/nginx/access.log dest=/tmp/logs/ flat=yes"
ansible dbservers -m fetch -a "src=/var/log/mysql/ dest=/tmp/mysql_logs/ flat=yes"
To set the properties of a file, or to create/delete files, directories, and symbols.
ansible all -m file -a "path=/opt/myapp/logs state=directory owner=myuser group=mygroup mode=0755"
ansible webservers -m file -a "src=/opt/myapp dest=/usr/local/myapp state=link"
ansible all -m file -a "path=/tmp/oldfile state=absent"
Uncompress files or archive them to the remote host.
ansible all -m unarchive -a "src=/tmp/app.tar.gz dest=/opt/ copy=no"
ansible webservers -m unarchive -a "src=/opt/archives/app.zip dest=/opt/myapp/"
Creates a compressed archive file.
ansible webservers -m archive -a "path=/var/log/nginx dest=/tmp/nginx_logs.tar.gz format=gz"
ansible dbservers -m archive -a "path=/var/lib/mysql dest=/tmp/mysql_backup.zip format=zip"
Manages the host name of the remote host.
ansible webserver01 -m hostname -a "name=web01.example.com"
ansible dbserver01 -m hostname -a "name=db01.example.com use=systemd"
Manages the cron operations, which create, modify and delete timed tasks.
ansible all -m cron -a "name='daily backup' minute=0 hour=2 job='/usr/local/bin/backup.sh'"
ansible webservers -m cron -a "name='daily backup' disabled=yes"
ansible all -m cron -a "name='daily backup' state=absent"
Manage the YUM software warehouse based on the RPM system.
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"
ansible all -m yum_repository -a "name=epel state=absent"
The dnf package manager is used to manage the software package on RPM-based systems.
ansible fedora_servers -m dnf -a "name=nginx state=present"
ansible all -m dnf -a "name=* state=latest"
ansible webservers -m dnf -a "name=apache state=absent"
Manage service status, e.g. start, stop, restart and start up.
ansible webservers -m service -a "name=httpd state=started enabled=yes"
ansible all -m service -a "name=nginx state=restarted"
ansible dbservers -m service -a "name=mysql state=stopped"
Manage Firewall Rules.
ansible webservers -m firewalld -a "service=http permanent=yes state=enabled"
ansible dbservers -m firewalld -a "port=5432/tcp permanent=yes state=enabled"
ansible all -m firewalld -a "source=192.168.1.0/24 zone=trusted state=enabled"
Manages user accounts, including creating, modifying and deleting users.
ansible all -m user -a "name=john comment='John Doe' home=/home/john shell=/bin/bash groups=wheel append=yes"
ansible all -m user -a "name=john state=absent remove=yes"
ansible webservers -m user -a "name=john shell=/bin/zsh"
Manage user groups.
ansible all -m group -a "name=developers state=present"
ansible dbservers -m group -a "name=dbadmin system=yes state=present"
ansible all -m group -a "name=oldgroup state=absent"
Ensure that there are specific rows in the document, or modify/replace existing rows.
ansible all -m lineinfile -a "path=/etc/ssh/sshd_config line='PermitRootLogin no'"
ansible webservers -m lineinfile -a "path=/etc/nginx/nginx.conf regexp='^worker_processes' line='worker_processes 4;'"
ansible all -m lineinfile -a "path=/etc/hosts line='192.168.1.100 server.example.com' insertafter='127.0.0.1'"
Replaces all instances in a file that match a regular expression.
ansible webservers -m replace -a "path=/etc/nginx/nginx.conf regexp='old.server.com' replace='new.server.com'"
ansible all -m replace -a "dest=/etc/config.conf regexp='debug=true' replace='debug=false' backup=yes"
The collection of system information on remote hosts is one of the most important modules of Ansible.
ansible all -m setup
ansible webservers -m setup -a "filter=ansible_distribution*"
ansible all -m setup -a "filter=ansible_default_ipv4"
Prints debug information during the mission.
ansible all -m debug -a "msg='系统配置完成'"
ansible webservers -m debug -a "var=ansible_distribution"