当前位置: 首页 > news >正文

Ansible-palybook学习

目录

  • 一.playbook介绍
  • 二.playbook格式
    • 1.书写格式
    • 2.notify介绍

一.playbook介绍

playbook 是 ansible 用于配置,部署,和管理被控节点的剧本。通过 playbook 的详细描述,执行其中的一系列 tasks ,可以让远端主机达到预期的状态。playbook 就像 Ansible 控制器给被控节点列出的的一系列 to-do-list ,而被控节点必须要完成。也可以这么理解,playbook 字面意思,即剧本,现实中由演员按照剧本表演,在 Ansible 中,这次由计算机进行表演,由计算机安装,部署应用,提供对外服务,以及组织计算机处理各种各样的事情

二.playbook格式

1.书写格式

  1. 文件的第一行应该以 “—” (三个连字符)开始,表明 YMAL 文件的开始。
  2. 在同一行中,# 之后的内容表示注释,类似于 shell,python 和 ruby。
  3. YMAL 中的列表元素以 ”-” 开头然后紧跟着一个空格,后面为元素内容。
  4. 同一个列表中的元素应该保持相同的缩进。否则会被当做错误处理。
  5. play 中 hosts,variables,roles,tasks 等对象的表示方法都是键值中间以 “:”
    分隔表示,“:” 后面还要增加一个空格

举例 安装挂载nfs服务

[root@tdm1 playbook]# cat nfs.yml 
---
- hosts: webremote_user: roottasks: - name: install nfsyum:  name=rpcbind,nfs-utils state=present- name: nfs configure filecopy: src=./export.j2 dest=/etc/exports backup=yes- name: mkdir share dirfile: path=/data state=directory owner=nfsnobody group=nfsnobody- name: start rpcbindservice: name=rpcbind state=started enabled=yes  - name: start nfsservice: name=nfs  state=started enabled=yes  - name: mount localmount: src=47.93.98.117:/data path=/mnt fstype=nfs state=mounted

文件名称应该以.yml结尾

hosts :使用 hosts 指示使用哪个主机或主机组来运行下面的 tasks ,每个 playbook 都必须指定 hosts ,hosts 也可以使用通配符格式。主机或主机组在 inventory 清单中指定,可以使用系统默认的 /etc/ansible/hosts,也可以自己编辑,在运行的时候加上 -i 选项,指定清单的位置即可。在运行清单文件的时候,–list-hosts 选项会显示那些主机将会参与执行 task 的过程中。

remote_user:指定远端主机中的哪个用户来登录远端系统,在远端系统执行 task 的用户,可以任意指定,也可以使用 sudo,但是用户必须要有执行相应 task 的权限。

tasks:指定远端主机将要执行的一系列动作。tasks 的核心为 ansible 的模块,前面已经提到模块的用法。tasks 包含 name 和要执行的模块,name 是可选的,只是为了便于用户阅读,不过还是建议加上去,模块是必须的,同时也要给予模块相应的参数。

使用ansible-playbook运行playbook文件,得到以下输出信息,输出内容为json格式,由不同颜色组成
绿色代表执行成功,系统保持原样
黄色代表系统状态发生改变
红色代表失败,显示错误输出

执行有三个步骤:

1.收集facts
2. 执行tasks
3. 报告结果

在这里插入图片描述

2.notify介绍

Ansible提供了notify指令和handlers功能。如果在某个task中定义了notify指令,当Ansible在监控到该任务 changed=1时,会触发该notify指令所定义的handler,然后去执行handler。所谓handler,其实就是task,无论在写法上还是作用上它和task都没有区别,唯一的区别在于hander是被触发而被动执行的,不像普通task一样会按流程正常执行

测试1
当检测到nfs的配置发生变化是,会重启nfs服务和重新挂载
notify和handlers中定义的名称必须一致

