天天看点

ansible部署tocmat

说明:ansible-palybook批量部署tomcat以及tomcat所需要的jdk环境,以及分发站点文件

1.安装python

yum -y install zlib zlib-devel   openssl openssl-devel #后期安装ansible会依赖所以
wgethttp://www.python.org/ftp/python/2.7.11/Python-2.7.11.tgz
gunzip Python-2.7.11.tgz   #查看解压的结果
tar  xf  Python-2.7.11.tar
cd Python-2.7.11
mkdir  -p  /application/python2.7.11
./configure --prefix=/application/python2.7.11
make
make install
mv /usr/bin/python /usr/bin/python_old
ln  -s  /application/python2.7.11/bin/python2   /usr/bin/python      

2.python安装需要解决的问题

错误1

[root@haixiao Python-2.7.11]# yum -y  install lrzsz
/usr/bin/python: error while loading sharedlibraries: libpython2.7.so.1.0: cannot open shared object file: No such file ordirectory
解决办法:
vim /usr/bin/yum
#!/usr/bin/python 改成     /usr/bin/python_old
错误2
[root@haixiao bin]# python  -V
python: error while loading shared libraries:libpython2.7.so.1.0: cannot open shared object file: No such file or directory
解决办法:
echo "/application/python2.7.6/lib">>/etc/ld.so.conf
 ldconfig
 python -V      

3.安装pip命令为了安装特定版本的ansible

wget https://bootstrap.pypa.io/get-pip.py
pip命令在python安装的路径下的bin下
/opt/service/python2.7.11/bin
所以:
把python的这个环境变量导进入
export PATH=/opt/service/python2.7.11/bin:$PATH
python  get-pip.py      

4.安装ansible

 pip   install     ansible==2.0.2      

5.ansible基础的命令就不讲解了,直接在另外一台服务器上安装tomcat

ansible分发机:10.0.0.9 内网Ip:172.16.1.9
ansible被分发机:10.0.0.10 内网IP:172.16.1.10  #在此台机器上安装tomcat      

6.在10.0.0.9

1.

[root@centos67 ~]# cd /opt/lansgg/ansible_playbook/
[root@centos67 ansible_playbook]# pwd
/opt/lansgg/ansible_playbook      

2.

[root@centos67 ansible_playbook]# tree

.
├── hosts      
├── roles
│   └── http
│       ├── default
│       ├── files
│       │  ├── apache-tomcat-8.0.27.tar.gz
│       │  └── jdk-8u101-linux-x64.rpm
│       ├── handlers
│       │  └── main.yml
│       ├── meta
│       │  └── test
│       │       └── index.html
│       ├── tasks
│       │  └── main.yml
│       ├── template
│       │  └── test.html
│       └── vars
└── site.yml
 
10 directories, 8 files      

[root@centos67 ansible_playbook]#

3.

[root@centos67 ansible_playbook]# cat site.yml
---
- name: Install tomcat application prod enviroment
  hosts:tomcat
  become:True
  roles:
    - http
[root@centos67 ansible_playbook]# cat hosts
[tomcat]
172.16.1.10
[root@centos67 ansible_playbook]#      

4.

[root@centos67 tasks]# pwd
/opt/lansgg/ansible_playbook/roles/http/tasks
[root@centos67 tasks]# cat main.yml
- name: 将jdk压缩包copy到远程主机jdk-7u72-linux-x64.tar.gz
  copy:src=jdk-8u101-linux-x64.rpm dest=/opt/lansgg/
- name: 解压jdk压缩包jdk-7u72-linux-x64.tar.gz
  yum:name=/opt/lansgg/jdk-8u101-linux-x64.rpm  state=present
- name: 将tomcat压缩包copy到远程主机apache-tomcat-7.0.68.tar.gz
  copy:src=apache-tomcat-8.0.27.tar.gz dest=/opt/lansgg
- name: 解压tomcat压缩包
  shell:chdir=/opt/lansgg tar zxvf apache-tomcat-8.0.27.tar.gz
- name: 做软连接
  shell:chdir=/opt/lansgg ln  -sapache-tomcat-8.0.27 tomcat
- name: 将tomcat压缩包 重新命名为 tomcat
  shell:chdir=/opt/lansgg mv apache-tomcat-8.0.27.tar.gz /tmp/
- name: 首次启动tomcat
  shell:chdir=/opt/lansgg/tomcat/bin nohup ./startup.sh &
- name: 将 test 工程 copy 到远程主机
  copy:src=/opt/lansgg/ansible_playbook/roles/http/      
meta/testdest=/opt/lansgg/tomcat/webapps/
  tags:
   -updateconf
  notify:
   - stop tomcat service
   - starttomcat service
- name: 将某些模板文件 copy 到远程主机      
  template:src=/opt/lansgg/ansible_playbook/roles/http/template/test.htmldest=/opt/lansgg/tomcat/webapps/test/test.html      

5

[root@centos67 handlers]# pwd
/opt/lansgg/ansible_playbook/roles/http/handlers
[root@centos67 handlers]#
[root@centos67 handlers]# cat main.yml
#- name: stop tomcat service
#  command:"ps -ef |grep /opt/lansgg/tomcat |grep -v grep |awk '{print $2}' |xargskill -9"
- name: start tomcat service
  shell:chdir=/opt/lansgg/tomcat/bin nohup ./startup.sh &
[root@centos67 handlers]#      
[root@centos67 ansible_playbook]# pwd
/opt/lansgg/ansible_playbook
[root@centos67 ansible_playbook]#ansible-playbook  -i  hosts site.yml      
[root@centos67 handlers]# curlhttp://172.16.1.10:8080/test/test.html
1234
[root@centos67 handlers]#