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 ...
随机推荐
- Python 引用
python引用python中的数值类型变量也是引用,例如: a = 100b=a那么a和b指向同一块内存但是当修改a或者b的值得时候,Python会新分配一块内存来存储新的值 python中不可变类 ...
- C和C++的内存操作小贴士(一):const char*的内存释放问题
C和C++的内存操作一直是困扰开发人员的老问题,基本概念相信老司机们都很清楚了,在这里就不做过多的描述了,只是把在实际开发中可能遇到的一些小问题的案例列举下,供大家参考.“C和C++的内存操作小贴士” ...
- Golang (Go语言) Mac OS X下环境搭建 环境变量配置 开发工具配置 Sublime Text 2 【转】
一.安装Golang的SDK 在官网 http://golang.org/ 直接下载安装包安装即可.下载pkg格式的最新安装包,直接双击运行,一路按照提示操作即可完成安装. 安装完成后,打开终端,输入 ...
- Axiom3D:Ogre动画基本流程与骨骼动画
在Axiom中,Animation类用于管理动画,在此对象中主要管理着AnimationTrack对象,此对象用于管理动画的各种类型的每一桢.在Axiom中,动画类型主要有变形动画,姿态动画,骨骼动画 ...
- MD5骨骼动画模型加载(一)
前面我们分析了静态模型OBJ格式,桢动画模型MD2,这篇主要分析骨骼动画MD5的一些概念并且实现. 混合桢动画有计算简单,容易实现等优点,但是在需要比较细致的效果时,则需要更多的关键桢,每桢都添加相同 ...
- 【转】【MySQL】Mysql模糊查询like提速优化
在使用msyql进行模糊查询的时候,很自然的会用到like语句,通常情况下,在数据量小的时候,不容易看出查询的效率,但在数据量达到百万级,千万级的时候,查询的效率就很容易显现出来.这个时候查询的效率就 ...
- JUnit4时间(超时)测试实例
“时间测试”是指,一个单元测试运行时间是否超过指定的毫秒数,测试将终止并标记为失败. import org.junit.*; /** * JUnit TimeOut Test * @author yi ...
- 使用Maven创建Web应用程序项目
用到的技术/工具: Maven 3.3.3 Eclipse 4.3 JDK 8 Spring 4.1.1.RELEASED Tomcat 7 Logback 1.0.13 1. 从Maven模板创建W ...
- css 阻止元素中的文本。双击选中
//firefox -moz-user-select: none; //chrome.safari -webkit-user-select: none; //ie -ms-user-select: n ...
- 分布式缓存系统 Memcached 整体架构
分布式缓存系统 Memcached整体架构 Memcached经验分享[架构方向] Memcached 及 Redis 架构分析和比较