自动化运维(ansible---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 键,都使用空格

 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: 用于指定远程主机上的执行任务的用户.
- 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
variables: 变量
        定义变量可以被多次方便调用
vim /etc/ansible/playbook/example2.yaml
---
 - hosts: group1
   remote_user: root
   vars:
   - user: test1
   tasks:
   - name: create user
     user: name={{user}} state=present
ansible-playbook /etc/ansible/playbook/example2.yaml

案例: playbook编排vsftpd 

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 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

  • 10
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值