Ansible 使用 Playbook 安装 Nginx
思路:先在一台机器上编译安装好 Nginx,打包,然后通过 Ansible 下发
[root@localhost ~]$ cd /etc/ansible/
[root@localhost ansible]$ mkdir nginx_install
[root@localhost ansible]$ cd nginx_install/
[root@localhost nginx_install]$ mkdir -p roles/{common,install}/{handlers,files,meta,tasks,templates,vars}
[root@localhost nginx_install]$ tree ./
./
└── roles # 定义角色目录,common为一些准备操作,install为安装nginx的操作
├── common
│ ├── files # 存放安装包的目录
│ ├── handlers # 存放当发生变更是要执行的操作,如修改配置文件、重启服务等
│ ├── meta # 存放说明信息,如说明角色依赖等信息
│ ├── tasks # 存放核心的任务配置文件
│ ├── templates # 存放配置文件、启动文件等模板文件
│ └── vars # 用来定义变量的目录
└── install
├── files
├── handlers
├── meta
├── tasks
├── templates
└── vars
编译安装 Nginx :
[root@localhost ~]$ cd /usr/local/src/
[root@localhost ~]$ yum install -y pcre-devel zlib-devel gcc gcc-c++
[root@localhost src]$ wget http://nginx.org/download/nginx-1.12.2.tar.gz
[root@localhost src]$ tar xf nginx-1.12.2.tar.gz
[root@localhost src]$ cd nginx-1.12.2/
[root@localhost nginx-1.12.2]$ ./configure --prefix=/usr/local/nginx
[root@localhost nginx-1.12.2]$ make && make install
[root@localhost nginx-1.12.2]$ vim /etc/init.d/nginx # https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/etc_init.d_nginx
[root@localhost nginx-1.12.2]$ chmod 755 /etc/init.d/nginx
[root@localhost nginx-1.12.2]$ chkconfig --add nginx
[root@localhost nginx-1.12.2]$ chkconfig nginx on
[root@localhost nginx-1.12.2]$ cd /usr/local/nginx/conf/
[root@localhost conf]$ mv nginx.conf nginx.conf.bak
[root@localhost conf]$ vim nginx.conf # https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/nginx.conf
[root@localhost conf]$ /usr/local/nginx/sbin/nginx -t
[root@localhost conf]$ /etc/init.d/nginx start
[root@localhost conf]$ netstat -tunlp | grep 80
打包:
[root@localhost ~]$ cd /etc/ansible/nginx_install/roles/install/files/
[root@localhost files]$ tar czf nginx.tar.gz --exclude "nginx.conf" --exclude "vhost" /usr/local/nginx
拷贝配置文件和启动文件到模板目录:
[root@localhost ~]$ cp /usr/local/nginx/conf/nginx.conf /etc/ansible/nginx_install/roles/install/templates/
[root@localhost ~]$ cp /etc/init.d/nginx /etc/ansible/nginx_install/roles/install/templates/
安装一些依赖包:
[root@localhost ~]$ cd /etc/ansible/nginx_install/roles/
[root@localhost roles]$ cat common/tasks/main.yml
- name: Install initializtion require software
yum: name={{ item }} state=installed
with_items:
- zlib-devel
- pcre-devel
定义一些变量:
[root@localhost roles]$ cat install/vars/main.yml
nginx_user: www
nginx_port: 80
nginx_basedir: /usr/local/nginx
把用到的文档拷贝到目标机器:
[root@localhost roles]$ cat install/tasks/copy.yml
- name: Copy Nginx Software
copy: src=nginx.tar.gz dest=/tmp/nginx.tar.gz owner=root group=root - name: Uncompression Nginx Software
shell: tar zxf /tmp/nginx.tar.gz -C /usr/local/ - name: Copy Nginx Start Script
template: src=nginx dest=/etc/init.d/nginx owner=root group=root mode=0755 - name: Copy Nginx Config
template: src=nginx.conf dest={{ nginx_basedir }}/conf/ owner=root group=root mode=0644
创建用户 ,启动服务 ,删除压缩包:
[root@localhost roles]$ cat install/tasks/install.yml
- name: Create Nginx User
user: name={{ nginx_user }} state=present createhome=no shell=/sbin/nologin - name: Start Nginx Service
shell: /etc/init.d/nginx start - name: Add Boot Start Nginx Service
shell: chkconfig --level 345 nginx on - name: Delete Nginx compression files
shell: rm -rf /tmp/nginx.tar.gz
创建 main.yml ,去调用 copy.yml 和 install.yml:
[root@localhost roles]$ cat install/tasks/main.yml
- include: copy.yml
- include: install.yml
最后,创建总入口文件:
[root@localhost ~]$ cat /etc/ansible/nginx_install/install.yml
---
- hosts: 192.168.119.134
remote_user: root
gather_facts: True
roles:
- common
- install
执行 yaml 安装 Nginx:
[root@localhost ~]$ ansible-playbook /etc/ansible/nginx_install/install.yml
Ansible 使用 Playbook 安装 Nginx的更多相关文章
- ansible-初始playbook安装nginx
1. ansible-初始playbook安装nginx 1) 创建一个ansible存放路径 1 [root@test-1 scripts]# mkdir -p /ansible/nginx/{co ...
- ansible roles实践——安装nginx
1.创建roles 在/etc/ansible/roles目录下 1.1 手动创建需要的目录 1.2 使用命令创建,用不到的目录可以创建为空目录,但不可以不创建. 创建目录[root@master] ...
- Ansible 使用 Playbook 管理 Nginx 配置文件
前面我们已经安装完 Nginx,但是在日常维护中经常需要修改配置文件,并重新加载配置文件,因此来写一个管理 Nginx 配置文件的 Playbook: [root@localhost ~]$ mkdi ...
- Ansible 之Playbook
ansbile playbook是一系列ansible命令的集合,利用yaml 语言编写,playbook命令根据自上而下的顺序依次执行.同时,playbook开创了很多特性,它可以允许你传输某个命令 ...
- Linux centosVMware 自动化运维Ansible介绍、Ansible安装、远程执行命令、拷贝文件或者目录、远程执行脚本、管理任务计划、安装rpm包/管理服务、 playbook的使用、 playbook中的循环、 playbook中的条件判断、 playbook中的handlers、playbook实战-nginx安装、管理配置文件
一.Ansible介绍 不需要安装客户端,通过sshd去通信 基于模块工作,模块可以由任何语言开发 不仅支持命令行使用模块,也支持编写yaml格式的playbook,易于编写和阅读 安装十分简单,ce ...
- ansible的playbook进行yum批量安装nginx最新版本
环境:centos7 版本:nginx最新版本 软件: ansible 作用: 进行批量执行不同机器上,进行安装nginx版本 检查脚本是否正确: [root@ansible-test ansible ...
- ansible案例-安装nginx
一.创建目录: mkidr -p playbook/{files,templates} 二.自定义index.html文件 $ vim playbook/templates/index.html. ...
- 在CentOS7.6上安装自动化运维工具Ansible以及playbook案例实操
前言 Ansible是一款优秀的自动化IT运维工具,具有远程安装.远程部署应用.远程管理能力,支持Windows.Linux.Unix.macOS和大型机等多种操作系统. 下面就以CentOS 7.6 ...
- playbook实现nginx安装
1. 先在一台机器上编译安装好nginx,然后打包 tar -zcvf nginx.tar.gz /usr/local/nginx --exclude=conf/nginx.conf --exclud ...
随机推荐
- 初步认识 LESS,我要开始学习LESS啦!
LESS 是一个流行的样式表语言,它提供了 CSS3 也未曾实现的多种功能,让您编写 CSS 更加方便,更加直观.LESS 已经被广泛使用在多种框架中 ( 例如:BootStrap).本文将介绍 LE ...
- 【转】一个小妙招能让你在服装上省下好多rmb
朋友们,你们仔细算过自己每年在淘宝上买衣服消费了多少rmb吗?100?1000?10000?甚至更多? 朋友们,你知道淘宝上大多数店铺的衣服是哪里来的吗? 朋友们,你知道怎么在这上面能节省更多的mon ...
- Java注释规范整理
Version:0.9 StartHTML:-1 EndHTML:-1 StartFragment:00000099 EndFragment:00018736 在软件开发的过程中总是强调注释的规范,但 ...
- 清华DNS
DNS: 166.111.8.28 166.111.8.29 FQ方式:http://www.cnblogs.com/huangshiyu13/p/6227068.html
- selenium+java+chrome环境搭建
我只能说因为版本冲突,简直太折腾了,而搜了无数个博友的帖子才找到正确条案,就不能好好的写篇文章吗? 最近真的是太闲太闲了,平时没事总得搞点技术,不然心里感觉好空虚, 最近看上了selenium,所以试 ...
- e832. 从JTabbedPane中移动卡片
To move a tab, it must first be removed and then reinserted into the tabbed pane as a new tab. Unfor ...
- ubuntu 安装 SVN 后的错误:Subversion Native Library Not Available & Incompatible JavaHL library loaded
问题一 安装了SVN的eclipse插件,使用的时候就会弹出一个错误的提示框: Subversion Native Library Not Available,加载不到JavaHL. 解决方法 ...
- c# 创建压缩包并下载文件
//DLL using ICSharpCode.SharpZipLib.Core;using ICSharpCode.SharpZipLib.Zip; public void DownloadZipF ...
- springBoot配置文件application.properties
# =================================================================== # COMMON SPRING BOOT PROPERTIE ...
- python pip 更换国内安装源(windows)
1.点击此电脑,在最上面的的文件夹窗口输入 : %APPDATA% 2.按回车跳转到以下目录,新建pip文件夹 3.创建pip.ini文件 4.打开文件夹,输入以下内容,关闭即可(注意:源镜像可替换) ...