当前位置 : 主页 > 操作系统 > centos >

ansible部署tomcat及 include机制

来源:互联网 收集:自由互联 发布时间:2022-06-20
1、本文尝试着使用ansible部署tomcat,test为测试工程 roles如下: [lansgg@node1ansible_playbook]$pwd/opt/lansgg/ansible_playbook[lansgg@node1ansible_playbook]$lshostsrolessite.yml[lansgg@node1ansible_playbook]$tree.├──

1、本文尝试着使用ansible部署tomcat,test为测试工程

roles如下:

[lansgg@node1 ansible_playbook]$ pwd /opt/lansgg/ansible_playbook [lansgg@node1 ansible_playbook]$ ls hosts  roles  site.yml [lansgg@node1 ansible_playbook]$ tree . ├── hosts ├── roles │   └── http │       ├── default │       ├── files │       │   ├── apache-tomcat-7.0.68.tar.gz │       │   ├── jdk-7u72-linux-x64.tar.gz │       │   └── ROOT │       │       └── test.html │       ├── handlers │       │   └── main.yml │       ├── meta │       │   └── test │       │       └── test.html │       ├── tasks │       │   ├── bak.yml │       │   └── main.yml │       ├── templates │       │   └── test.yml │       └── vars └── site.yml 11 directories, 10 files [lansgg@node1 ansible_playbook]$

main.yml 如下:

[lansgg@node1 tasks]$ cat main.yml  - name: 将jdk压缩包copy到远程主机 jdk-7u72-linux-x64.tar.gz   copy: src=jdk-7u72-linux-x64.tar.gz dest=/opt/lansgg/ - name: 解压jdk压缩包 jdk-7u72-linux-x64.tar.gz   shell: chdir=/opt/lansgg tar zxvf jdk-7u72-linux-x64.tar.gz - name: 将tomcat压缩包copy到远程主机 apache-tomcat-7.0.68.tar.gz   copy: src=apache-tomcat-7.0.68.tar.gz dest=/opt/lansgg - name: 解压tomcat压缩包   shell: chdir=/opt/lansgg tar zxvf apache-tomcat-7.0.68.tar.gz - name: 将tomcat压缩包 重新命名为 tomcat   shell: chdir=/opt/lansgg mv apache-tomcat-7.0.68 tomcat - name: 将用户的 .bashrc 模板文件 copy 到远程主机   copy: src=.bashrc dest=`ansible_user_dir`/ - name: 将用户的 .bash_profile 模板文件 copy 到远程主机   copy: src=.bash_profile dest=`ansible_user_dir`/ - name: 首次启动tomcat   shell: chdir=`ansible_user_dir`/tomcat/bin nohup ./startup.sh & - name: 将 test 工程 copy 到远程主机   copy: src=/opt/lansgg/ansible_playbook/roles/http/meta/test dest=`ansible_user_dir`/tomcat/webapps/   tags:     - updateconf   notify:    - stop tomcat service    - start tomcat service - name: 将某些模板文件 copy 到远程主机   template: src=test.yml dest=`ansible_user_dir`/tomcat/webapps/test/test.html

handlers:

[lansgg@node1 handlers]$ more main.yml  - name: stop tomcat service   shell: "ps -ef |grep `ansible_user_dir`/tomcat |grep -v grep |awk '{print $2}' |xargs kill -9" - name: start tomcat service   shell: chdir=`ansible_user_dir`/tomcat/bin nohup ./startup.sh &

执行结果:

