Ansible10:Playbook的角色與包含【轉】

來源:互聯網
上載者:User

標籤:upd   模板   https   工具   href   default   命名   hold   info   

當單個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

Ansible10:Playbook的角色與包含【轉】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.