ansible-初始playbook安装nginx
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的更多相关文章
- Ansible 使用 Playbook 安装 Nginx
思路:先在一台机器上编译安装好 Nginx,打包,然后通过 Ansible 下发 [root@localhost ~]$ cd /etc/ansible/ [root@localhost ansibl ...
- 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 ...
随机推荐
- 模拟CMOS集成电路 课后习题总结(2.1)
前几天开始自学拉扎维的模设教材,看之前浏览了EETOP论坛里面好多大神们对这本书的看法,当然也有人在抱怨,比如冒出“太科幻”.“一年才看完”之类恐怖的修饰语句,因此在开始看的时候就对此书充满了“敬畏” ...
- Spring.Net依赖注入(属性注入)学习笔记
一.前言: Spring.Net是Java开源框架迁移过来的,主要分为 1)依赖注入 2)面向方面编程 3)数据访问抽象 4)Asp.Net扩展 四个模块功能,这里只是简单介绍依赖注入模块功能. 对于 ...
- JS -- DOM(文档对象模型)
认识DOM(文档对象模型) DOM(Document Object Model):定义访问和处理HTML文档的标准方法. DOM 将HTML文档呈现为带有元素.属性和文本的树结构(节点树). < ...
- [LeetCode]215. 数组中的第K个最大元素(堆)
题目 在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 1: 输入: [3,2,1,5,6,4] 和 k = 2 输出 ...
- JVM垃圾回收行为的并行与并发
程序的并行和并发 程序的并发(Concurrent) 在操作系统中,是指一个时间段中有几个程序都处于己启动运行到运行完毕之间,且这几个程序都是在同一个处理器_上运行. 并发不是真正意义上的“同时进行” ...
- 关于properties文件的一些问题
在写properties文件时,比如jdbc.properties文件配置连接数据库的账号密码时,不要留有空格,不然会报错 com.mchange.v2.resourcepool.CannotAcqu ...
- C语言专项错题集
2020-08-10 记录 #1 1 struct student{ 2 int num; 3 int age; 4 }; 5 struct student stu[3]={{6001,20},{60 ...
- golang开发:channel使用
channel主要是用于多个goroutine之间通信 channel语法 channel是引用类型,需要实用make来创建channel,如下 make(chan Type, [buffer]) c ...
- 单元测试框架怎么搭?快来看看新版Junit5的这些神奇之处吧!
为什么使用JUnit5 JUnit4被广泛使用,但是许多场景下使用起来语法较为繁琐,JUnit5中支持lambda表达式,语法简单且代码不冗余. JUnit5易扩展,包容性强,可以接入其他的测试引擎. ...
- Centos-远程拷贝-scp
scp 依赖ssh协议,实现从哟个linux系统拷贝到另一个linux系统 格式 scp -P port localPath user@IP:targetPath # 如果拷贝的是文件则需要传递 -r ...