使用ansible的playbook语言批量部署
-
Ansible
绿色: 状态没有改变,执行成功的
黄色: 状态有改变,执行成功的
紫色: 警告,在清单文件没有匹配到你指定的主机组,你使用的操作有对应模块可以实现
红色: 严重错误,任务无法执行 -
/bin/true
nohup &
批量部署tomcat 的playbook
准备好jdk和tomcat安装包,然后开始编写playbook
vim tomcat.yml
- hosts: webservers
tasks:- name: “发送tomcat安装包”
unarchive: src=apache-tomcat-8.5.35.tar.gz dest=/opt/ - name: “重命名tomcat”
shell: mv /opt/apache-tomcat-8.5.35 /opt/tomcat - name: “发送jdk”
copy: src=jdk-8u131-linux-x64_.rpm dest=/root/ - name: “安装jdk”
shell: rpm -ivh /root/jdk-8u131-linux-x64_.rpm || /bin/true - name: “启动tomcat”
shell: nohup /opt/tomcat/bin/startup.sh
- name: “发送tomcat安装包”
ansible-playbook tomcat.yml
批量部署lamp
vim lamp.yml
- hosts: webservers
tasks:- name: “安装lamp相关组件”
yum: name=httpd,mariadb,mariadb-server,php,php-mysql,php-gd state=latest - name: “配置httpd”
shell: sed -i ‘s/index.html/index.php/’ /etc/httpd/conf/httpd.conf - name: “创建测试页面”
shell: echo “<?php phpinfo();?>” > /var/www/html/index.php - name: “重启httpd”
systemd: name=httpd state=restarted enabled=yes - name: “启动mariadb”
systemd: name=mariadb state=started
- name: “安装lamp相关组件”
ansible-playbook lamp.yml
批量部署lnmp
1.安装 mariadb,mariadb-server,php,php-mysql,php-gd,php-fpm,gcc,pcre-devel,openssl-devel
2.发包nginx
3.编译安装nginx
4.配置nginx
5.写页面
6.启动nginx
7.启动php-fpm
vim lnmp.yml
- hosts: webservers
tasks:- name: “安装相关组件和依赖环境”
yum: name=mariadb,mariadb-server,php,php-mysql,php-gd,php-fpm,gcc,pcre-devel,openssl-devel state=latest - name: “推送nginx源码包”
unarchive: src=nginx-1.18.0.tar.gz dest=/root/ - name: “编译安装nginx”
shell: cd /root/nginx-1.18.0 && ./configure && make && make install - name: “推送配置文件模板”
copy: src=nginx.conf dest=/usr/local/nginx/conf/nginx.conf - name: “推送测试页面”
copy: src=index.php dest=/usr/local/nginx/html/index.php - name: “启动nginx”
shell: /usr/local/nginx/sbin/nginx
ignore_errors: true - name: “启动php-fpm”
systemd: name=php-fpm state=started enabled=yes - name: “启动mariadb”
systemd: name=mariadb state=started enabled=yes
- name: “安装相关组件和依赖环境”