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

ansible部署nginx:1个简单的playbook脚本

文章目录

    • hosts--ventory
    • roles
    • 执行命令

使用ansible向3台centos7服务器上安装nginx

hosts–ventory

[root@stand playhook1]# cat /root/HOSTS 
# /root/HOSTS
[webservers]
192.168.196.111 ansible_ssh_pass=password
192.168.196.112 ansible_ssh_pass=password
192.168.196.113 ansible_ssh_pass=password[docker]
192.168.196.111
192.168.196.112
192.168.196.113[docker:vars]
ansible_ssh_pass=password[ansible:children]
docker

roles

.
├── roles
│   └── nginx
│       ├── files
│       │   └── index.html
│       ├── handlers
│       │   └── main.yaml
│       ├── tasks
│       │   └── main.yaml
│       ├── templates
│       │   └── nginx.conf.j2
│       └── vars
│           └── main.yaml
└── site.yml7 directories, 6 files

[root@stand playhook1]# cat roles/nginx/files/index.html

<html><head><title>Welcome to Nginx!</title></head><body><h1>Hello, Nginx is working!</h1></body>
</html>

[root@stand playhook1]# cat roles/nginx/handlers/main.yaml

---
- name: restart nginxservice:name: nginxstate: restarted

[root@stand playhook1]# cat roles/nginx/templates/nginx.conf.j2

user  nginx;
worker_processes  {{ ansible_processor_cores }};error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;events {worker_connections  1024;
}http {include       /etc/nginx/mime.types;default_type  application/octet-stream;log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';access_log  /var/log/nginx/access.log  main;sendfile        on;#tcp_nopush     on;keepalive_timeout  65;# Gzip Settingsgzip  on;gzip_disable "msie6";include /etc/nginx/conf.d/*.conf;server {listen       80;server_name  {{ server_name }};location / {root   /usr/share/nginx/html;index  index.html index.htm;}# redirect server error pages to the static page /50x.html#error_page   500 502 503 504  /50x.html;location = /50x.html {root   /usr/share/nginx/html;}}
}

[root@stand playhook1]# cat roles/nginx/vars/main.yaml

---
ansible_processor_cores: 2
server_name: localhost

[root@stand playhook1]# cat site.yml

---- hosts: webserversroles:- { role: nginx, version: "1.24.0" }

[root@stand playhook1]# cat roles/nginx/handlers/main.yaml

---
- name: restart nginxservice:name: nginxstate: restarted

[root@stand playhook1]# cat roles/nginx/tasks/main.yaml

---
- name: Add Nginx repositoryyum_repository:name: nginxdescription: Nginx Repositorybaseurl: http://nginx.org/packages/centos/$releasever/$basearch/gpgcheck: yesenabled: yesgpgkey: https://nginx.org/keys/nginx_signing.key- name: Install nginx packageyum:name: "nginx-{{ version }}"state: present- name: Ensure Nginx html directory existsfile:path: /usr/share/nginx/htmlstate: directoryowner: nginxgroup: nginxmode: 0755- name: Copy nginx.conf Templatetemplate:src: nginx.conf.j2dest: /etc/nginx/nginx.confowner: rootgroup: rootbackup: yesmode: 0755notify: restart nginx- name: Copy index htmlcopy:src: index.htmldest: /usr/share/nginx/html/index.htmlowner: rootgroup: rootbackup: yesmode: 0755- name: Make sure nginx service is runningservice:name: nginxstate: started

执行命令

ansible-playbook -i /root/HOSTS site.yml

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
测试:
在这里插入图片描述

显示已经成功。


大功告成

http://www.lryc.cn/news/503610.html

相关文章:

  • 三、汇总统计
  • opencv实现给图像加上logo图像
  • 亚马逊云科技2024 re:Invent大会亮点:Nova大模型与AI基础设施全面升级
  • 总结与提升
  • 入门pytorch-Transformer
  • 泛型编程--
  • 【大语言模型】LangChain 核心模块介绍(Agents)
  • 19C-RAC 环境mgmtca.trc.1过大
  • 基于Spring Boot的同城宠物照看系统的设计与实现
  • 爬虫学习案例5
  • 视频监控汇聚平台方案设计:Liveweb视频智能监管系统方案技术特点与应用
  • ansible自动化运维(三)jinja2模板roles角色管理
  • 队列+宽搜_429. N 叉树的层序遍历_二叉树最大宽度
  • Windows11安装及使用nvm
  • (一)机器学习 - 入门
  • 【解决】k8s使用kubeadm初始化集群失败问题整理
  • apache-dubbo
  • ECharts柱状图-柱图2,附视频讲解与代码下载
  • 【新人系列】Python 入门(十六):正则表达式
  • HTML综合
  • 孚盟云 MailAjax.ashx SQL注入漏洞复现
  • 解决“VMware虚拟机报Intel VT-x”错误
  • NiceGUI `ui.table` 基础
  • 分布式 Raft算法 总结
  • C++ 中面向对象编程如何实现动态绑定?
  • 微服务-01
  • 这是一个vue3 + scss的数字滚动效果
  • 数字证书管理工具 openssl keytool
  • Polars数据聚合与旋转实战教程
  • 引用类型集合的深拷贝,无需手动写循环:Apache Commons Lang (SerializationUtils)