简单、强大、无代理的IT自动化平台,可实现应用部署、配置管理和任务自动化
Ansible 是一个开源的自动化工具,用于配置管理、应用部署和任务自动化。它使用简单的YAML语法(称为Playbooks)来描述自动化任务,无需在远程主机上安装代理程序,通过SSH进行通信。
Ansible 的核心优势在于其简单性、易读性和强大的模块化架构。它拥有丰富的内置模块,可以管理各种系统资源,包括文件、软件包、服务、用户等。
# 基本命令格式
ansible <host-pattern> -m <module-name> -a "<module-parameters>"
在远程节点上执行命令。这是默认模块,不需要指定模块名。
ansible webservers -m command -a "/usr/bin/make_database.sh"
ansible dbservers -a "/usr/bin/make_database.sh db_user db_name"
在远程节点上通过shell执行命令,支持管道、重定向等shell特性。
ansible all -m shell -a "ps aux | grep java | grep -v grep"
ansible webservers -m shell -a "echo 'Hello World' > /tmp/hello.txt"
将本地脚本传输到远程节点并在远程执行。
ansible all -m script -a "/opt/scripts/setup_environment.sh"
ansible dbservers -m script -a "/opt/scripts/backup_database.sh --dbname=myapp"
用于将文件从控制节点复制到远程主机。可以设置文件权限、所有者和组。
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"
从远程节点获取文件到控制节点。
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"
用于设置文件的属性,或创建/删除文件、目录和符号链接。
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"
解压缩文件或存档到远程主机。
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/"
创建压缩存档文件。
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"
管理远程主机的主机名。
ansible webserver01 -m hostname -a "name=web01.example.com"
ansible dbserver01 -m hostname -a "name=db01.example.com use=systemd"
管理cron作业,可以创建、修改和删除定时任务。
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"
管理基于RPM系统的YUM软件仓库。
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"
在基于RPM的系统上使用dnf包管理器管理软件包。
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"
管理服务状态,如启动、停止、重启和启用开机自启。
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"
管理firewalld防火墙规则。
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"
管理用户账户,包括创建、修改和删除用户。
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"
管理用户组。
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"
确保文件中存在特定行,或修改/替换现有行。
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'"
替换文件中匹配正则表达式的所有实例。
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"
收集远程主机的系统信息,是Ansible最重要的模块之一。
ansible all -m setup
ansible webservers -m setup -a "filter=ansible_distribution*"
ansible all -m setup -a "filter=ansible_default_ipv4"
在任务执行期间打印调试信息。
ansible all -m debug -a "msg='系统配置完成'"
ansible webservers -m debug -a "var=ansible_distribution"