[lansgg@node1 ansible_playbook]$ ansible-playbook -i hosts site.yml PLAY [http] *******************************************************************  GATHERING FACTS ***************************************************************  ok: [192.168.100.131] TASK: [http | 将jdk压缩包copy到远程主机 jdk-7u72-linux-x64.tar.gz] ***  changed: [192.168.100.131] TASK: [http | 解压jdk压缩包 jdk-7u72-linux-x64.tar.gz] *******************  changed: [192.168.100.131] TASK: [http | 将tomcat压缩包copy到远程主机 apache-tomcat-7.0.68.tar.gz] ***  changed: [192.168.100.131] TASK: [http | 解压tomcat压缩包] ******************************************  changed: [192.168.100.131] TASK: [http | 将tomcat压缩包 重新命名为 tomcat] **********************  changed: [192.168.100.131] TASK: [http | 将用户的 .bashrc 模板文件 copy 到远程主机] *********  changed: [192.168.100.131] TASK: [http | 将用户的 .bash_profile 模板文件 copy 到远程主机] ***  changed: [192.168.100.131] TASK: [http | 首次启动tomcat] *********************************************  changed: [192.168.100.131] TASK: [http | 将 test 工程 copy 到远程主机] ***************************  changed: [192.168.100.131] TASK: [http | 将某些模板文件 copy 到远程主机] *********************  changed: [192.168.100.131] NOTIFIED: [http | stop tomcat service] ****************************************  changed: [192.168.100.131] NOTIFIED: [http | start tomcat service] ***************************************  changed: [192.168.100.131] PLAY RECAP ********************************************************************  192.168.100.131            : ok=13   changed=12   unreachable=0    failed=0    [lansgg@node1 ansible_playbook]$

核实测试主机:

[lansgg@node1 ansible_playbook]$ ansible -i hosts http -m shell -a 'ps -ef|grep tomcat |grep -v grep' 192.168.100.131 | success | rc=0 >> lansgg   54034     1  6 13:09 ?        00:00:06 /opt/lansgg/jdk1.7.0_72/jre/bin/java -Djava.util.logging.config.file=/opt/lansgg/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/opt/lansgg/tomcat/endorsed -classpath /opt/lansgg/tomcat/bin/bootstrap.jar:/opt/lansgg/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/opt/lansgg/tomcat -Dcatalina.home=/opt/lansgg/tomcat -Djava.io.tmpdir=/opt/lansgg/tomcat/temp org.apache.catalina.startup.Bootstrap start [lansgg@node1 ansible_playbook]$[lansgg@node1 ansible_playbook]$ curl http://192.168.100.131:8080/test/test.html this is a test  hostname:v2.lansgg.com abcdefg [lansgg@node1 ansible_playbook]$

测试更新操作:


1.1、修改测试文件:

[lansgg@node1 ansible_playbook]$ echo "abcdefg" >> roles/http/meta/test/test.html

执行:

[lansgg@node1 ansible_playbook]$ ansible-playbook -i hosts site.yml --tags=updateconf PLAY [http] *******************************************************************  GATHERING FACTS ***************************************************************  ok: [192.168.100.131] TASK: [http | 将 test 工程 copy 到远程主机] ***************************  changed: [192.168.100.131] NOTIFIED: [http | stop tomcat service] ****************************************  changed: [192.168.100.131] NOTIFIED: [http | start tomcat service] ***************************************  changed: [192.168.100.131] PLAY RECAP ********************************************************************  192.168.100.131            : ok=4    changed=3    unreachable=0    failed=0    [lansgg@node1 ansible_playbook]$  [lansgg@node1 ansible_playbook]$ curl http://192.168.100.131:8080/test/test.html this is a test  hostname:hostname 1111111111111111111111111111 [lansgg@node1 ansible_playbook]$ ansible -i hosts http -m shell -a 'ps -ef|grep tomcat |grep -v grep' 192.168.100.131 | success | rc=0 >> lansgg   54236     1 24 13:11 ?        00:00:05 /opt/lansgg/jdk1.7.0_72/jre/bin/java -Djava.util.logging.config.file=/opt/lansgg/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/opt/lansgg/tomcat/endorsed -classpath /opt/lansgg/tomcat/bin/bootstrap.jar:/opt/lansgg/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/opt/lansgg/tomcat -Dcatalina.home=/opt/lansgg/tomcat -Djava.io.tmpdir=/opt/lansgg/tomcat/temp org.apache.catalina.startup.Bootstrap start [lansgg@node1 ansible_playbook]$

2、ansible 中的include 机制

测试目的,可以随意指定要操作的主机组 和 要操作的状态

