ansible高級使用知識點


ansible功能重用和高級特性

Handlers

·Handlers只有在其所在的任務被執行時,才會被運行;如果一個任務中定義了notify調用Handlers,但是由於條件判斷等原因,該任務未被執行,那么Handlers同樣不會被執行。

·Handlers只會在Play的末尾運行一次;如果想在一個Playbook的中間運行Handlers,則需要使用meta模塊來實現,例如:-meta:flush_handlers。

·如果一個Play在運行到調用Handlers的語句之前失敗了,那么這個Handlers將不會被執行。我們可以使用mega模塊的--force-handlers選項來強制執行Handlers,即使是Handlers所在的Play中途運行失敗也能執行。

handlers:

  • name: restart apache
    service: name=apache2 state=restarted
    notify: restart memcached
  • name: restart memcached
    service: name=memcached state=restarted

Facts

在運行任何一個Playbook之前,Ansible默認會先抓取Playbook中所指定的所有主機的系統信息,這些信息我們稱之為Facts。

ansible munin -m setup

設置gather_facts:no來暫時讓Ansible在執行Playbook任務之前跳過收集遠程主機Facts信息這一步,這樣可以為任務節省幾秒鍾的時間

交互式提示

---

  • hosts: all
    vars_prompt:
    • name: share_user
      prompt: "What is your network username?"
    • name: share_pass
      prompt: "What is your network password?"
      private: yes

Block

---

  • hosts: web
    tasks:

    Install and configure Apache on RedHat/CentOS hosts.

    • block:
      • yum: name=httpd state=present
      • template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
      • service: name=httpd state=started enabled=yes
        when: ansible_os_family == 'RedHat'
        sudo: yes

    Install and configure Apache on Debian/Ubuntu hosts.

    • block:
      • apt: name=apache2 state=present
      • template: src=httpd.conf.j2 dest=/etc/apache2/apache2.conf
      • service: name=apache2 state=started enabled=yes
        when: ansible_os_family == 'Debian'
        sudo: yes

塊功能非常適合於多個任務共用同一套任務參數的情況

control node

控制端版本

ansible-core 2.11 and Ansible 4.0.0

make Python 3.8 a soft dependency

ansible-core 2.12 and Ansible 5.0.0

will require Python 3.8 or newer

Starting with ansible-core 2.11, the project will only be packaged for Python 3.8 and newer.

版本

  • 從2.10版本開始
    community package called ansible
    minimalist language and runtime called ansible-core (called ansible-base in version 2.10)

Includes

- include: ./static_git_pull.yml

Includes引用方式的好處不言而喻:簡潔,干凈,解耦,復用度高,易於維護

- include: ./static_git_pull.yml

when
動態引用

Roles

相對Includes功能,Roles更適合於大項目Playbook的編排架構

Roles不僅支持Tasks的集合,同時包括vars_files、tasks、handlers、meta、templates

Roles主要依賴於目錄的命名和擺放,默認tasks/main.yml是所有任務的入口

每個目錄下均由main.yml定義該功能的任務集

引用role的方式


  • hosts: all
    roles:
    • role_name

·roles/x/tasks/main.yml:主函數,包括在其中的所有任務將被執行。

·roles/x/handlers/main.yml:所有包括其中的handlers將被執行。
·roles/x/vars/main.yml:所有包括在其中的變量將在roles中生效。
·roles/x/meta/main.yml:roles所有依賴將被正常登入。
·roles/x/{files,templates,tasks}/(dir depends on task):所有文件、模板都可存放在這里,放在這里最大的好處是不用指定絕對路徑。

Jinja2

  • 變量的提取使用{{variable}},{%statement execution%}括起來的內容為Jinja2命令執行語句
  • {% for item in all_items %}
    {{ item }}
    {% endfor %}
  • bind-address=0.0.0.0:{{ PORT | default(3306) }}

Galaxy

  • collections
  • Galaxy provides pre-packaged units of work such as roles, and new in Galaxy 3.2, collections You can find roles for provisioning infrastructure, deploying applications, and all of the tasks you do everyday. The collection format provides a comprehensive package of automation that may include multiple playbooks, roles, modules, and plugins.

XMind: ZEN - Trial Version


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM