1. ansible-playbook安装tomcat
1) 编写playbook的tomcat安装配置

 1 [root@test-1 bin]# vim /ansible/tomcat/bin/tomcat.yaml 
 2 [root@test-1 bin]# cat /ansible/tomcat/bin/tomcat.yaml 
 3 ---
 4 - hosts: web1
 5   remote_user: root
 6   vars:
 7     tomcat_version: 8.5.38
 8     tomcat_install_dir: /opt/
10   tasks:
11     - name: Install jdk1.8
12       yum:
13         name: java-1.8.0-openjdk
14         state: present
15     - name:  Download tomcat
16       get_url:
17         url: http://mirrors.hust.edu.cn/apache/tomcat/tomcat-8/v{{tomcat_version}}/bin/apache-tomcat-{{tomcat_version}}.tar.gz
18         dest: /tmp
19     - name:  Unarchive tomcat
20       unarchive:
21         src: /tmp/apache-tomcat-{{tomcat_version}}.tar.gz
22         dest: "{{tomcat_install_dir}}/"
23         copy: no
24     - name: Start tomcat
25       shell: cd {{tomcat_install_dir}} && mv apache-tomcat-{{tomcat_version}}  tomcat8  && cd tomcat8/bin && nohup ./startup.sh &

2)配置文件检查

1 [root@test-1 bin]# ansible-playbook  --syntax-check tomcat.yaml 
3 playbook: tomcat.yaml

3) 执行playbook的tomcat安装文件

 1 [root@test-1 bin]# ansible-playbook   tomcat.yaml 
 3 PLAY [web1] ******************************************************************************************************************************************************************************************************************************************************************
 5 TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************************************************
 6 ok: [192.168.200.132]
 7 ok: [192.168.200.133]
 9 TASK [Install jdk1.8] ********************************************************************************************************************************************************************************************************************************************************
10 changed: [192.168.200.132]
11 changed: [192.168.200.133]
13 TASK [Download tomcat] *******************************************************************************************************************************************************************************************************************************************************
14 ok: [192.168.200.133]
15 ok: [192.168.200.132]
17 TASK [Unarchive tomcat] ******************************************************************************************************************************************************************************************************************************************************
18 changed: [192.168.200.133]
19 changed: [192.168.200.132]
21 TASK [Start tomcat] **********************************************************************************************************************************************************************************************************************************************************
22 changed: [192.168.200.133]
23 changed: [192.168.200.132]
25 PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************************
26 192.168.200.132            : ok=5    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
27 192.168.200.133            : ok=5    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

2. 验证playbook的tomcat验证是否正常
1)验证端口和进程是否存在

 1 [root@test-1 bin]# ansible web1 -m shell -a " ps -ef |grep java "
 2 192.168.200.132 | CHANGED | rc=0 >>
 3 root     24559 24558  0 19:18 ?        00:00:08 /usr/bin/java -Djava.util.logging.config.file=/opt/tomcat8/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /opt/tomcat8/bin/bootstrap.jar:/opt/tomcat8/bin/tomcat-juli.jar -Dcatalina.base=/opt/tomcat8 -Dcatalina.home=/opt/tomcat8 -Djava.io.tmpdir=/opt/tomcat8/temp org.apache.catalina.startup.Bootstrap start
 4 root     25207 25202  0 19:53 pts/1    00:00:00 /bin/sh -c ps -ef |grep java 
 5 root     25209 25207  0 19:53 pts/1    00:00:00 grep java
 7 192.168.200.133 | CHANGED | rc=0 >>
 8 root     24457 24456  0 19:18 ?        00:00:08 /usr/bin/java -Djava.util.logging.config.file=/opt/tomcat8/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /opt/tomcat8/bin/bootstrap.jar:/opt/tomcat8/bin/tomcat-juli.jar -Dcatalina.base=/opt/tomcat8 -Dcatalina.home=/opt/tomcat8 -Djava.io.tmpdir=/opt/tomcat8/temp org.apache.catalina.startup.Bootstrap start
 9 root     25103 25098  0 19:53 pts/1    00:00:00 /bin/sh -c ps -ef |grep java 
10 root     25105 25103  0 19:53 pts/1    00:00:00 grep java
12 [root@test-1 bin]# ansible web1 -m shell -a " netstat -lntup |grep 8080"
13 192.168.200.132 | CHANGED | rc=0 >>
14 tcp6       0      0 :::8080                 :::*                    LISTEN      24559/java          
16 192.168.200.133 | CHANGED | rc=0 >>
17 tcp6       0      0 :::8080                 :::*                    LISTEN      24457/java  

2)curl 模拟浏览器请求是否正常访问

 1 [root@test-1 bin]# curl -I http://192.168.200.132:8080/
 2 HTTP/1.1 200 
 3 Content-Type: text/html;charset=UTF-8
 4 Transfer-Encoding: chunked
 5 Date: Thu, 22 Aug 2019 11:55:39 GMT
 7 [root@test-1 bin]# curl -I http://192.168.200.133:8080/
 8 HTTP/1.1 200 
 9 Content-Type: text/html;charset=UTF-8
10 Transfer-Encoding: chunked
11 Date: Thu, 22 Aug 2019 11:55:44 GMT