Playbooks的介绍

playbooks简介

Playbooks 与 adhoc 相比,是一种完全不同的运用 ansible 的方式,是非常之强大的。

简单来说,playbooks 是一种简单的配置管理系统与多机器部署系统的基础。与现有的其他系统有不同之处,且非常适合于复杂应用的部署。

Playbooks 可用于声明配置,更强大的地方在于,在 playbooks 中可以编排有序的执行过程,甚至于做到在多组机器间,来回有序的执行特别指定的步骤。并且可以同步或异步的发起任务。

我们使用 adhoc 时,主要是使用 /usr/bin/ansible 程序执行任务。而使用 playbooks 时,更多是将之放入源码控制之中,用之推送你的配置或是用于确认你的远程系统的配置是否符合配置规范。

在连接——ansible-examples repository 有一些整套的playbooks,它们阐明了上述的这些技巧。

playbooks的语言示例

Playbooks 的格式是YAML,语法做到最小化,意在避免 playbooks 成为一种编程语言或是脚本,但它也并不是一个配置模型或过程的模型。

playbook 由一个或多个 ‘plays’ 组成,它的内容是一个以 ‘plays’ 为元素的列表。

在 play 之中,一组机器被映射为定义好的角色。在 ansible 中,play 的内容被称为 tasks,即任务。在基本层次的应用中。一个任务是一个对 ansible 模块的调用。.

‘plays’ 好似音符,playbook 好似由 ‘plays’ 构成的曲谱,通过 playbook,可以编排步骤进行多机器的部署,比如在 webservers 组的所有机器上运行一定的步骤,然后在 database server 组运行一些步骤,最后回到 webservers 组,再运行一些步骤,诸如此类。

“plays” 算是一个体育方面的类比,你可以通过多个 plays 告诉你的系统做不同的事情,不仅是定义一种特定的状态或模型。你可以在不同时间运行不同的 plays。

这里有一个 playbook,其中仅包含一个 play:

---
- hosts: webservers
  vars:
    http_port: 80
    max_clients: 200
  remote_user: root
  tasks:
  - name: ensure apache is at the latest version
    yum: pkg=httpd state=latest
  - name: write the apache config file
    template: src=/srv/httpd.j2 dest=/etc/httpd.conf
    notify:
    - restart apache
  - name: ensure apache is running
    service: name=httpd state=started
  handlers:
    - name: restart apache
      service: name=httpd state=restarted

playbooks基础

主机和用户

你可以为 playbook 中的每一个 play,个别地选择操作的目标机器是哪些,以哪个用户身份去完成要执行的步骤(called tasks)。

hosts行的内容是一个或多个组或主机的 patterns,以逗号为分隔符。

remote_user表示账户名。

---
- hosts: webservers
  remote_user: root

参数 remote_user 以前写做 user,在 Ansible 1.4 以后才改为 remote_user。主要为了不跟 user 模块混淆(user 模块用于在远程系统上创建用户)。

再者,在每一个 task 中,可以定义自己的远程用户:

---
- hosts: webservers
  remote_user: root
  tasks:
    - name: test connection
      ping:
      remote_user: yourname

task 中的 remote_user 参数在 1.4 版本以后才添加的。

支持sudo

---
- hosts: webservers
  remote_user: yourname
  sudo: yes

同样的,你可以仅在一个 task 中,使用 sudo 执行命令,而不是在整个 play 中使用 sudo:

---
- hosts: webservers
  remote_user: yourname
  tasks:
    - service: name=nginx state=started
      sudo: yes

你也可以登陆后,sudo 到不同的用户身份,而不是使用 root:

---
- hosts: webservers
  remote_user: yourname
  sudo: yes
  sudo_user: postgres

如果你需要在使用 sudo 时指定密码,可在运行 ansible-playbook 命令时加上选项 --ask-sudo-pass (-K)。如果使用 sudo 时,playbook 疑似被挂起,可能是在 sudo prompt 处被卡住,这时可执行 Control-C 杀死卡住的任务,再重新运行一次。

tasks列表

每一个play都包含了一个task列表,这个task会在它对应的所有主机上面(主机写到hosts中,然后通过install.yml调用)。在一个play里面,所有的主机都会执行同一个task列表。

在task列表里面,每一个task都是为了执行一个module,并且都会带上特定的参数。在参数中可以使用变量[variables]。

并且每一个task都会有一个name,这个name一般用于描述这个任务是干什么的,这样子在执行playbooks的时候,就能很清楚地从输出中得到这个task是做什么的。

那么如何生命一个task呢?在老版本中,以前有“action:module options”,但是现在,一般都是使用下面的格式:

tasks:
  - name: make sure apache is running
    service: name=httpd state=running

当然了,也有两个比较特殊的模块(module),他们并不是使用上面的key-value的形式,而是使用如下的格式:

command模块:

tasks:
  - name: disable selinux
    command: /sbin/setenforce 0

shell模块:

tasks:
  - name: run this command and ignore the result
    shell: /usr/bin/somecommand || /bin/true

或者

tasks:
  - name: run this command and ignore the result
    shell: /usr/bin/somecommand
    ignore_errors: True

如果action太长的情况下,你也可以通过空格或者缩进隔开连续的一行:

tasks:
  - name: Copy ansible inventory file to client
    copy: src=/etc/ansible/hosts dest=/etc/ansible/hosts
            owner=root group=root mode=0644

并且可以在action行中进行变量的使用,假如你在变量文件中定义了一个vhost,可以像下面那样子使用:

tasks:
  - name: create a virtual host file for {{ vhost }}
    template: src=somefile.j2 dest=/etc/httpd/conf.d/{{ vhost }}

handlers

handlers:在发生改变时执行的操作

如果远程服务器被人改动了,ansible可以识别这种改动,并且对其发生相应。当发生改动的时候,handlers中的notify action会在playbook的每一个task结束的时候被触发,并且只会被触发一次。

下面我们列出使用方式:

- name: template configuration file
  template: src=template.j2 dest=/etc/foo.conf
  notify:
     - restart memcached
     - restart apache

notify下面列出的就是handlers,会通过“restart memcached”这样的handlers定位到相应的task,然后执行任务,被定位的handlers中的task如下所示:

handlers:
    - name: restart memcached
      service:  name=memcached state=restarted
    - name: restart apache
      service: name=apache state=restarted

备注:handlers会按照声明的顺序执行。

执行一个playbook

ansible-playbook -i hosts xxx.yml

坚壁清野

猜你喜欢

转载自www.cnblogs.com/hzhiping/p/10165106.html