Ansible PlayBook的原理和实现

1、Ansible playbooksweb

    Playbooks 是 Ansible 管理配置、部署应用和编排的语言,可使用 Playbooks 来描述你想在远程主机执行的策略或者执行的一组步骤过程等。若是说 Ansible 模块是工做中的工具的话,那么 playbooks 就是方案playbook是由一个或多个“play”组成的列表。play的主要功能在于将事先归并为一组的主机装扮成事先经过ansible中的task定义好的角色。从根本上来说,所谓task无非是调用ansible的一个module。将多个play组织在一个playbook中,便可以让它们联同起来按事先编排的机制同唱一台大戏。shell

①、Playbooks组成apache

     Task section安全

    定义将要在远程主机上执行的任务列表bash

    Variable section数据结构

    定义 playbook 运行时须要使用的变量ide

    Templates section
memcached

    定义playbooks使用的模块,根据模版建立配置文件工具

    Handler sectionui

    定义task执行完成之后须要调用的任务

    Roles section

    定义playbooks的角色


2、YMAL的介绍

    因为Playbooks 采用 YMAL 语法结构。因此这里先介绍一下YMAL。YAML是一个可读性高的用来表达资料序列的格式。YAML参考了其余多种语言,包括:XML、C语言、Python、Perl以及电子邮件格式RFC2822等。Clark Evans在2001年在首次发表了这种语言,另外Ingy dt Net与Oren Ben-Kiki也是这语言的共同设计者。

    YAML Ain't Markup Language,即YAML不是XML。不过,在开发的这种语言时,YAML的意思实际上是:"Yet Another Markup Language"(还是一种标记语言)。其特性:

YAML的可读性好

YAML和脚本语言的交互性好

YAML使用实现语言的数据类型

YAML有一个一致的信息模型

YAML易于实现

YAML能够基于流来处理

YAML表达能力强,扩展性好


3、YMAL的介绍

    YAML的语法和其余高阶语言相似,而且能够简单表达清单、散列表、标量等数据结构。其结构(Structure)经过空格来展现,序列(Sequence)里的项用"-"来表明,Map里的键值对用":"分隔。下面是一个示例。

name: John Smith

age: 41

gender: Male

spouse:

    name: Jane Smith

    age: 37

    gender: Female

children:

    - name: Jimmy Smith

        age: 17

        gender: Male

    - name: Jenny Smith

        age 13

        gender: Female

YAML文件扩展名一般为.yaml,如example.yaml。


①、list

列表的全部元素均使用“-”打头,例如:

# A list of tasty fruits

- Apple

- Orange

- Strawberry

- Mango


②、dictionary

字典经过key与valuef进行标识,例如:

# An employee record

name: Example Developer

job: Developer

skill: Elite


也能够将key:value放置于{}中进行表示,例如:

# An employee record

{name: Example Developer, job: Developer, skill: Elite}


4、playbook基础组件

①、Hosts和Users

    playbook中的每个play的目的都是为了让某个或某些主机以某个指定的用户身份执行任务。hosts用于指定要执行指定任务的主机,其能够是一个或多个由冒号分隔主机组;remote_user则用于指定远程主机上的执行任务的用户。

    - hosts: web

     remote_user: root


不过,remote_user也可用于各task中。也能够经过指定其经过sudo的方式在远程主机上执行任务,其可用于play全局或某任务;此外,甚至能够在sudo时使用sudo_user指定sudo时切换的用户。

    - hosts: web

     remote_user: wlw

     tasks:

       - name: test connection

        ping:

        remote_user: mageedu

       sudo: yes


②、任务列表和action

    play的主体部分是task list。task list中的各任务按次序逐个在hosts中指定的全部主机上执行,即在全部主机上完成第一个任务后再开始第二个。在运行自下而下某playbook时,若是中途发生错误,全部已执行任务都将回滚,所以,在更正playbook后从新执行一次便可。

    task的目的是使用指定的参数执行模块,而在模块参数中可使用变量。模块执行是幂等的,这意味着屡次执行是安全的,由于其结果均一致。

    每一个task都应该有其name,用于playbook的执行结果输出,建议其内容尽量清晰地描述任务执行步骤。若是未提供name,则action的结果将用于输出。

    定义task的可使用“action: module options”或“module: options”的格式,推荐使用后者以实现向后兼容。若是action一行的内容过多,也中使用在行首使用几个空白字符进行换行。

    tasks:

        - name: make sure apache is running

         service: name=httpd state=running


    若是命令或脚本的退出码不为零,可使用以下方式替代:

    tasks:

        - name: run this command and ignore the result

         shell: /usr/bin/somecommand || /bin/true


    或者使用ignore_errors来忽略错误信息:

    tasks:

        - name: run this command and ignore the result

        shell: /usr/bin/somecommand

        ignore_errors: True


③、handlers

    用于当关注的资源发生变化时采起必定的操做。

    “notify”这个action可用于在每一个play的最后被触发,这样能够避免屡次有改变发生时每次都执行指定的操做,取而代之,仅在全部的变化发生完成后一次性地执行指定操做。在notify中列出的操做称为handler,也即notify中调用handler中定义的操做。

    tasks:

    - name: template configuration file

     template: src=template.j2 dest=/etc/foo.conf

     notify:

     - restart memcached

     - restart apache

    handlers:

    - name: restart memcached

     service:  name=memcached state=restarted

    - name: restart apache

     service: name=apache state=restarted

案例:

    - 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
相关文章
相关标签/搜索