ip段访问网站代码,wordpress不显示引用图片,辽宁省建设工程信息网必须用主锁,网站建设与管理专业就业前景目录 一.playbook介绍二.playbook格式1.书写格式2.notify介绍 一.playbook介绍
playbook 是 ansible 用于配置#xff0c;部署#xff0c;和管理被控节点的剧本。通过 playbook 的详细描述#xff0c;执行其中的一系列 tasks #xff0c;可以让远端主机达到预期的状态。pl… 目录 一.playbook介绍二.playbook格式1.书写格式2.notify介绍 一.playbook介绍
playbook 是 ansible 用于配置部署和管理被控节点的剧本。通过 playbook 的详细描述执行其中的一系列 tasks 可以让远端主机达到预期的状态。playbook 就像 Ansible 控制器给被控节点列出的的一系列 to-do-list 而被控节点必须要完成。也可以这么理解playbook 字面意思即剧本现实中由演员按照剧本表演在 Ansible 中这次由计算机进行表演由计算机安装部署应用提供对外服务以及组织计算机处理各种各样的事情
二.playbook格式
1.书写格式 文件的第一行应该以 “—” (三个连字符)开始表明 YMAL 文件的开始。在同一行中# 之后的内容表示注释类似于 shellpython 和 ruby。YMAL 中的列表元素以 ”-” 开头然后紧跟着一个空格后面为元素内容。同一个列表中的元素应该保持相同的缩进。否则会被当做错误处理。play 中 hostsvariablesrolestasks 等对象的表示方法都是键值中间以 “:” 分隔表示“:” 后面还要增加一个空格 举例 安装挂载nfs服务
[roottdm1 playbook]# cat nfs.yml
---
- hosts: webremote_user: roottasks: - name: install nfsyum: namerpcbind,nfs-utils statepresent- name: nfs configure filecopy: src./export.j2 dest/etc/exports backupyes- name: mkdir share dirfile: path/data statedirectory ownernfsnobody groupnfsnobody- name: start rpcbindservice: namerpcbind statestarted enabledyes - name: start nfsservice: namenfs statestarted enabledyes - name: mount localmount: src47.93.98.117:/data path/mnt fstypenfs statemounted文件名称应该以.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在监控到该任务 changed1时会触发该notify指令所定义的handler然后去执行handler。所谓handler其实就是task无论在写法上还是作用上它和task都没有区别唯一的区别在于hander是被触发而被动执行的不像普通task一样会按流程正常执行
测试1 当检测到nfs的配置发生变化是会重启nfs服务和重新挂载 notify和handlers中定义的名称必须一致
[roottdm1 playbook]# cat nfs.yml
---
- hosts: webtasks: - name: install nfsyum: namerpcbind,nfs-utils statepresent- name: nfs configure filecopy: src./export.j2 dest/etc/exports backupyesnotify: restart nfs #当export.j2文件发生变化就会由handlers来执行。- name: mkdir share dirfile: path/data statedirectory ownernfsnobody groupnfsnobody- name: start rpcbindservice: namerpcbind statestarted enabledyes - name: start nfsservice: namenfs statestarted enabledyes - name: mount localmount: src47.93.98.117:/data path/mnt fstypenfs statemountednotify: client remounthandlers: - name: restart nfs #名称和notify中的一致service: namenfs staterestarted - name: client remountservice: src47.93.98.117:/data path/mnt fstypenfs stateremounted#修改export.j2的内容
vim export.j2
/data 47.93.98.0/24(rw,all_squash)执行剧本观察
#查看文件是否更改
[roottdm1 playbook]# ansible web -m shell -a cat /etc/exports
47.93.98.117 | CHANGED | rc0
/data 47.93.98.0/24(rw,all_squash)测试2 修改nginx的文件检测到文件被修改handlers下面任务会被执行
[roottdm1 playbook]# cat nginx.yml
---
- hosts: webtasks: - name: install nginx yum:name: nginxstate: installed- name: index filecopy:content: This is ansible testdest: /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