[lansgg@node1 ansible_playbook]$ pwd /opt/lansgg/ansible_playbook [lansgg@node1 ansible_playbook]$ tree . ├── hosts ├── roles │   └── test │       └── tasks │           ├── del.yml │           ├── main.yml │           └── touch.yml └── test.yml 3 directories, 5 files [lansgg@node1 ansible_playbook]$

tasks:

[lansgg@node1 ansible_playbook]$ cat roles/test/tasks/touch.yml  - name: touch file   shell: touch /tmp/123 [lansgg@node1 ansible_playbook]$ cat roles/test/tasks/del.yml  - name: del file   shell: rm /tmp/123 [lansgg@node1 ansible_playbook]$ cat roles/test/tasks/main.yml  - name: include touch yml   include: touch.yml   when: "state=='touch'" - name: include del yml   include: del.yml   when: "state=='del'" [lansgg@node1 ansible_playbook]$  [lansgg@node1 ansible_playbook]$ cat test.yml  - hosts: '`testhost`'   remote_user: lansgg   roles:    - test [lansgg@node1 ansible_playbook]$ cat hosts [http] 192.168.100.131 [db] 192.168.100.132 [lansgg@node1 ansible_playbook]$

执行并查看结果:

[lansgg@node1 ansible_playbook]$ ansible-playbook -i hosts test.yml --extra-vars "testhost=http state=touch" PLAY [http] *******************************************************************  GATHERING FACTS ***************************************************************  ok: [192.168.100.131] TASK: [test | touch file] *****************************************************  changed: [192.168.100.131] TASK: [test | del file] *******************************************************  skipping: [192.168.100.131] PLAY RECAP ********************************************************************  192.168.100.131            : ok=2    changed=1    unreachable=0    failed=0

上面是创建文件,我们也可以删除操作

[lansgg@node1 ansible_playbook]$ ansible-playbook -i hosts test.yml --extra-vars "testhost=http state=del" PLAY [http] *******************************************************************  GATHERING FACTS ***************************************************************  ok: [192.168.100.131] TASK: [test | touch file] *****************************************************  skipping: [192.168.100.131] TASK: [test | del file] *******************************************************  changed: [192.168.100.131] PLAY RECAP ********************************************************************  192.168.100.131            : ok=2    changed=1    unreachable=0    failed=0    [lansgg@node1 ansible_playbook]$ ansible -i hosts http -m shell -a 'ls -l /tmp/' 192.168.100.131 | success | rc=0 >> total 0 [lansgg@node1 ansible_playbook]$

可以以命令式的方式控制要操作的主机及状态 (对 db 主机组操作)

[lansgg@node1 ansible_playbook]$ ansible-playbook -i hosts test.yml --extra-vars "testhost=db state=touch" PLAY [db] *********************************************************************  GATHERING FACTS ***************************************************************  ok: [192.168.100.132] TASK: [test | touch file] *****************************************************  changed: [192.168.100.132] TASK: [test | del file] *******************************************************  skipping: [192.168.100.132] PLAY RECAP ********************************************************************  192.168.100.132            : ok=2    changed=1    unreachable=0    failed=0    [lansgg@node1 ansible_playbook]$ ansible -i hosts db -m shell -a 'ls -l /tmp/' 192.168.100.132 | success | rc=0 >> total 0 -rw-rw-r-- 1 lansgg lansgg 0 Mar  9 16:20 123 [lansgg@node1 ansible_playbook]$

删除操作:

[lansgg@node1 ansible_playbook]$ ansible-playbook -i hosts test.yml --extra-vars "testhost=db state=del" PLAY [db] *********************************************************************  GATHERING FACTS ***************************************************************  ok: [192.168.100.132] TASK: [test | touch file] *****************************************************  skipping: [192.168.100.132] TASK: [test | del file] *******************************************************  changed: [192.168.100.132] PLAY RECAP ********************************************************************  192.168.100.132            : ok=2    changed=1    unreachable=0    failed=0    [lansgg@node1 ansible_playbook]$ ansible -i hosts db -m shell -a 'ls -l /tmp/' 192.168.100.132 | success | rc=0 >> total 0 [lansgg@node1 ansible_playbook]$


上一篇:Linux文件查找工具之find “大宝剑”
下一篇:没有了
网友评论