0
点赞
收藏
分享

微信扫一扫

集师知识付费小程序搭建。。。

紫荆峰 2024-08-20 阅读 5

playbook(剧本): 是ansible用于配置,部署,和管理被控节点的剧本。用于ansible操作的编排。

参考:https://docs.ansible.com/ansible/latest/user_guide/playbooks _intro.html

使用的格式为yaml格式(saltstack,elk,docker,docker-compose,kubernetes等也都会用到yaml格式)

YMAL格式

 playbook实例

第1步: 创建一个存放playbook的目录(路径自定义)

mkdir /etc/ansible/playbook

第2步: 准备httpd配置文件,并修改成想要的配置

yum install httpd -y
#按需要修改你想要的配置(为了测试可以随意改动标记⼀下)
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 itat boot)
  service: name=httpd state=started enabled=yes
handlers:
  - name: restart apache
    service: name=httpd state=restarted

第4步: 执行写好的palybook

ansible-playbook /etc/ansible/playbook/example.yaml

会显示出执行的过程,并且执行的每一步都有ok,changed,failed 等标识

执行如果有错误(failed)会回滚,解决问题后,直接再执行这条命 令即可,并会把failed改为changed(幂等性)

Playbook常见语法

hosts: 用于指定要执行任务的主机,其可以是一个或多个由冒号分 隔主机组.

remote_user: 用于指定远程主机上的执行任务的用户.

tasks: 任务列表, 按顺序执行任务.

如果⼀个host执行task失败, 整个tasks都会回滚, 修正playbook 中的错误, 然后重新执行即可.

handlers: 类似task,但需要使用notify通知调⽤。

不管有多少个通知者进行了notify,等到play中的所有task执行完成之后,handlers也只会被执行一次.

handlers最佳的应用场景是用来重启服务,或者触发系统重启操作.除此以外很少用到了.

variables: 变量

        定义变量可以被多次方便调用

案例: playbook编排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 itat 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
# 这⾥不能⽤---分隔,会报语法错误
- 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配置⽂件
    shell: 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

举报

相关推荐

0 条评论