Ansible10:Playbook的角色與包含

    當單個playbook文件越來越大的時候,我們就需要重新來組織Playbooks了。我們可以將一個大的playbook拆成若干個小的playbook文件,然後通過include的方式,在主配置文件中將這些零碎的小文件包含進來,這叫做playbook的包含。我們也可以按照一定的規則將執行的某一類型任務放在一個目錄裏,並在這個目錄中再次對這個playbook按照tasks,handlers,files,templates,vars等類型劃分成若干文件,將對應文件存放在對應的目錄中,這種組織方式就叫做playbook的roles。

一、Playbook的包含

playbook的包含其實就是使用include關鍵字

1、tasks包含

示例1:

一個task文件foo.yml示例如下:

# possibly saved as tasks/foo.yml

- name: placeholder foo

  command: /bin/foo

- name: placeholder bar

  command: /bin/bar


在另一個task文件bar.yml中包含foo.yml:

tasks:

  - include: tasks/foo.yml

也可以在include的時候,帶入變量:

tasks:

 - include: wordpress.yml user=timmy

 - include: wordpress.yml user=alice

 - include: wordpress.yml user=bob


通過如下方式帶入變量:

tasks:

 - { include: wordpress.yml, user: timmy, ssh_keys: [ 'keys/one.txt', 'keys/two.txt' ] }

再給一個例子:

tasks:
  - include: wordpress.yml
    vars:
        remote_user: timmy
        some_list_variable:
          - alpha
          - beta
          - gamma

2、handlers包含

handlers包含與tasks的包含大體類似,直接給例子:

handlers1.yml內容如下:

# this might be in a file like handlers/handlers.yml

- name: restart apache

  service: name=apache state=restarted

handlers.yml包含handlers1.yml示例:
handlers:

 - include: handlers/handlers.yml

3、混合包含

include也可以用於將一個playbook導入到另一個playbook中:

- name: this is a play at the top level of a file

 hosts: all

 remote_user: root

 tasks:

 - name: say hi

   tags: foo

   shell: echo "hi..."

- include: load_balancers.yml

- include: webservers.yml

- include: dbservers.yml

二、角色(roles)

1、創建role

創建role的步驟如下:

1)創建以roles命令的目錄

2)在roles目錄中分別創建角色名稱命名的目錄,如websrvs等

3)在每個角色命名的目錄中分別創建files、handlers、meta、tasks、teamplates和vars目錄,用不到的目錄可以創建爲空目錄,也可以不創建。

4)在playbook文件中,調用各角色

roles文件組織結構示例:

group_vas/

site.yml

webservers.yml

roles/

   common/

     files/

     templates/

     tasks/

     handlers/

     vars/

     defaults/

     meta/

   webservers/

     files/

     templates/

     tasks/

     handlers/

     vars/

     defaults/

     meta/


roles各目錄的作用及可用的文件:

    files:存放由copy或script等模塊調用的文件

    tempaltes:Jinja2模板文件

    tasks:至少應該包含一個名爲main.yml的文件,其定義了此角色的任務列表,些文件可以使用include包含其它的位於此目錄中的task文件

    handlers:至少包含一個main.yml文件,用於定義此角色用到的各handler,在handler中使用include包含的其他handler文件也應該位於此目錄

    vars:應當包含一個main.yml文件,用於定義此角色用到的變量

    meta:應當包含一個main.yml文件,用於定義此角色的特殊設定及依賴關係等

    default:爲當前角色設定默認變量時使用些目錄,包含一個main.yml文件


2、引用roles

基本引用的方法:

- hosts: webservers

  roles:

     - common

     - webserver


也可以通過如下方法引用時帶入變量:

- hosts: webservers

  roles:

    - common

    - { role: foo_app_instance, dir: '/opt/a',  port: 5000 }

    - { role: foo_app_instance, dir: '/opt/b',  port: 5001 }


還可以在引用時使用條件語句:

- hosts: webservers

  roles:

    - { role: some_role, when: "ansible_os_family == 'RedHat'" }


下面也是一個帶入變量的示例:

- hosts: webservers

 roles:

   - role: database

     database_name: ` db_name `

     database_user: {{ db_pass }}

   - role: webserver

     live_hostname: web1

     domains:

       - example.com

       - www.example.com

3、pre_tasks和post_tasks

    如果在執行一個role時,需要在其前或其後依然要執行某些任務,我們可以使用pre_tasks及post_tasks來聲明。pre_tasks是在role之前執行,而post_tasks則在role之後執行:

- name: deply webservers

 host: webservers

 vars_files:

   - secrets.yml

 pre_tasks:

   - name: update yum cache

     yum: update_cache=yes

 roles:

   - role: apache

     database_host: ` hostvars`.`db`.`ansible_eth0`.`ipv4`.`address `

     domains:

       - exampel.com

       - www.example.com

  post_tasks:

    - name: print something

      shell: echo "The roles have been updated!"


4、role的依賴

如果當前role在執行前需要依賴另一個role,我們可以在roles的meta目錄中的main.yml中定義role的依賴關係。

示例1:

#roles/webservers/meta/main.yml

dependencies:

 - { role: common, some_parameter: 3 }

 - { role: apache, port: 80 }

 - { role: postgres, dbname: blarg, other_parameter: 12 }


示例2:

dependencies:

 - {role: ntp, ntp_server=ntp.ubuntu.com}

 - {role: web}

 - {role: memcached}


5、Ansible Galaxy

ansible-galaxy是一個工具,我們可以利用它快速的創建一個標準的roles目錄結構,還可以通過它在https:/galaxy.ansible.com上下載別人寫好的roles,直接拿來用。

通過ansible-galaxy初始化一個roles的目錄結構,方法如下:

    ansible-galaxy init /etc/ansible/roles/websrvs

安裝別人寫好的roles:

    ansible-galaxy install -p /etc/ansible/roles bennojoy.mysql

列出已安裝的roles:

    ansible-galaxy list

查看已安裝的roles信息:

    ansible-galaxy info bennojoy.mysql

卸載roles:

    ansible-galaxy remove bennojoy.mysql



本文出自 “無名小卒” 博客,請務必保留此出處http://breezey.blog.51cto.com/2400275/1757832

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章