使用Ansible-playbook部署Nginx+Tomcat
2019-04-24 本文已影响101人
高多金
准备环境
测试服务器4台
192.168.14.10 操作机
192.168.14.20 主机
192.168.14.30 主机
192.168.14.40 主机
操作机
- 安装epel源
yum -y install epel-release
- 通过epel安装Ansible
yum -y install ansible
- 配置主机群组
vim /etc/ansible/hosts
#在文件最后添加
[webs]
192.168.14.10 把操作机也添加
192.168.14.20
192.168.14.30
192.168.14.40
- 使用ssh连接主机群组
#一路回车
ssh-keygen
Enter file in which to save the key (/root/.ssh/id_rsa):
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:F5ZCtmMkn3+04fsT2n+yRY/WNW3pFREYc7j1qSTg14I root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
| . + o++.|
| * + . oo..|
| O * + o.o|
| . E B * .=|
| S + B .o*|
| . . oo==|
| .ooo+|
| .o+..|
| o=o|
+----[SHA256]-----+
#输入root密码
[root@localhost ~]# ssh-copy-id 192.168.14.10
[root@localhost ~]# ssh-copy-id 192.168.14.20
[root@localhost ~]# ssh-copy-id 192.168.14.30
[root@localhost ~]# ssh-copy-id 192.168.14.40
- 测试是否连接主机群组
ansible webs -m ping
192.168.14.10 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.14.30 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.14.40 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.14.20 | SUCCESS => {
"changed": false,
"ping": "pong"
}
- 使用playbook一键部署
cd /etc/ansible
vim nginx.yml
- yml文件
---
- hosts: webs
tasks:
- name: "安装组件"
yum: name={{ item }} state=latest
with_items:
- openssl-devel
- zlib-devel
- pcre-devel
- gcc
- gcc-c++
- name: "解压源码包"
unarchive: src=nginx-1.4.7.tar.gz dest=/root
- name: "编译"
shell: chdir=/root/nginx-1.4.7 ./configure && make && make install
- name: "分发配置文件"
copy: src=nginx.conf dest=/usr/local/nginx/conf/nginx.conf
- name: "启动nginx"
shell: /usr/local/nginx/sbin/nginx
- name: "安装djk环境"
yum: name=java-1.8.0-openjdk state=latest
- name: "创建tomcat目录"
shell: mkdir /opt/tomcat
- name: "解压tomcat源码包"
unarchive: src=apache-tomcat-7.0.47.tar.gz dest=/opt/tomcat
- name: "启动tomcat"
shell: nohup /opt/tomcat/apache-tomcat-7.0.47/bin/startup.sh &
- nginx配置文件
location / {
root html;
index index.html index.htm index.jsp;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
location ~ \.jsp$ {
proxy_pass http://127.0.0.1:8080;
}
完成