当前位置: 代码迷 >> 综合 >> ansible playbook 部署 Tomcat、Nginx、maven、Keepalived
  详细解决方案

ansible playbook 部署 Tomcat、Nginx、maven、Keepalived

热度:54   发布时间:2024-01-04 20:58:30.0

在这里插入图片描述

  • 实验环境:

? ansible: 192.168.116.101/24

? nexus私服: 192.168.116.102/24

? Tomcat1: 192.168.116.103/24

? Tomcat2: 192.168.116.104/24

? Nginx1: 192.168.116.105/24

? Nginx2: 192.168.116.106/24

? keeplived: 192.168.116.150/24

? keeplived-M: 192.168.116.105/24

? keeplived-S: 192.168.116.105/24

1. 部署ansible

? 通过 ansible playbook 实现以上服务 的安装与配置

1)安装部署 ansible

	[root@localhost ~]# yum -y install epel-release 		# 安装系统扩展源?	[root@localhost ~]#  yum makecache fast					# 新建缓存?	[root@localhost ~]# yum -y install ansible				# 安装ansible

·

2) 部署 SSH 免密登陆

?

	[root@localhost ~]# ssh-keygen ?	[root@localhost ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.116.103?	[root@localhost ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.116.104?	[root@localhost ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.116.105?	[root@localhost ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.116.106

3) 编辑 hosts 清单

	[root@localhost ~]# vim /etc/ansible/hosts [tomcatservers]192.168.116.103192.168.116.104[tomcat-1]192.168.116.103[tomcat-2]192.168.116.104[nginxservers]192.168.116.105192.168.116.106[keepalived-m]192.168.116.105[keepalived-s]192.168.116.106

4) 编写Tomcat playbook

?

- hosts: tomcatserversremote_user: roottasks:- name: tar javaunarchive: src=/root/jdk-8u201-linux-x64.tar.gz dest=/usr/src- name: mv javashell: mv /usr/src/jdk1* /usr/local/java- name: bina lianglineinfile: path=/etc/bashrc insertafter=EOF line="export JAVA_HOME=/usr/local/java"- name: bina lianglineinfile: path=/etc/bashrc insertafter=EOF line="export JRE_HOME=/usr/local/java/jre"- name: bina lianglineinfile: path=/etc/bashrc insertafter=EOF line="export CLASSPATH=$JAVA_HOME/lib:$JRE_HOME/lib"- name: bina lianglineinfile: path=/etc/bashrc  insertafter=EOF line="export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME:/bin"- name: source bashrcshell: source /etc/bashrc- name: jie tomcatunarchive: src=/root/apache-tomcat-8.5.35.tar.gz dest=/usr/src- name:  move tomcatshell: mv /usr/src/apache-tomcat* /usr/local/tomcat- name: tar mavenunarchive: src=/root/apache-maven-3.6.0-bin.tar.gz dest=/usr/src- name: mv mavenshell: mv /usr/src/apache-maven-3.6.0 /usr/local/maven- name: bina liang lineinfile: path=/etc/bashrc insertafter=EOF line="export PATH=$PATH:/usr/local/maven/bin"- name: delete settings.xmlfile: path=/usr/local/maven/conf/settings.xml state=absent- name: aliyuncopy: src=/root/settings.xml dest=/usr/local/maven/conf/- name: create webappshell: mvn archetype:generate -DgroupId=cn.kgc.kgcweb -DartifactId=kgcweb -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=falseargs:chdir: /root- name: mvn package shell: mvn packageargs:chdir: /root/kgcweb- name: copy kgcwev.warcopy: src=/root/kgcweb/target/kgcweb.war dest=/usr/local/tomcat/webapps/ remote_src=yes- name: copy shcopy: src=/root/tomcat_start.sh dest=/root/- name: start tomcatshell: sh /root/tomcat_start.sh- hosts: tomcat-1remote_user: roottasks:- name: update index.jsplineinfile: path=/usr/local/tomcat/webapps/kgcweb/index.jsp insertafter="hello world" line="This is the Tomcat-1 server"- hosts: tomcatserversremote_user: roottasks:- name: start tomcatshell: sh /root/tomcat_start.sh

?

修改 aliyun maven仓库

aliyun maven 仓库模板

5) 部署 Nginx

?

- hosts: nginxserversremote_user: roottasks:- name:  tar nginxunarchive: src=/root/nginx-1.12.2.tar.gz dest=/usr/src- name: yumyum: name=pcre*,openssl-devel,pcre-devel,zlib-devel- name: create user nginxuser: name=nginx create_home=no shell=/usr/sbin/nologin state=presion state=present- name: make && make installshell: ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-pcre && make && make installargs:chdir: /usr/src/nginx-1.12.2- name: create linkfile: src=/usr/local/nginx/sbin/nginx  dest=/usr/sbin/nginx state=link- name: upadte nginx.conflineinfile: "path=/usr/local/nginx/conf/nginx.conf insertafter='gzip on;' line='upstream www.tomcat.servers {'" - name: uodate nginx.conflineinfile: "path=/usr/local/nginx/conf/nginx.conf insertafter='^upstream www.tomcat.servers {$' line='server 192.168.116.103:8080 weight=1;'"- name: uodate nginx.conflineinfile: "path=/usr/local/nginx/conf/nginx.conf insertafter='^upstream www.tomcat.servers {$' line='server 192.168.116.104:8080 weight=1;'"- name: update nginx.confshell: sed -i "/server 192.168.116.103:8080 weight=1;/a\}" /usr/local/nginx/conf/nginx.conf- name: update nginx.confreplace: path=/usr/local/nginx/conf/nginx.conf regexp=" root html;" replace=           #root html;- name: update nginx.confreplace: path=/usr/local/nginx/conf/nginx.conf regexp=" index index.html index.htm;" replace=            #index index.html index.htm;- name: update nginx.conflineinfile: "path=/usr/local/nginx/conf/nginx.conf insertafter='^ location / {$' line=' proxy_pass http://www.tomcat.servers/kgcweb/index.jsp;'"- name: start nginxshell: nginx

6) 部署 Keeplived

?

- hosts: nginxserversremote_user: roottasks:- name: yum keepalivyum: name=popt-devel,kernel-devel,openssl-devel- name: tar keepalivedunarchive: src=/root/keepalived-1.2.13.tar.gz dest=/usr/src- name: make && make installshell: ./configure --prefix=/ --with-kernel-dir=/usr/src/kernel && make && make installargs:chdir: /usr/src/keepalived-1.2.13- hosts: keepalived-mremote_user: roottasks:- name: copycopy: src=/etc/keepalived/keepalived.conf dest=/etc/keepalived/- name: start keepalivedshell: service keepalived start
- hosts: keepalived-sremote_user: roottasks:- name: copycopy: src=/root/keepalived.conf dest=/etc/keepalived/- name: start keepalivedshell: service keepalived start

Keepalived-M 模板

Keepalived-S 模板

7) 验证

?

[root@localhost ~]# curl 192.168.116.150
<html>
<body>
<h2>Hello World!</h2>
This is Tomcaat server
</body>
</html>
[root@localhost ~]# curl 192.168.116.150
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
  相关解决方案