[root@tdm1 playbook]# cat nfs.yml 
---
- hosts: webtasks: - name: install nfsyum:  name=rpcbind,nfs-utils state=present- name: nfs configure filecopy: src=./export.j2 dest=/etc/exports backup=yesnotify: restart nfs   #当export.j2文件发生变化,就会由handlers来执行。- name: mkdir share dirfile: path=/data state=directory owner=nfsnobody group=nfsnobody- name: start rpcbindservice: name=rpcbind state=started enabled=yes  - name: start nfsservice: name=nfs  state=started enabled=yes  - name: mount localmount: src=47.93.98.117:/data path=/mnt fstype=nfs state=mountednotify: client remounthandlers: - name: restart nfs     #名称和notify中的一致service: name=nfs  state=restarted - name: client remountservice: src=47.93.98.117:/data path=/mnt fstype=nfs state=remounted
#修改export.j2的内容
vim export.j2
/data 47.93.98.0/24(rw,all_squash)

执行剧本,观察
在这里插入图片描述

#查看文件是否更改
[root@tdm1 playbook]# ansible web -m shell -a 'cat /etc/exports'
47.93.98.117 | CHANGED | rc=0 >>
/data 47.93.98.0/24(rw,all_squash)

测试2
修改nginx的文件,检测到文件被修改,handlers下面任务会被执行

[root@tdm1 playbook]# cat nginx.yml 
---
- hosts: webtasks: - name: install  nginx yum:name: nginxstate: installed- name: index filecopy:content: "This is ansible test"dest: /usr/share/nginx/html/index.html- name: nginx configure filecopy:src: ./nginx.conf.j2dest: /etc/nginx/conf.d/default.confbackup: yesnotify: restart nginx    #文件被修改,重启nginx- name: start nginxservice: name: nginxstate: startedhandlers:- name: restart nginx     #重启nginxservice: name: nginxstate: restarted
#修改nginx.conf.j2的文件
vim nginx.conf.j2
server {listen       81;server_name  localhost;location / {root   /usr/share/nginx/html;index  index.html index.htm;}
}

在这里插入图片描述

参考:https://blog.csdn.net/u012562943/category_6298590.html

http://www.lryc.cn/news/156657.html

相关文章:

  • 服务注册与服务发现
  • RabbitMQ从入门到精通之安装、通讯方式详解
  • 植物大战僵尸植物表(二)
  • UML基础
  • C# void 关键字学习
  • OA与CRM与ORACLE
  • 【C++杂货铺】探索list的底层实现
  • NX/UG二次开发—Parasolid—PK_BODY_pick_topols
  • 【校招VIP】前端算法考点之大数据相关
  • Goland2023版新UI的debug模式调试框按钮功能说明
  • 【AIGC专题】Stable Diffusion 从入门到企业级应用0414
  • 汇编原理学习记录:物理地址=段地址*16+偏移地址
  • mysql-2:安装mysql
  • gin框架
  • Laravel 完整开源项目大全
  • Spring MVC @Controller和@RequestMapping注解
  • 软件架构之前后端分离架构服务器端高并发演进之路
  • 第4节-PhotoShop基础课程-Ps格式
  • C语言malloc函数学习
  • 从零开始学习deepsort目标追踪算法----原理和代码详解
  • 第三章 LInux多线程开发 3.1-3.5线程创建 终止 分离
  • 空间曲线的参数方程
  • 非华为机型如何体验HarmonyOS鸿蒙系统 刷写HarmonyOS鸿蒙GSI系统以及一些初步的bug修复
  • Flutter 生成小程序的混合 App 实践
  • 利用 Python-user-agents 解析 User_Agent
  • Java版企业电子招标采购系统源码Spring Cloud + Spring Boot +二次开发+ MybatisPlus + Redis
  • Mybatis如何给字段起别名?
  • php对接AWS S3云存储,上传S3及访问权限问题
  • java 实现单例模式
  • minio文件服务器开启https