自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Ansible 劇本快速入門(mén)指南

運(yùn)維 系統(tǒng)運(yùn)維
我們已經(jīng)寫(xiě)了兩篇關(guān)于 Ansible 的文章,這是第三篇。如果你是 Ansible 新手,我建議你閱讀下面這兩篇文章,它會(huì)教你一些 Ansible 的基礎(chǔ)以及它是什么。如果你已經(jīng)閱讀過(guò)了,那么在閱讀本文時(shí)你才不會(huì)感到突兀。

[[384413]]

我們已經(jīng)寫(xiě)了兩篇關(guān)于 Ansible 的文章,這是第三篇。

如果你是 Ansible 新手,我建議你閱讀下面這兩篇文章,它會(huì)教你一些 Ansible 的基礎(chǔ)以及它是什么。

如果你已經(jīng)閱讀過(guò)了,那么在閱讀本文時(shí)你才不會(huì)感到突兀。

什么是 Ansible 劇本?

劇本playbook比點(diǎn)對(duì)點(diǎn)命令模式更強(qiáng)大,而且完全不同。

它使用了 /usr/bin/ansible-playbook 二進(jìn)制文件,并且提供豐富的特性使得復(fù)雜的任務(wù)變得更容易。

如果你想經(jīng)常運(yùn)行一個(gè)任務(wù),劇本是非常有用的。此外,如果你想在服務(wù)器組上執(zhí)行多個(gè)任務(wù),它也是非常有用的。

劇本是由 YAML 語(yǔ)言編寫(xiě)。YAML 代表一種標(biāo)記語(yǔ)言,它比其它常見(jiàn)的數(shù)據(jù)格式(如 XML 或 JSON)更容易讀寫(xiě)。

下面這張 Ansible 劇本流程圖將告訴你它的詳細(xì)結(jié)構(gòu)。

 

理解 Ansible 劇本的術(shù)語(yǔ)

  • 控制節(jié)點(diǎn)Control node:Ansible 安裝的機(jī)器,它負(fù)責(zé)管理客戶端節(jié)點(diǎn)。
  • 受控節(jié)點(diǎn)Managed node:控制節(jié)點(diǎn)管理的主機(jī)列表。
  • 劇本playbook:一個(gè)劇本文件包含一組自動(dòng)化任務(wù)。
  • 主機(jī)清單Inventory:這個(gè)文件包含有關(guān)管理的服務(wù)器的信息。
  • 任務(wù)Task:每個(gè)劇本都有大量的任務(wù)。任務(wù)在指定機(jī)器上依次執(zhí)行(一個(gè)主機(jī)或多個(gè)主機(jī))。
  • 模塊Module: 模塊是一個(gè)代碼單元,用于從客戶端節(jié)點(diǎn)收集信息。
  • 角色Role:角色是根據(jù)已知文件結(jié)構(gòu)自動(dòng)加載一些變量文件、任務(wù)和處理程序的方法。
  • 動(dòng)作Play:每個(gè)劇本含有大量的動(dòng)作,一個(gè)動(dòng)作從頭到尾執(zhí)行一個(gè)特定的自動(dòng)化。
  • 處理程序Handler: 它可以幫助你減少在劇本中的重啟任務(wù)。處理程序任務(wù)列表實(shí)際上與常規(guī)任務(wù)沒(méi)有什么不同,更改由通知程序通知。如果處理程序沒(méi)有收到任何通知,它將不起作用。

基本的劇本是怎樣的?

下面是一個(gè)劇本的模板:

  1. --- [YAML 文件應(yīng)該以三個(gè)破折號(hào)開(kāi)頭]
  2. - name: [腳本描述]
  3. hosts: group [添加主機(jī)或主機(jī)組]
  4. become: true [如果你想以 root 身份運(yùn)行任務(wù),則標(biāo)記它]
  5. tasks: [你想在任務(wù)下執(zhí)行什么動(dòng)作]
  6. - name: [輸入模塊選項(xiàng)]
  7. module: [輸入要執(zhí)行的模塊]
  8. module_options-1: value [輸入模塊選項(xiàng)]
  9. module_options-2: value
  10. .
  11. module_options-N: value

