ansible playbook 部署 Tomcat、Nginx、maven、Keepalived

在这里插入图片描述

  • 实验环境:

​ 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 [email protected][root@localhost ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected][root@localhost ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected][root@localhost ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]

3) 编辑 hosts 清单

	[root@localhost ~]# vim /etc/ansible/hosts 

        [tomcatservers]
        192.168.116.103
        192.168.116.104
        
        [tomcat-1]
        192.168.116.103
        
        [tomcat-2]
        192.168.116.104

        [nginxservers]
        192.168.116.105
        192.168.116.106

        [keepalived-m]
        192.168.116.105

        [keepalived-s]
        192.168.116.106

4) 编写Tomcat playbook

- hosts: tomcatservers
  remote_user: root
  tasks:
    - name: tar java
      unarchive: src=/root/jdk-8u201-linux-x64.tar.gz dest=/usr/src
    - name: mv java
      shell: mv /usr/src/jdk1* /usr/local/java
    - name: bina liang
      lineinfile: path=/etc/bashrc insertafter=EOF line="export JAVA_HOME=/usr/local/java"
    - name: bina liang
      lineinfile: path=/etc/bashrc insertafter=EOF line="export JRE_HOME=/usr/local/java/jre"
    - name: bina liang
      lineinfile: path=/etc/bashrc insertafter=EOF line="export CLASSPATH=$JAVA_HOME/lib:$JRE_HOME/lib"
    - name: bina liang
      lineinfile: path=/etc/bashrc  insertafter=EOF line="export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME:/bin"
    - name: source bashrc
      shell: source /etc/bashrc
    - name: jie tomcat
      unarchive: src=/root/apache-tomcat-8.5.35.tar.gz dest=/usr/src
    - name:  move tomcat
      shell: mv /usr/src/apache-tomcat* /usr/local/tomcat
    - name: tar maven
      unarchive: src=/root/apache-maven-3.6.0-bin.tar.gz dest=/usr/src
    - name: mv maven
      shell: 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.xml
      file: path=/usr/local/maven/conf/settings.xml state=absent
    - name: aliyun
      copy: src=/root/settings.xml dest=/usr/local/maven/conf/
    - name: create webapp
      shell: mvn archetype:generate -DgroupId=cn.kgc.kgcweb -DartifactId=kgcweb -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
      args:
        chdir: /root
    - name: mvn package 
      shell: mvn package
      args:
        chdir: /root/kgcweb
    - name: copy kgcwev.war
      copy: src=/root/kgcweb/target/kgcweb.war dest=/usr/local/tomcat/webapps/ remote_src=yes
    - name: copy sh
      copy: src=/root/tomcat_start.sh dest=/root/
    - name: start tomcat
      shell: sh /root/tomcat_start.sh



- hosts: tomcat-1
  remote_user: root
  tasks:
    - name: update index.jsp
      lineinfile: path=/usr/local/tomcat/webapps/kgcweb/index.jsp insertafter="hello world" line="This is the Tomcat-1 server"

- hosts: tomcatservers
  remote_user: root
  tasks:
    - name: start tomcat
      shell: sh /root/tomcat_start.sh

修改 aliyun maven仓库

aliyun maven 仓库模板

5) 部署 Nginx

- hosts: nginxservers
  remote_user: root
  tasks:
    - name:  tar nginx
      unarchive: src=/root/nginx-1.12.2.tar.gz dest=/usr/src
    - name: yum
      yum: name=pcre*,openssl-devel,pcre-devel,zlib-devel
    - name: create user nginx
      user: name=nginx create_home=no shell=/usr/sbin/nologin state=presion state=present
    - name: make && make install
      shell: ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-pcre && make && make install
      args:
        chdir: /usr/src/nginx-1.12.2
    - name: create link
      file: src=/usr/local/nginx/sbin/nginx  dest=/usr/sbin/nginx state=link
    - name: upadte nginx.conf
      lineinfile: "path=/usr/local/nginx/conf/nginx.conf insertafter='gzip  on;' line='upstream www.tomcat.servers {'" 
    - name: uodate nginx.conf
      lineinfile: "path=/usr/local/nginx/conf/nginx.conf insertafter='^upstream www.tomcat.servers {$' line='server 192.168.116.103:8080 weight=1;'"
    - name: uodate nginx.conf
      lineinfile: "path=/usr/local/nginx/conf/nginx.conf insertafter='^upstream www.tomcat.servers {$' line='server 192.168.116.104:8080 weight=1;'"
    - name: update nginx.conf
      shell: sed -i "/server 192.168.116.103:8080 weight=1;/a\}" /usr/local/nginx/conf/nginx.conf
    - name: update nginx.conf
      replace: path=/usr/local/nginx/conf/nginx.conf regexp="            root   html;" replace=           #root   html;
    - name: update nginx.conf
      replace: path=/usr/local/nginx/conf/nginx.conf regexp="            index  index.html index.htm;" replace=            #index  index.html index.htm;
    - name: update nginx.conf
      lineinfile: "path=/usr/local/nginx/conf/nginx.conf insertafter='^        location / {$' line='          proxy_pass http://www.tomcat.servers/kgcweb/index.jsp;'"
    - name: start nginx
      shell: nginx


6) 部署 Keeplived

- hosts: nginxservers
  remote_user: root
  tasks:
    - name: yum keepaliv
      yum: name=popt-devel,kernel-devel,openssl-devel
    - name: tar keepalived
      unarchive: src=/root/keepalived-1.2.13.tar.gz dest=/usr/src
    - name: make && make install
      shell: ./configure --prefix=/ --with-kernel-dir=/usr/src/kernel && make && make install
      args:
        chdir: /usr/src/keepalived-1.2.13

- hosts: keepalived-m
  remote_user: root
  tasks:
    - name: copy
      copy: src=/etc/keepalived/keepalived.conf dest=/etc/keepalived/
    - name: start keepalived
      shell: service keepalived start
- hosts: keepalived-s
  remote_user: root
  tasks:
    - name: copy
      copy: src=/root/keepalived.conf dest=/etc/keepalived/
    - name: start keepalived
      shell: 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>
发布了7 篇原创文章 · 获赞 0 · 访问量 112

猜你喜欢

转载自blog.csdn.net/RunzIyy/article/details/104471614