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.yaml 6 directories, 11 files 2、初始化一个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/hosts
sudo_user=root
remote_port=22
host_key_checking=False
remote_user=root
log_path=/var/log/ansible.log
module_name=command
private_key_file=/root/.ssh/id_rsa 4、配置变量all文件,注意:名字只能写成all,写其他的就报错 [root@bogon group_vars]# cat all
---
# vars file for /etc/ansible/roles/webservs
worker_processes: 4
worker_connections: 768
max_open_files: 65506 5、配置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:
- webservs 6、配置handlers文件 ,就是触发器,比如满足条件后启动nginx [root@bogon webservs]# cat handlers/main.yml
---
# handlers file for /etc/ansible/roles/webservs
- name: restart nginx
service: name=nginx state=restarted 7、配置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 nginx 8、配置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
<html>
<head>
<title>welcome to american</title>
</head>
<body>
<h1>nginx, confitured by ansible</h1>
<p>if you can see this, ansible successfully installed nginx.</p> <p>{{ ansible_hostname }}</p>
</body>
</html> 9、执行部署 [root@bogon ansible]# ls
ansible.cfg group_vars hosts roles site.retry site.yaml
[root@bogon ansible]# ansible-playbook site.yaml

ansible最佳实战部署nginx的更多相关文章

  1. Git+Gitlab+Ansible的roles实现一键部署Nginx静态网站(一)--技术流ken

    前言 截止目前已经写了<Ansible基础认识及安装使用详解(一)--技术流ken>,<Ansible常用模块介绍及使用(二)--技术流ken><Ansible剧本介绍及 ...

  2. Ansible实战之Nginx代理Tomcat主机架构

    author:JevonWei 版权声明:原创作品 实验架构:一台nginx主机为后端两台tomcat主机的代理,并使用Ansible主机配置 实验环境 Nginx 172.16.252.82 Tom ...

  3. Git+Gitlab+Ansible的roles实现一键部署Nginx静态网站(4)

    前言 截止目前已经写了<Ansible基础认识及安装使用详解(一)–技术流ken>,<Ansible常用模块介绍及使用(二)–技术流ken><Ansible剧本介绍及使用 ...

  4. [svc]NFS存储企业场景及nfs最佳实战探究

    办公网络里人一般系统用共享,尤其是财务, 他们喜欢直接点开编辑. 而不喜欢ftp nfs在网站架构中的用途 注: 如果pv量少,则放在一台机器上速度更快,如果几千万pv,则存储分布式部署. 网站架构中 ...

  5. 最佳实战Docker持续集成图文详解

    最佳实战Docker持续集成图文详解 这是一种真正的容器级的实现,这个带来的好处,不仅仅是效率的提升,更是一种变革:开发人员第一次真正为自己的代码负责——终于可以跳过运维和测试部门,自主维护运行环境( ...

  6. k8s pod的4种网络模式最佳实战(externalIPs )

    [k8s]k8s pod的4种网络模式最佳实战(externalIPs )       hostPort相当于docker run -p 8081:8080,不用创建svc,因此端口只在容器运行的vm ...

  7. https证书最佳实战目录

    [svc]openssl对称加密,非对称加密最佳实战 http://blog.csdn.net/iiiiher/article/details/78593464 [svc]证书的生成和各个字段的含义 ...

  8. Puppet单机实战之Nginx代理Tomcat

    author:JevonWei 版权声明:原创作品 blog:http://119.23.52.191/ --- 构建实战之Nginx代理Tomcat [root@node1 modules]# mk ...

  9. docker(部署常见应用):docker部署nginx

    上回说到centos安装docker. 这次用实战,docker部署运行常见的应用. docker常用命令 参看:docker命令大全.这里不做赘述. docker部署nginx 1.docker h ...

随机推荐

  1. zencart目录结构

    zencart目录结构 文件路径 注释 index.php 主文件 includes/templates/[custom template folder]/common/html_header.php ...

  2. 设置ecShop网店用户名和email均可登录

    修改user.php文件,如果您网站的该文件修改过,按照下面的修改说明修改文件. 查找代码:elseif ($action == 'act_login') 在:$back_act = isset($_ ...

  3. Mongodb的mapreduce

    简单的看了一下mapreduce,我尝试不看详细的api去做一个group效果,结果遇到了很多问题,罗列在这里,如果别人也遇到了类似的bug,可以检索到结果. //先看person表的数据 > ...

  4. package+explorer不显示项目的问题

    昨天遇到了这个问题,百度了一下,怎么搞的都有,但是感觉都不理想,晚上的时候才在网上又发现这个方法,今天试了一下,效果还不错,分享一下. 点击 Window ---> Close All Pers ...

  5. qt5--模态对话框和非模态对话框

    视频教程:https://www.bilibili.com/video/av51766541/?p=20 #include "win.h" #include <QMenuBa ...

  6. 【leetcode】Department Top Three Salaries

    The Employee table holds all employees. Every employee has an Id, and there is also a column for the ...

  7. Java多线程和并发(十一),CAS(Compare and Swap)

    目录 1.CAS简介 2.CAS多数情况下对开发者来说是透明的 3.CAS缺点 十一.CAS(Compare and Swap) Synchronized直观意义上是一种悲观锁 cas则是乐观锁的一种 ...

  8. postfix -- 发件调试

    按照教程(https://www.cnblogs.com/huandada/p/10554603.html)搭建好postfix之后,由于收件的邮件运营商的限制,部分邮件不能正常发送,需要更多其他配置 ...

  9. 9.Python关键字(保留字)一览表

    保留字是 Python 语言中一些已经被赋予特定意义的单词,这就要求开发者在开发程序时,不能用这些保留字作为标识符给变量.函数.类.模板以及其他对象命名. Python 包含的保留字可以执行如下命令进 ...

  10. Android_(游戏)打飞机01:前言

    (游戏)打飞机01:前言 传送门 (游戏)打飞机02:游戏背景滚动 传送门 (游戏)打飞机03:控制玩家飞机 传送门 (游戏)打飞机04:绘画敌机.添加子弹   传送门 (游戏)打飞机05:处理子弹, ...