如何理解 Ansible 的輸出

Ansible 劇本的輸出有四種顏色,下面是具體含義:

  • 綠色ok 代表成功,關(guān)聯(lián)的任務(wù)數(shù)據(jù)已經(jīng)存在,并且已經(jīng)根據(jù)需要進(jìn)行了配置。
  • 黃色changed 指定的數(shù)據(jù)已經(jīng)根據(jù)任務(wù)的需要更新或修改。
  • 紅色FAILED 如果在執(zhí)行任務(wù)時(shí)出現(xiàn)任何問(wèn)題,它將返回一個(gè)失敗消息,它可能是任何東西,你需要相應(yīng)地修復(fù)它。
  • 白色:表示有多個(gè)參數(shù)。

為此,創(chuàng)建一個(gè)劇本目錄,將它們都放在同一個(gè)地方。

  1. $ sudo mkdir /etc/ansible/playbooks

劇本-1:在 RHEL 系統(tǒng)上安裝 Apache Web 服務(wù)器

這個(gè)示例劇本允許你在指定的目標(biāo)機(jī)器上安裝 Apache Web 服務(wù)器:

  1. $ sudo nano /etc/ansible/playbooks/apache.yml
  2.  
  3. ---
  4. - hosts: web
  5. become: yes
  6. name: "Install and Configure Apache Web server"
  7. tasks:
  8. - name: "Install Apache Web Server"
  9. yum:
  10. name: httpd
  11. state: latest
  12. - name: "Ensure Apache Web Server is Running"
  13. service:
  14. name: httpd
  15. state: started
  1. $ ansible-playbook apache1.yml

 

如何理解 Ansible 中劇本的執(zhí)行

使用以下命令來(lái)查看語(yǔ)法錯(cuò)誤。如果沒(méi)有發(fā)現(xiàn)錯(cuò)誤,它只顯示劇本文件名。如果它檢測(cè)到任何錯(cuò)誤,你將得到一個(gè)如下所示的錯(cuò)誤,但內(nèi)容可能根據(jù)你的輸入文件而有所不同。

  1. $ ansible-playbook apache1.yml --syntax-check
  2.  
  3. ERROR! Syntax Error while loading YAML.
  4. found a tab character that violate indentation
  5. The error appears to be in '/etc/ansible/playbooks/apache1.yml': line 10, column 1, but may
  6. be elsewhere in the file depending on the exact syntax problem.
  7. The offending line appears to be:
  8. state: latest
  9. ^ here
  10. There appears to be a tab character at the start of the line.
  11.  
  12. YAML does not use tabs for formatting. Tabs should be replaced with spaces.
  13. For example:
  14. - name: update tooling
  15. vars:
  16. version: 1.2.3
  17. # ^--- there is a tab there.
  18. Should be written as:
  19. - name: update tooling
  20. vars:
  21. version: 1.2.3
  22. # ^--- all spaces here.

或者,你可以使用這個(gè) URL YAML Lint 在線檢查 Ansible 劇本內(nèi)容。

執(zhí)行以下命令進(jìn)行“演練”。當(dāng)你運(yùn)行帶有 --check 選項(xiàng)的劇本時(shí),它不會(huì)對(duì)遠(yuǎn)程機(jī)器進(jìn)行任何修改。相反,它會(huì)告訴你它將要做什么改變但不是真的執(zhí)行。

  1. $ ansible-playbook apache.yml --check
  2.  
  3. PLAY [Install and Configure Apache Webserver] ********************************************************************
  4.  
  5. TASK [Gathering Facts] *******************************************************************************************
  6. ok: [node2.2g.lab]
  7. ok: [node1.2g.lab]
  8.  
  9. TASK [Install Apache Web Server] *********************************************************************************
  10. changed: [node2.2g.lab]
  11. changed: [node1.2g.lab]
  12.  
  13. TASK [Ensure Apache Web Server is Running] ***********************************************************************
  14. changed: [node1.2g.lab]
  15. changed: [node2.2g.lab]
  16.  
  17. PLAY RECAP *******************************************************************************************************
  18. node1.2g.lab : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
  19. node2.2g.lab : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

