当前位置: 首页 > news >正文

ansible自动化playbook简单实践

方法一:部分使用ansible

基于现有的nginx配置文件,定制部署nginx软件,将我们的知识进行整合 定制要求:
启动用户:nginx-test,uid是82,系统用户,不能登录
启动端口82 web项目根目录/data/webserver/html
默认首页:index.html
首页内容:"welcome to ansible"

1.1 准备工作 

  • 前提:三台系统都是rocky
    检查系统:确保三台主机的 Rocky 系统已联网,能正常安装软件包。
  • 关闭防火墙和 SELinux (测试环境建议关闭,生产环境按需配置规则):
    关闭防火墙:systemctl stop firewalld;systemctl disable firewalld

永久关闭 SELinux(需重启生效):编辑 /etc/selinux/config 文件,将 SELINUX=enforcing 改为 SELINUX=disabled 

1.2 创建启动用户

在每台主机上执行以下命令创建 nginx-test 用户,且设置为系统用户、不能登录:
useradd -u 82 -s /sbin/nologin nginx-test

1.3 安装 Nginx

yum install nginx -y  

1.4 配置 Nginx

修改配置文件:打开 Nginx 的主配置文件(yum 安装一般在 /etc/nginx/nginx.conf)

user nginx-test;  # 修改启动用户
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;include /etc/nginx/modules-enabled/*.conf;events {worker_connections 1024;
}http {server {listen 82;  # 修改监听端口为82server_name _;root /data/webserver/html;  # 设置项目根目录index index.html;  # 设置默认首页location / {try_files $uri $uri/ =404;}}include /etc/nginx/mime.types;default_type application/octet-stream;sendfile on;tcp_nopush on;tcp_nodelay on;keepalive_timeout 65;gzip on;
}
  • 创建项目根目录及首页文件
    mkdir -p /data/webserver/html
    echo "welcome to ansible" > /data/webserver/html/index.html
    chown -R nginx-test:nginx-test /data/webserver/html  # 设置目录所有者

1.5 启动 Nginx 

systemctl start nginx;systemctl enable nginx # 设置开机自启

1.6 测试访问

创建nginx_deploy.yml文件并输入下面的内容并使用 ansible-playbook nginx_deploy.yml 执行

- hosts: 10.0.0.12,10.0.0.15,10.0.0.18become: truetasks:- name: Create nginx-test useruser:name: nginx-testuid: 82shell: /sbin/nologin- name: Install nginxyum:name: nginxstate: present- name: Configure nginx.conflineinfile:path: /etc/nginx/nginx.confregexp: "{{ item.regexp }}"line: "{{ item.line }}"create: trueloop:- { regexp: "^user", line: "user nginx-test;" }- { regexp: "^listen", line: "listen 82;" }- { regexp: "^root", line: "root /data/webserver/html;" }- { regexp: "^index", line: "index index.html;" }- name: Create web project root directoryfile:path: /data/webserver/htmlstate: directoryowner: nginx-testgroup: nginx-test- name: Create index.htmlcopy:content: "welcome to ansible"dest: /data/webserver/html/index.htmlowner: nginx-testgroup: nginx-test- name: Start nginxservice:name: nginxstate: startedenabled: true

方法二:完全用ansible实现自动化

注意:按照方法一 前提环境已部署好,防护墙,selinux等 这里就不再操作

1.1 制作⼀个nginx.conf

yum install nginx -y;systemctl stop nginx
mkdir /data/ansible/nginx -p;cd /data/ansible/nginx/
grep -Ev '#|^$' /etc/nginx/nginx.conf > nginx.conf
并且进行定制修改,修改内容如下
sed -i 's#www-data#nginx-test#' nginx.conf
cat > nginx-define.conf <<- eof
server {listen 10086;root /data/webserver/html;location / {index index.html;  # 添加默认首页try_files $uri $uri/ =404;  # 添加请求处理规则}
}

1.2 编写playbook   

编写 nginx playbook ⽂件 01-nginx.yml
- hosts: webremote_user: roottasks:- name: create new useruser:name: nginx-testsystem: yesuid: 82shell: /sbin/nologin- name: create web rootfile:name: /data/webserver/htmlowner: nginx-teststate: directory- name: touch web indexshell: echo '<h1>welcome to ansible</h1>' > /data/webserver/html/index.html- name: install packageyum:name: nginxstate: present- name: copy configcopy:src: nginx.confdest: /etc/nginx/nginx.conf- name: copy subconfigcopy:src: nginx-define.confdest: /etc/nginx/conf.d- name: start serviceservice:name: nginxstate: startedenabled: yes

1.3 检测执行效果

ansible-playbook 01-nginx.yml --syntax-check    检测效果
ansible-playbook 01-nginx.yml -C      模拟执行
注:该步骤执行即使存在一两个异常,也不影响后续的正常安装
ansible-playbook 01-nginx.yml   执行文件
ansible web -m shell -a "getent passwd | grep 82"
ansible web -m shell -a "ls -l /data/"
ansible web -m shell -a "netstat -tnulp | grep nginx"  

若出现都是80端口,则还需要执行  ansible web -m shell -a "systemctl reload nginx"

 

ansible web -m shell -a "curl -s localhost:10086"

1.4 清除环境

ansible web - m service - a "name=nginx state=stopped"
ansible web - m yum  - a "name=nginx,nginx-common state=absent"
ansible web - m file - a "path=/data/webserver state=absent"
ansible web - m user - a "name=nginx-test state=absent"

相关文章:

  • MSTNet:用于糖尿病视网膜病变分类的多尺度空间感知 Transformer 与多实例学习方法|文献速递-深度学习医疗AI最新文献
  • C++高级编程深度指南:内存管理、安全函数、递归、错误处理、命令行参数解析、可变参数应用与未定义行为规避
  • 面试题 08.08. 有重复字符串的排列组合【 力扣(LeetCode) 】
  • 低功耗架构突破:STM32H750 与 SD NAND (存储芯片)如何延长手环续航至 14 天
  • OCC笔记:BRepMesh_IncrementalMesh的使用
  • 用Git管理你的服务器配置文件与自动化脚本:版本控制、变更追溯、团队协作与安全回滚的运维之道
  • day 26 函数专题
  • 尚硅谷redis7 90-92 redis集群分片之集群扩容
  • day41 python图像识别任务
  • Unity 中实现首尾无限循环的 ListView
  • 机房网络设备操作安全管理制度
  • OpenCV中的重要、常用知识点汇总(图像处理、特征检测与匹配、图像分割与轮廓分析、视频处理与分析和机器学习与深度学习等)
  • 云服务器系统日志占满磁盘怎么办?
  • 下一代液晶显示底层技术与九天画芯的技术突围
  • 5. 算法与分析 (2)
  • 企业应用AI对向量数据库选型思考
  • 【python深度学习】Day 40 训练和测试的规范写法
  • 深入解析Go语言数据类型:从底层到高级应用
  • 历年浙江大学计算机保研上机真题
  • 0-EATSA-GNN:基于图节点分类师生机制的边缘感知和两阶段注意力增强图神经网络(code)
  • 本地常州微信网站建设/建站平台哪个好
  • 西樵网站设计/广告开户南京seo
  • 网站的百度地图怎么做的/微信小程序排名关键词优化
  • 门户网站是以什么为主/2023年的新闻时事热点论文
  • 透明度 宁波政府网站建设/百度竞价最低点击一次多少钱
  • 政府网站建设模板 html/现在什么app引流效果好