博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ansible最佳实战部署nginx
阅读量:4458 次
发布时间:2019-06-08

本文共 2860 字,大约阅读时间需要 9 分钟。

1、先看下整体目录架构[root@bogon ~]# cd /etc/ansible/[root@bogon ansible]# tree.├── ansible.cfg├── group_vars│   └── all├── hosts├── roles│   └── webservs│       ├── handlers│       │   └── main.yml│       ├── README.md│       ├── tasks│       │   ├── install_nginx.yaml│       │   └── main.yaml│       └── templates│           ├── index.html.j2│           └── nginx.conf.j2├── site.retry└── site.yaml6 directories, 11 files2、初始化一个role[root@bogon ~]# ansible-galaxy init /etc/ansible/roles/websrvs查看已经创建的role[root@bogon ~]# ls /etc/ansible/roles/webservs把初始化后, role里面没用的目录删除,没有的目录就创建,按照第一步的目录架构来3、配置ansible.cfg[root@bogon ansible]# cat ansible.cfg [defaults]inventory = /etc/ansible/hostssudo_user=rootremote_port=22host_key_checking=Falseremote_user=rootlog_path=/var/log/ansible.logmodule_name=commandprivate_key_file=/root/.ssh/id_rsa4、配置变量all文件,注意:名字只能写成all,写其他的就报错[root@bogon group_vars]# cat all---# vars file for /etc/ansible/roles/webservsworker_processes: 4worker_connections: 768max_open_files: 655065、配置site.yaml作为执行入口文件,里面定义都对哪些roles操作[root@bogon ansible]# cat site.yaml ---# this playbook deploy the whole application stack in this site- name: configuration and deploy webservers and application code  hosts: webservers    roles:    - webservs6、配置handlers文件 ,就是触发器,比如满足条件后启动nginx[root@bogon webservs]# cat handlers/main.yml ---# handlers file for /etc/ansible/roles/webservs- name: restart nginx  service: name=nginx state=restarted7、配置tasks, 这是具体执行操作的yaml文件[root@bogon webservs]# cat tasks/main.yaml ---- include: install_nginx.yaml[root@bogon webservs]# cat tasks/install_nginx.yaml ---# tasks file for /etc/ansible/roles/webservs- name: install nginx  command: yum install nginx -y- name: copy nginx config file  template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf  notify: restart nginx- name: copy index.html  template:    src: index.html.j2    dest: /usr/share/nginx/www/index.html    mode: 0644  notify: restart nginx- name: see file  command: ls /root  notify: restart nginx8、配置templates。  就是准备需要的模板文件,没有就不用准备[root@bogon webservs]# cat templates/nginx.conf.j2 worker_processes {
{ worker_processes }};worker_rlimit_nofile {
{ max_open_files }};events { worker_connections {
{ worker_connections }};}http { server { listen 80; root /usr/share/nginx/www; index index.html index.htm default.html index.php; server_name loclhost; location / { try_files / =404; } } }[root@bogon webservs]# cat templates/index.html.j2 welcome to american

nginx, confitured by ansible

if you can see this, ansible successfully installed nginx.

{

{ ansible_hostname }}

9、执行部署[root@bogon ansible]# lsansible.cfg group_vars hosts roles site.retry site.yaml[root@bogon ansible]# ansible-playbook site.yaml

 

转载于:https://www.cnblogs.com/effortsing/p/10286420.html

你可能感兴趣的文章
Sharepoint Solution Gallery Active Solution时激活按钮灰色不可用的解决方法
查看>>
MyBatis Generator去掉生成的注解
查看>>
教你50招提升ASP.NET性能(二十二):利用.NET 4.5异步结构
查看>>
lua连续随机数
查看>>
checkstyle使用介绍
查看>>
history.js 一个无刷新就可改变浏览器栏地址的插件(不依赖jquery)
查看>>
会了这十种Python优雅的写法,让你工作效率翻十倍,一人顶十人用!
查看>>
二维码图片生成
查看>>
在做操作系统实验的一些疑问
查看>>
Log4J日志配置详解
查看>>
NameNode 与 SecondaryNameNode 的工作机制
查看>>
Code obfuscation
查看>>
大厂资深面试官 带你破解Android高级面试
查看>>
node.js系列(实例):原生node.js实现接收前台post请求提交数据
查看>>
SignalR主动通知订阅者示例
查看>>
golang的表格驱动测试
查看>>
用python实现矩阵转置
查看>>
linux 小技巧(磁盘空间搜索)
查看>>
iOS开发——捕获崩溃信息
查看>>
(for 循环)编程找出四位整数 abcd 中满足 (ab+cd)(ab+cd)=abcd 的数
查看>>