8.20笔记

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

下⾯看⼀个官⽅的示例感受⼀下

 
  1. ---

  2. # ⼀位职⼯记录

  3. name: Example Developer

  4. job: Developer

  5. skill: Elite

  6. employed: True

  7. foods:

  8. - Apple

  9. - Orange

  10. - Strawberry

  11. - Mango

  12. languages:

  13. ruby: Elite

  14. python: Elite

  15. dotnet: Lame

playbook实例

先直接来看⼀个实例

第1步: 创建⼀个存放playbook的⽬录(路径⾃定义)

master# mkdir /etc/ansible/playbook

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

 
  1. master# yum install httpd -y

  2. 按需要修改你想要的配置(为了测试可以随意改动标记⼀下)

  3. master# vim /etc/httpd/conf/httpd.conf

第3步: 写⼀个playbook⽂件(后缀为.yml或.yaml)

 
  1. # vim /etc/ansible/playbook/example.yaml

  2. ---

  3. - hosts: group1

  4. remote_user: root

  5. tasks:

  6. - name: ensure apache is at the latest version

  7. yum: name=httpd,httpd-devel state=latest

  8. - name: write the apache config file

  9. copy: src=/etc/httpd/conf/httpd.conf

  10. dest=/etc/httpd/conf/httpd.conf

  11. notify:

  12. - restart apache

  13. - name: ensure apache is running (and enable it

  14. at boot)

  15. service: name=httpd state=started enabled=yes

  16. handlers:

  17. - name: restart apache

  18. service: name=httpd state=restarted

第4步: 执⾏写好的palybook

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

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

 
  1. # ansible-playbook

  2. /etc/ansible/playbook/example.yaml

Playbook常⻅语法

hosts: ⽤于指定要执⾏任务的主机,其可以是⼀个或多个由冒号分隔主机组

remote_user: ⽤于指定远程主机上的执⾏任务的⽤户

 
  1. - hosts: group1

  2. remote_user: root

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

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

 
  1. tasks:

  2. - name: ensure apache is at the latest version

  3. yum: name=httpd,httpd-devel state=latest

  4. - name: write the apache config file

  5. copy: src=/etc/httpd/conf/httpd.conf

  6. dest=/etc/httpd/conf/httpd.conf

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

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

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

 
  1. notify:

  2. - restart apache

  3. - name: ensure apache is running (and enable it

  4. at boot)

  5. service: name=httpd state=started enabled=yes

  6. handlers:

  7. - name: restart apache

  8. service: name=httpd state=restarted

案例: playbook编排vsftpd

写⼀个playbook实现

1. 配置yum

2. 安装vsftpd包

3. 修改配置⽂件(要求拒绝匿名⽤户登录)

4. 启动服务并实现vsftpd服务开机⾃动启动

 
  1. ---

  2. - hosts: group1

  3. remote_user: root

  4. tasks:

  5. - name: rm yum repository

  6. file: path=/etc/yum.repos.d/ state=absent

  7. - name: 同步master上的yum源到group1

  8. copy: src=/etc/yum.repos.d dest=/etc/

  9. - name: ensure vsftpd is at the latest version

  10. yum: name=vsftpd state=latest

  11. - name: write the apache config file

  12. copy: src=/etc/vsftpd/vsftpd.conf

  13. dest=/etc/vsftpd/vsftpd.conf

  14. notify:

  15. - restart vsftpd

  16. - name: ensure vsftpd is running (and enable it

  17. at boot)

  18. service: name=vsftpd state=started enabled=yes

  19. handlers:

  20. - name: restart vsftpd

  21. service: name=vsftpd state=restarted

playbook编排多个hosts任务

 
  1. --- # ---代表开始(可选项,不写也可以)

  2. - hosts: 10.1.1.12

  3. remote_user: root

  4. tasks:

  5. - name: 创建/test1/⽬录

  6. file: path=/test1/ state=directory

  7. # 这⾥不能⽤---分隔,会报语法错误(后⾯课程玩k8s编排也写YAML

  8. ⽂件,是可以⽤---来分隔段落的)

  9. - hosts: 10.1.1.13

  10. remote_user: root

  11. tasks:

  12. - name: 创建/test2/⽬录

  13. file: path=/test2/ state=directory

  14. ... # ...代表结束(可选项,不写也可以)

案例: 编排nfs搭建与客户端挂载

1, 在master上准备nfs配置⽂件

 
  1. # vim /etc/exports

  2. /share *(ro)

2, 编写yaml编排⽂件

 
  1. # vim /etc/ansible/playbook/nfs.yml

  2. ---

  3. - hosts: 10.1.1.12

  4. remote_user: root

  5. tasks:

  6. - name: 安装nfs服务相关软件包

  7. yum: name=nfs-utils,rpcbind,setup state=latest

  8. - name: 创建共享⽬录

  9. file: path=/share/ state=directory

  10. - name: 同步nfs配置⽂件

  11. copy: src=/etc/exports dest=/etc/exports

  12. notify: restart nfs

  13. - name: 启动rpcbind服务,并设置为开机⾃启动

  14. service: name=rpcbind state=started enabled=on

  15. - name: 启动nfs服务,并设置为开机⾃启动

  16. service: name=nfs state=started enabled=on

  17. handlers:

  18. - name: restart nfs

  19. service: name=nfs state=restarted

  20. - hosts: 10.1.1.13

  21. remote_user: root

  22. tasks:

  23. - name: 安装nfs客户端软件包

  24. yum: name=nfs-utils state=latest

  25. - name: 挂载nfs服务器的共享

  26. shell: mount 10.1.1.12:/share /mnt

3, 执⾏playbook

# ansible-playbook /etc/ansible/playbook/nfs.yaml
  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值