您的位置:首页 > 健康 > 美食 > playbook!!!

playbook!!!

2024/10/6 16:25:52 来源:https://blog.csdn.net/2301_76858161/article/details/141365562  浏览:    关键词:playbook!!!
playbook(剧本): 是ansible⽤于配置,部署,和管理被控节点的剧本。⽤于ansible操作的编排。
参考:https://docs.ansible.com/ansible/latest/user_guide/playbooks _intro.html
使⽤的格式为yaml格式(saltstack,elk,docker,docker——compose,kubernetes等也都会⽤到yaml格式)
YMAL格式
以.yaml或.yml结尾
⽂件的第⼀⾏以 "---"开始,表明YMAL⽂件的开始(可选的)
以#号开头为注释
列表中的所有成员都开始于相同的缩进级别, 并且使⽤⼀个 "- " 作为开头(⼀个横杠和⼀个空格)
⼀个字典是由⼀个简单的 键: 值 的形式组成(这个冒号后⾯必须 是⼀个空格)
注意: 写这种⽂件不要使⽤tab键,都使⽤空格
参考:https://docs.ansible.com/ansible/latest/reference_appendices /YAMLSyntax.html#yaml-syntax
下⾯看⼀个官⽅的示例感受⼀下
---
# ⼀位职⼯记录
name: Example Developer
job: Developer
skill: Elite
employed: True
foods:
- Apple
- Orange
- Strawberry
- Mango
languages:
ruby: Elite
python: Elite
dotnet: Lame
playbook实例
先直接来看⼀个实例
第1步: 创建⼀个存放playbook的⽬录(路径⾃定义)
master# mkdir /etc/ansible/playbook
第2步: 准备httpd配置⽂件,并修改成你想要的配置
master# yum install httpd -y
按需要修改你想要的配置(为了测试可以随意改动标记⼀下)
master# vim /etc/httpd/conf/httpd.conf
第3步: 写⼀个playbook⽂件(后缀为.yml或.yaml)
# vim /etc/ansible/playbook/example.yaml
---
- hosts: group1
remote_user: root
tasks:
- name: ensure apache is at the latest version 
yum: name=httpd,httpd-devel state=latest
- name: write the apache config file 
copy: src=/etc/httpd/conf/httpd.conf
dest=/etc/httpd/conf/httpd.conf
notify:
- restart apache
- name: ensure apache is running (and enable it
at boot)
service: name=httpd state=started enabled=yes
handlers:
- name: restart apache
service: name=httpd state=restarted
第4步: 执⾏写好的palybook
会显示出执⾏的过程,并且执⾏的每⼀步都有ok,changed,failed等标识
执⾏如果有错误(failed)会回滚,解决问题后,直接再执⾏这条命令即可,并会把failed改为changed(幂等性)
# ansible-playbook
/etc/ansible/playbook/example.yaml
Playbook常⻅语法
hosts: ⽤于指定要执⾏任务的主机,其可以是⼀个或多个由冒号分隔主机组
remote_user: ⽤于指定远程主机上的执⾏任务的⽤户
- hosts: group1 
remote_user: root
tasks: 任务列表, 按顺序执⾏任务.
如果⼀个host执⾏task失败, 整个tasks都会回滚, 修正playbook中的错误, 然后重新执⾏即可.
tasks:
- name: ensure apache is at the latest version 
yum: name=httpd,httpd-devel state=latest
- name: write the apache config file 
copy: src=/etc/httpd/conf/httpd.conf
dest=/etc/httpd/conf/httpd.conf
handlers: 类似task,但需要使⽤notify通知调⽤。
不管有多少个通知者进⾏了notify,等到play中的所有task执⾏完成之后,handlers也只会被执⾏⼀次.
handlers最佳的应⽤场景是⽤来重启服务,或者触发系统重启操 作.除此以外很少⽤到了
notify: 
- restart apache
- name: ensure apache is running (and enable it
at boot)
service: name=httpd state=started enabled=yes
handlers:
- name: restart apache
service: name=httpd state=restarted
案例: playbook编排vsftpd
写⼀个playbook实现
1. 配置yum
2. 安装vsftpd包
3. 修改配置⽂件(要求拒绝匿名⽤户登录)
4. 启动服务并实现vsftpd服务开机⾃动启动
---
- hosts: group1
remote_user: root
tasks:
- name: rm yum repository
file: path=/etc/yum.repos.d/ state=absent
- name: 同步master上的yum源到group1
copy: src=/etc/yum.repos.d dest=/etc/
- name: ensure vsftpd is at the latest version
yum: name=vsftpd state=latest
- name: write the apache config file
copy: src=/etc/vsftpd/vsftpd.conf
dest=/etc/vsftpd/vsftpd.conf
notify:
- restart vsftpd
- name: ensure vsftpd is running (and enable it
at boot)
service: name=vsftpd state=started enabled=yes
handlers:
- name: restart vsftpd
service: name=vsftpd state=restarted
playbook编排多个hosts任务
--- # ---代表开始(可选项,不写也可以)
- hosts: 10.1.1.12
remote_user: root
tasks:
- name: 创建/test1/⽬录
file: path=/test1/ state=directory
# 这⾥不能⽤---分隔,会报语法错误(后⾯课程玩k8s编排也写YAML
⽂件,是可以⽤---来分隔段落的)
- hosts: 10.1.1.13
remote_user: root
tasks:
- name: 创建/test2/⽬录
file: path=/test2/ state=directory
... # ...代表结束(可选项,不写也可以)
案例: 编排nfs搭建与客户端挂载
1, 在master上准备nfs配置⽂件
# vim /etc/exports
/share *(ro)
2, 编写yaml编排⽂件
# vim /etc/ansible/playbook/nfs.yml
---
- hosts: 10.1.1.12
remote_user: root
tasks:
- name: 安装nfs服务相关软件包
yum: name=nfs-utils,rpcbind,setup state=latest
- name: 创建共享⽬录
file: path=/share/ state=directory
- name: 同步nfs配置⽂件
copy: src=/etc/exports dest=/etc/exports
notify: restart nfs
- name: 启动rpcbind服务,并设置为开机⾃启动
service: name=rpcbind state=started enabled=on
- name: 启动nfs服务,并设置为开机⾃启动
service: name=nfs state=started enabled=on
handlers:
- name: restart nfs
service: name=nfs state=restarted
- hosts: 10.1.1.13
remote_user: root
tasks:
- name: 安装nfs客户端软件包
yum: name=nfs-utils state=latest
- name: 挂载nfs服务器的共享
shell: mount 10.1.1.12:/share /mnt
3, 执⾏playbook
# ansible-playbook /etc/ansible/playbook/nfs.yaml

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com