如果你想要知道 ansible 劇本實(shí)現(xiàn)的詳細(xì)信息,使用 -vv 選項(xiàng),它會(huì)展示如何收集這些信息。

  1. $ ansible-playbook apache.yml --check -vv
  2.  
  3. ansible-playbook 2.9.2
  4. config file = /etc/ansible/ansible.cfg
  5. configured module search path = ['/home/daygeek/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  6. ansible python module location = /usr/lib/python3.8/site-packages/ansible
  7. executable location = /usr/bin/ansible-playbook
  8. python version = 3.8.1 (default, Jan 8 2020, 23:09:20) [GCC 9.2.0]
  9. Using /etc/ansible/ansible.cfg as config file
  10.  
  11. PLAYBOOK: apache.yml *****************************************************************************************************
  12. 1 plays in apache.yml
  13.  
  14. PLAY [Install and Configure Apache Webserver] ****************************************************************************
  15.  
  16. TASK [Gathering Facts] ***************************************************************************************************
  17. task path: /etc/ansible/playbooks/apache.yml:2
  18. ok: [node2.2g.lab]
  19. ok: [node1.2g.lab]
  20. META: ran handlers
  21.  
  22. TASK [Install Apache Web Server] *****************************************************************************************
  23. task path: /etc/ansible/playbooks/apache.yml:6
  24. changed: [node2.2g.lab] => {"changed": true, "msg": "Check mode: No changes made, but would have if not in check mod
  25. e", "rc": 0, "results": ["Installed: httpd"]}
  26. changed: [node1.2g.lab] => {"changed": true, "changes": {"installed": ["httpd"], "updated": []}, "msg": "", "obsolet
  27. es": {"urw-fonts": {"dist": "noarch", "repo": "@anaconda", "version": "2.4-16.el7"}}, "rc": 0, "results": []}
  28.  
  29. TASK [Ensure Apache Web Server is Running] *******************************************************************************
  30. task path: /etc/ansible/playbooks/apache.yml:10
  31. changed: [node1.2g.lab] => {"changed": true, "msg": "Service httpd not found on host, assuming it will exist on full run"}
  32. changed: [node2.2g.lab] => {"changed": true, "msg": "Service httpd not found on host, assuming it will exist on full run"}
  33. META: ran handlers
  34. META: ran handlers
  35.  
  36. PLAY RECAP ***************************************************************************************************************
  37. node1.2g.lab : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
  38. node2.2g.lab : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

劇本-2:在 Ubuntu 系統(tǒng)上安裝 Apache Web 服務(wù)器

這個(gè)示例劇本允許你在指定的目標(biāo)節(jié)點(diǎn)上安裝 Apache Web 服務(wù)器。

  1. $ sudo nano /etc/ansible/playbooks/apache-ubuntu.yml
  2.  
  3. ---
  4. - hosts: web
  5. become: yes
  6. name: "Install and Configure Apache Web Server"
  7. tasks:
  8. - name: "Install Apache Web Server"
  9. yum:
  10. name: apache2
  11. state: latest
  12.  
  13. - name: "Start the Apache Web Server"
  14. service:
  15. name: apaceh2
  16. state: started
  17.  
  18. - name: "Enable mod_rewrite module"
  19. apache2_module:
  20. name: rewrite
  21. state: present
  22.  
  23. notify:
  24. - start apache
  25.  
  26. handlers:
  27. - name: "Ensure Apache Web Server is Running"
  28. service:
  29. name: apache2
  30. state: restarted
  31. enabled: yes

劇本-3:在 Red Hat 系統(tǒng)上安裝軟件包列表

這個(gè)示例劇本允許你在指定的目標(biāo)節(jié)點(diǎn)上安裝軟件包。

方法-1:

  1. $ sudo nano /etc/ansible/playbooks/packages-redhat.yml
  2.  
  3. ---
  4. - hosts: web
  5. become: yes
  6. name: "Install a List of Packages on Red Hat Based System"
  7. tasks:
  8. - name: "Installing a list of packages"
  9. yum:
  10. name:
  11. - curl
  12. - httpd
  13. - nano
  14. - htop

方法-2:

  1. $ sudo nano /etc/ansible/playbooks/packages-redhat-1.yml
  2.  
  3. ---
  4. - hosts: web
  5. become: yes
  6. name: "Install a List of Packages on Red Hat Based System"
  7. tasks:
  8. - name: "Installing a list of packages"
  9. yum: name={{ item }} state=latest
  10. with_items:
  11. - curl
  12. - httpd
  13. - nano
  14. - htop

方法-3:使用數(shù)組變量

  1. $ sudo nano /etc/ansible/playbooks/packages-redhat-2.yml
  2.  
  3. ---
  4. - hosts: web
  5. become: yes
  6. name: "Install a List of Packages on Red Hat Based System"
  7. vars:
  8. packages: [ 'curl', 'git', 'htop' ]
  9. tasks:
  10. - name: Install a list of packages
  11. yum: name={{ item }} state=latest
  12. with_items: "{{ packages }}"

劇本-4:在 Linux 系統(tǒng)上安裝更新

這個(gè)示例劇本允許你在基于 Red Hat 或 Debian 的 Linux 系統(tǒng)上安裝更新。

  1. $ sudo nano /etc/ansible/playbooks/security-update.yml
  2.  
  3. ---
  4. - hosts: web
  5. become: yes
  6. name: "Install Security Update"
  7. tasks:
  8. - name: "Installing Security Update on Red Hat Based System"
  9. yum: name=* update_cache=yes security=yes state=latest
  10. when: ansible_facts['distribution'] == "CentOS"
  11.  
  12. - name: "Installing Security Update on Ubuntu Based System"
  13. apt: upgrade=dist update_cache=yes
  14. when: ansible_facts['distribution'] == "Ubuntu"

 

 

責(zé)任編輯:龐桂玉 來(lái)源: Linux中國(guó)
相關(guān)推薦

2021-03-01 13:00:21

Ansible系統(tǒng)運(yùn)維

2021-02-22 18:50:03

Ansible系統(tǒng)運(yùn)維

2011-03-08 16:50:35

2019-11-13 15:44:17

Kafka架構(gòu)數(shù)據(jù)

2020-11-25 19:05:50

云計(jì)算SaaS公有云

2025-02-28 08:42:53

SpringNetflixHystrix

2021-06-29 08:00:00

Ansible開(kāi)發(fā)工具

2024-08-27 09:09:49

Web系統(tǒng)JSP

2020-11-13 05:49:09

物聯(lián)網(wǎng)城域網(wǎng)IOT

2010-12-23 13:45:23

Office 2010批量激活

2021-01-24 16:00:22

Ansible系統(tǒng)運(yùn)維

2015-08-17 14:13:52

Ansible輕量自動(dòng)化部署工具

2024-01-10 17:24:00

2020-05-11 09:54:33

JavaScript開(kāi)發(fā)技術(shù)

2022-10-28 18:36:18

2023-12-19 09:36:35

PostgreSQL數(shù)據(jù)庫(kù)開(kāi)源

2019-03-08 11:29:05

開(kāi)源自動(dòng)化工具Ansible

2011-05-18 15:15:44

MySQL

2010-08-03 15:19:08

FlexBuilder

2021-03-26 10:31:19

人工智能AIOps
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)