1. ansible-初始playbook安装nginx

1) 创建一个ansible存放路径

1 [root@test-1 scripts]# mkdir -p /ansible/nginx/{conf,bin}

2) 验证存放路径

1 [root@test-1 bin]# tree /ansible/
2 /ansible/
3 └── nginx
4 ├── bin
5 │   └── nginx.yaml
6 └── conf
7 └── site.conf
8
9 3 directories, 2 files

2. 编写playbook的nginx安装yaml文件

1) 编写nginx.yaml配置文件

 1 [root@test-1 bin]# vim /ansible/nginx/bin/nginx.yaml
2 [root@test-1 bin]#cat /ansible/nginx/bin/nginx.yaml
3 - hosts: web1
4 remote_user: root
5 vars:
6 hello: Ansible
7
8 tasks:
9 - name: Add repo
10 yum_repository:
11 name: nginx
12 description: nginx repo
13 baseurl: http://nginx.org/packages/centos/7/$basearch/
14 gpgcheck: no
15 enabled: 1
16 - name: Install nginx
17 yum:
18 name: nginx
19 state: latest
20 - name: Copy nginx configuration file
21 copy:
22 src: /ansible/nginx/conf/site.conf
23 dest: /etc/nginx/conf.d/site.conf
24 - name: Start nginx
25 service:
26 name: nginx
27 state: started
28 - name: Create wwwroot directory
29 file:
30 dest: /var/www/html
31 state: directory
32 - name: Create test page index.html
33 shell: echo "hello {{hello}}" > /var/www/html/index.html

2) 编写nginx的配置文件

 1 [root@test-1 bin]# vim /ansible/nginx/conf/site.conf
2 [root@test-1 bin]# cat /ansible/nginx/conf/site.conf
3 server {
4 listen 80;
5 server_name www.ctnrs.com;
6 location / {
7 root /var/www/html;
8 index index.html;
9 }
10 }

3. 验证playbook是否正确

1) 验证

1 [root@test-1 bin]# ansible-playbook /ansible/nginx/bin/nginx.yaml  --syntax-check
2 [WARNING]: log file at /var/log/ansible/ansible.log is not writeable and we cannot create it, aborting
3
4
5 playbook: /ansible/nginx/bin/nginx.yaml

提示:
       验证配置文件正确,没问题

4. 执行ansible-playbook远程安装

1) 执行安装nginx

 1 [root@test-1 bin]# ansible-playbook /ansible/nginx/bin/nginx.yaml
2
3 PLAY [web1] ******************************************************************************************************************************************************************************************************************************************************************
4
5 TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************************************************
6 ok: [192.168.200.132]
7 ok: [192.168.200.133]
8
9 TASK [Add repo] **************************************************************************************************************************************************************************************************************************************************************
10 ok: [192.168.200.132]
11 ok: [192.168.200.133]
12
13 TASK [Install nginx] *********************************************************************************************************************************************************************************************************************************************************
14 changed: [192.168.200.132]
15 changed: [192.168.200.133]
16
17 TASK [Copy nginx configuration file] *****************************************************************************************************************************************************************************************************************************************
18 ok: [192.168.200.132]
19 ok: [192.168.200.133]
20
21 TASK [Start nginx] ***********************************************************************************************************************************************************************************************************************************************************
22 changed: [192.168.200.133]
23 changed: [192.168.200.132]
24
25 TASK [Create wwwroot directory] **********************************************************************************************************************************************************************************************************************************************
26 ok: [192.168.200.132]
27 ok: [192.168.200.133]
28
29 TASK [Create test page index.html] *******************************************************************************************************************************************************************************************************************************************
30 changed: [192.168.200.133]
31 changed: [192.168.200.132]
32
33 PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************************
34 192.168.200.132 : ok=7 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
35 192.168.200.133 : ok=7 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

5. 验证是否部署成功
  1) 验证

 1 [root@test-1 bin]# ansible   web1 -m shell -a "ps -ef |grep nginx"
2 [WARNING]: log file at /var/log/ansible/ansible.log is not writeable and we cannot create it, aborting
3
4 192.168.200.132 | CHANGED | rc=0 >>
5 root 17792 1 0 15:04 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
6 nginx 17793 17792 0 15:04 ? 00:00:00 nginx: worker process
7 root 17970 17965 68 15:22 pts/1 00:00:00 /bin/sh -c ps -ef |grep nginx
8 root 17972 17970 0 15:22 pts/1 00:00:00 grep nginx
9
10 192.168.200.133 | CHANGED | rc=0 >>
11 root 17881 1 0 15:04 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
12 nginx 17882 17881 0 15:04 ? 00:00:00 nginx: worker process
13 root 18058 18053 73 15:22 pts/1 00:00:00 /bin/sh -c ps -ef |grep nginx
14 root 18060 18058 0 15:22 pts/1 00:00:00 grep nginx
15
16
17 [root@test-1 bin]# ansible web1 -m shell -a "netstat -lntup |grep 80"
18 [WARNING]: log file at /var/log/ansible/ansible.log is not writeable and we cannot create it, aborting
19
20 192.168.200.132 | CHANGED | rc=0 >>
21 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 17792/nginx: master
22
23 192.168.200.133 | CHANGED | rc=0 >>
24 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 17881/nginx: master

2) 模拟浏览器请求

1 [root@test-1 scripts]# curl 192.168.200.132 -H "Host:www.ctnrs.com"
2 hello Ansible
3 [root@test-1 scripts]# curl 192.168.200.133 -H "Host:www.ctnrs.com"
4 hello Ansible

