目录
前言
Ansible 是一个开源的自动化平台,用于配置管理、应用部署和自动化任务执行
将手动部署的操作转换成yaml的形式,使用一条ansible的命令,就可以一键部署操作完成
提供诸多便利,提高工作运维效率
以下,我将通过role角色来实现LNMT环境的部署
电脑环境:MacBook pro apple m1pro
虚拟机环境:Ubuntu22 (ARM)
一、使用role角色实现Nginx部署
(一)手动部署Nginx操作流程
## 编译安装nginx
## 官网下载安装包:
root@nginx-40:/usr/local/src#wget http://nginx.org/download/nginx-1.25.5.tar.gz
## 下载安装依赖包
root@nginx-40:/usr/local/src# apt-update
root@nginx-40:/usr/local/src# apt-get install make -y
root@nginx-40:/usr/local/src# apt-get install make-guile -y
root@nginx-40:/usr/local/src# apt-get install gcc -y
root@nginx-40:/usr/local/src# apt-get install libpcre3 -y
root@nginx-40:/usr/local/src# apt-get install libpcre3-dev -y
root@nginx-40:/usr/local/src# apt-get install libssl-dev -y
root@nginx-40:/usr/local/src# apt-get install zlib1g-dev -y
## 添加nginx用户和用户组
root@nginx-40:/usr/local/src# groupadd -g 88 nginx
root@nginx-40:/usr/local/src# useradd -g nginx -M -s /sbin/nologin -u 88 nginx
## 解压并进入目录中
root@nginx-40:/usr/local/src# tar -xf nginx-1.25.5.tar.gz
root@nginx-40:/usr/local/src# cd nginx-1.25.5/
## 编译
root@nginx-40:/usr/local/src/nginx-1.25.5# ./configure --user=nginx --group=nginx \
--prefix=/usr/local/nginx --with-http_stub_status_module \
--with-http_sub_module --with-http_ssl_module \
--with-pcre --with-stream
## 安装
root@nginx-40:/usr/local/src/nginx-1.25.5# make && make install
## 编译后查看
root@nginx-40:/usr/local/src/nginx-1.25.5# cd /usr/local/nginx/
root@nginx-40:/usr/local/nginx# ls
conf html logs sbin
## 启动、停止、重载服务
root@nginx-40:/usr/local/nginx# /usr/local/nginx/sbin/nginx
root@nginx-40:/usr/local/nginx# /usr/local/nginx/sbin/nginx -s stop
root@nginx-40:/usr/local/nginx# /usr/local/nginx/sbin/nginx -s reload
(二)手动部署转换ansible的部署
# 创建角色
root@admin-111:/data/playbook/roles# ansible-galaxy init nginx-install
- Role mysql-intall was created successfully
# 默认没有 templates 和file 目录 -- 需要自行创建
root@admin-111:/data/playbook/roles/nginx-install# mkdir templates files
# 目录架构
root@admin-111:/data/playbook/roles# tree
.
├── nginx-install # role角色
│ ├── README.md
│ ├── defaults
│ │ └── main.yml
│ ├── files
│ │ └── nginx-1.25.5.tar.gz
│ ├── handlers
│ │ └── main.yml
│ ├── meta
│ │ └── main.yml
│ ├── tasks
│ │ └── main.yml
│ ├── templates
│ │ ├── index.html.j2
│ │ └── nginx.conf.j2
│ ├── tests
│ │ ├── inventory
│ │ └── test.yml
│ └── vars
│ └── main.yml
└── nginx_install.yaml # 主文件
# 主文件:
指定执行role的角色 - ansible-galaxy创建的角色名称
指定执行的权限,root
root@admin-111:/data/playbook/roles# cat nginx_install.yaml
---
- hosts: nginx1
remote_user: root
roles:
- nginx-install # 指定role角色
# 全局变量定义
root@admin-111:/data/playbook/roles/nginx-install# cat vars/main.yml
# vars file for nginx-install
# file_dir: /data/playbook/nginx-1.25.5.tar.gz
nginx_ops: "/usr/local/src/configure --user=nginx \
--group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module \
--with-http_sub_module --with-http_ssl_module --with-pcre --with-stream"
# 主任务,主要执行的操作步骤
root@admin-111:/data/playbook/roles# cat nginx-install/tasks/main.yml
---
# tasks file for nginx-install
- name: update package
shell: apt-get update- name: install package
apt:
name: ["make","make-guile", "gcc","libpcre3","libpcre3-dev","zlib1g-dev","libssl-dev","zlib1g-dev"]
state: present
# disable_gpg_check: yes- name: create group
group:
name: nginx
state: present
gid: 80- name: create user
user:
name: nginx
uid: 80
group: nginx
system: yes
shell: /sbin/nologin- name: 解压并推送到目录中
unarchive:
src: nginx-1.25.5.tar.gz
dest: /usr/local/src- name: 切换进入目录进行编译
# shell: cd /usr/local/src/nginx-1.25.5 && "{ { nginx_ops }}" && make && make install
shell: ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx \
--with-http_stub_status_module --with-http_sub_module \
--with-http_ssl_module --with-pcre --with-stream &&
make && make install
args:
chdir: /usr/local/src/nginx-1.25.5
creates: /usr/local/nginx/sbin/nginx # 确定不存在,避免重复编译
become: yes # 确保root权限编译- name: 提供配置文件模版
template:
src: nginx.conf.j2
dest: /usr/local/nginx/conf/nginx.conf
owner: nginx
group: nginx
mode: 644- name: 创建访问目录
file:
path: "{ { item }}"
state: directory
loop:
- /data/web-data/hello.com
- /data/web-data/logs- name: 提供访问界面
template:
src: index.html.j2
dest: /data/web-data/hello.com/index.htnl
owner: root
group: root
mode: 644
notify: start_nginx # 如果task没有发生变化,不会执行,只有第一次执行- name: check port
shell: netstat -nutlp
register: seeport- name: 输出查看端口
debug:
msg={ { seeport }}
# 提供app,安装文件
root@admin-111:/data/playbook/roles/nginx-install# ls files/
nginx-1.25.5.tar.gz
# 编写调度器,task任务中如有调用才会执行
root@admin-111:/data/playbook/roles/nginx-install# cat handlers/main.yml---
# handlers file for nginx-install
- name: start_nginx
shell:
free_form: /usr/local/nginx/sbin/nginx
removes: /usr/local/nginx/sbin/nginx # 文件不存在则不执行- name: check port
shell: netstat -nutlp
## 提供配置文件,一般配置文件模版都是放在templates目录中,以.j2形式存放
root@admin-111:/data/playbook/roles/nginx-install# ls templates/
index.html.j2 nginx.conf.j2
#&

4097

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



