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. win redis安装

    一.下载windows版本的Redis 去官网找了很久,发现原来在官网上可以下载的windows版本的,现在官网以及没有下载地址,只能在github上下载,官网只提供linux版本的下载 官网下载地址 ...

  2. mysql5.7.26部署MHA

    前期准备: mysql先部署好GTID主从,然后才部署MHA 1)环境准备(所有节点) #安装依赖包 yum install perl-DBD-MySQL -y #进入安装包存放目录 [root@my ...

  3. K8S集群组件

    master节点主要由apiserver.controller-manager和scheduler三个组件,以及一个用于集群状态存储的etcd存储服务组成,而每个node节点则主要包含kubelet. ...

  4. Newsgroups数据集研究

    1.数据集介绍 20newsgroups数据集是用于文本分类.文本挖据和信息检索研究的国际标准数据集之一. 数据集收集了大约20,000左右的新闻组文档,均匀分为20个不同主题的新闻组集合. 一些新闻 ...

  5. Hive中的日志

    Hive中的日志分为两种1. 系统日志,记录了hive的运行情况,错误状况.2. Job 日志,记录了Hive 中job的执行的历史过程. 1.系统日志 系统日志存储在什么地方呢 ?在hive/con ...

  6. alembic在tornado项目中的应用

    在项目中引用alembic 协助tornado项目生成数据表结构 alembic revision --autogenerate -m "create tables" 第二步执行 ...

  7. 洛谷-P3389-高斯消元模板

    链接: https://www.luogu.org/problem/P3389 题意: 给定一个线性方程组,对其求解 思路: 高斯消元,从第一项消到最后一项,消成一个上三角矩阵.再从最后一项依次向上回 ...

  8. WTL自定义控件:需要的头文件

    这两天自定义了一个Edit控件,继承自CEdit,如下: class CCheckEditEx : public CWindowImpl< CCheckEditEx, CEdit > 需要 ...

  9. C# 匿名对象 增加属性

    dynamic data = new System.Dynamic.ExpandoObject(); IDictionary<string, object> dictionary = (I ...

  10. [Flask]celery异步任务队列的使用

    Celery异步任务队列 目录结构树: 配置文件config.py: # 设置中间人地址 broker_url = 'redis://127.0.0.1:6379/1' 主main.py: impor ...