ansible-初始playbook安装nginx的更多相关文章

  1. Ansible 使用 Playbook 安装 Nginx

    思路:先在一台机器上编译安装好 Nginx,打包,然后通过 Ansible 下发 [root@localhost ~]$ cd /etc/ansible/ [root@localhost ansibl ...

  2. ansible roles实践——安装nginx

    1.创建roles 在/etc/ansible/roles目录下 1.1 手动创建需要的目录 1.2 使用命令创建,用不到的目录可以创建为空目录,但不可以不创建. 创建目录[root@master] ...

  3. Ansible 使用 Playbook 管理 Nginx 配置文件

    前面我们已经安装完 Nginx,但是在日常维护中经常需要修改配置文件,并重新加载配置文件,因此来写一个管理 Nginx 配置文件的 Playbook: [root@localhost ~]$ mkdi ...

  4. Ansible 之Playbook

    ansbile playbook是一系列ansible命令的集合,利用yaml 语言编写,playbook命令根据自上而下的顺序依次执行.同时,playbook开创了很多特性,它可以允许你传输某个命令 ...

  5. Linux centosVMware 自动化运维Ansible介绍、Ansible安装、远程执行命令、拷贝文件或者目录、远程执行脚本、管理任务计划、安装rpm包/管理服务、 playbook的使用、 playbook中的循环、 playbook中的条件判断、 playbook中的handlers、playbook实战-nginx安装、管理配置文件

    一.Ansible介绍 不需要安装客户端,通过sshd去通信 基于模块工作,模块可以由任何语言开发 不仅支持命令行使用模块,也支持编写yaml格式的playbook,易于编写和阅读 安装十分简单,ce ...

  6. ansible的playbook进行yum批量安装nginx最新版本

    环境:centos7 版本:nginx最新版本 软件: ansible 作用: 进行批量执行不同机器上,进行安装nginx版本 检查脚本是否正确: [root@ansible-test ansible ...

  7. ansible案例-安装nginx

    一.创建目录: mkidr -p playbook/{files,templates}   二.自定义index.html文件 $ vim playbook/templates/index.html. ...

  8. 在CentOS7.6上安装自动化运维工具Ansible以及playbook案例实操

    前言 Ansible是一款优秀的自动化IT运维工具,具有远程安装.远程部署应用.远程管理能力,支持Windows.Linux.Unix.macOS和大型机等多种操作系统. 下面就以CentOS 7.6 ...

  9. playbook实现nginx安装

    1. 先在一台机器上编译安装好nginx,然后打包 tar -zcvf nginx.tar.gz /usr/local/nginx --exclude=conf/nginx.conf --exclud ...

随机推荐

  1. 【Gin-API系列】部署和监控(九)

    本文是[Gin-API系列]的最后一篇文章,简单介绍如何在生产环境的部署架构和监控手段. 生产部署 部署架构 使用Nginx加Keepalived的方式搭建,可以达到高可用的效果,并可以横向扩容 如何 ...

  2. 安装与激活PhpStrom

    1.下载安装PHPstorm(https://pan.baidu.com/s/1nwK3ew5 密码dhtr) 2.运行 3.下一步选择中间,在http://idea.lanyus.com/ 上获取激 ...

  3. 来讲讲你对ThreadLocal的理解

    前言 面试的时候被问到ThreadLocal的相关知识,没有回答好(奶奶的,现在感觉问啥都能被问倒),所以我决定先解决这几次面试中都遇到的高频问题,把这几个硬骨头都能理解的透彻的说出来了,感觉最起码不 ...

  4. python基础一(安装、变量、循环、git)

    一.开发语言分类 系统的开发语言有java.c++.c#.python.ruby.php等等,开发语言可分为编译型语言和解释型语言. 编译型语言就是写好代码之后就把代码编译成二进制文件,运行的时候运行 ...

  5. 解决spark streaming集成kafka时只能读topic的其中一个分区数据的问题

    1. 问题描述 我创建了一个名称为myTest的topic,该topic有三个分区,在我的应用中spark streaming以direct方式连接kakfa,但是发现只能消费一个分区的数据,多次更换 ...

  6. JVM垃圾回收器前瞻

    垃圾回收器的新发展   GC仍然处于飞速发展之中,目前的默认选项G1 GC在不断的进行改进,很多我们原来认为的缺点,例如串行的Full GC.Card Table扫描的低效等,都已经被大幅改进,例如, ...

  7. 用友yonsuite产品二开之简单的yonsql查询小工具

    和以往的用友产品不同,yonsuite产品开发了低代码平台,满足客户的个性化开发需求.嗯~,一句话不知当讲不当讲,那就讲:所谓低代码平台就是开发不想用实施不会用系列.让我一个开发感受到了憋屈.

  8. 【答疑解惑】为什么你的 Charles 会抓包失败?

    作为一名 Web 开发工程师,天天都会和网络打交道.Charles 作为一款网络抓包工具,几乎成了 Web 开发的标配. 本文是我深度使用 Charles 后总结而成,不同于其它介绍 Charles ...

  9. Redis的五大数据类型以及key的相关操作命令

    Redis的五大数据类型 redis的数据都是以key/value存储,所以说,五大类型指的是value的数据类型 String 字符串,作为redis的最基本数据类型 redis中的string类型 ...

  10. Java源码赏析(一)Object 类

    写这个系列的原因,其实网上已经有无数源码分析的文章了,多一篇不多,少一篇不少,但为什么还要写这部分文章呢?于私,其一,上班族已经很久没有打过完整的一整段有意义的话,算是锻炼个人的书写.总结能力,其二, ...