前提条件部署好jdk、tomcat
1.设置密钥服务
安装依赖
yum install -y http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
安装ansible
yum -y install ansible
写入主机清单
vim /etc/ansible/hosts

ansible IP -m ping
2.#进入/usr/local/目录
mkdir nginx_test
cd nginx_test
上传nginx压缩包
tar zxf nginx-1.22.1.tar.gz
cd nginx-1.22.1/
cp conf/nginx.conf /usr/local/nginx_test
cd /usr/local/nginx_test
vim index.html
<h1>This is web1!<h1>
3.编写nginx.yml
vim nginx.yml
---
- hosts: 192.168.147.137
tasks:
- name: "推送Nginx源码包"
unarchive: src=nginx-1.22.1.tar.gz dest=/root/
- name: "安装依赖环境库"
yum: name=gcc,gcc-c++,pcre-devel,zlib-devel state=latest
- name: "安装Nginx"
shell: cd /root/nginx-1.22.1/ && ./configure && make && make install
- name: "推送配置文件"
copy: src=nginx.conf dest=/usr/local/nginx/conf/nginx.conf
- name: "推送测试页面"
copy: src=index.html dest=/usr/local/nginx/html/index.html
- name: "启动Nginx服务"
shell: netstat -ntl | grep -qw 80 || /usr/local/nginx/sbin/nginx
- hosts: 192.168.147.137
tasks:
- name: "推送Nginx源码包"
unarchive: src=nginx-1.22.1.tar.gz dest=/root/
- name: "安装依赖环境库"
yum: name=gcc,gcc-c++,pcre-devel,zlib-devel state=latest
- name: "安装Nginx"
shell: cd /root/nginx-1.22.1/ && ./configure && make && make install
- name: "推送配置文件"
copy: src=nginx.conf dest=/usr/local/nginx/conf/nginx.conf
- name: "推送测试页面"
copy: src=index.html dest=/usr/local/nginx/html/index.html
- name: "启动Nginx服务"
shell: netstat -ntl | grep -qw 80 || /usr/local/nginx/sbin/nginx
4.执行任务
#
用于执行
Ansible playbook
。
--syntax-check
参数表示仅检查
playbook
的语法
,
不执行其中的
任务。
ansible-playbook --syntax-check nginx.yml
#
执行任务
ansible-playbook nginx.yml
然后访问
192.168.147.137:80
该文章详细描述了如何在Linux环境中,通过Ansible自动化工具进行Nginx的部署。首先确保已安装JDK和Tomcat,接着安装Epel仓库和Ansible。然后创建Nginx的目录结构,上传并解压Nginx源码,编写自定义的配置文件和测试HTML页面。通过编写Ansibleplaybook,执行包括安装依赖、编译安装Nginx、复制配置文件和测试页面以及启动Nginx服务等一系列任务。最后,验证Nginx服务是否成功启动并通过IP地址访问测试页面。
1209

被折叠的 条评论
为什么被折叠?



