一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - Ansible 劇本快速入門指南

Ansible 劇本快速入門指南

2021-03-02 23:39Linux中國Magesh Maruthamuthu Python

我們已經寫了兩篇關于 Ansible 的文章,這是第三篇。如果你是 Ansible 新手,我建議你閱讀下面這兩篇文章,它會教你一些 Ansible 的基礎以及它是什么。如果你已經閱讀過了,那么在閱讀本文時你才不會感到突兀。

Ansible 劇本快速入門指南

我們已經寫了兩篇關于 Ansible 的文章,這是第三篇。

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

如果你已經閱讀過了,那么在閱讀本文時你才不會感到突兀。

什么是 Ansible 劇本?

 

劇本playbook比點對點命令模式更強大,而且完全不同。

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

如果你想經常運行一個任務,劇本是非常有用的。此外,如果你想在服務器組上執行多個任務,它也是非常有用的。

劇本是由 YAML 語言編寫。YAML 代表一種標記語言,它比其它常見的數據格式(如 XML 或 JSON)更容易讀寫。

下面這張 Ansible 劇本流程圖將告訴你它的詳細結構。

 

Ansible 劇本快速入門指南

理解 Ansible 劇本的術語

 

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

基本的劇本是怎樣的?

 

下面是一個劇本的模板:

--- [YAML 文件應該以三個破折號開頭]

- name: [腳本描述]

hosts: group [添加主機或主機組]

become: true [如果你想以 root 身份運行任務,則標記它]

tasks: [你想在任務下執行什么動作]

- name: [輸入模塊選項]

module: [輸入要執行的模塊]

module_options-1: value [輸入模塊選項]

module_options-2: value

.

module_options-N: value

如何理解 Ansible 的輸出

 

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

  • 綠色ok 代表成功,關聯的任務數據已經存在,并且已經根據需要進行了配置。
  • 黃色changed 指定的數據已經根據任務的需要更新或修改。
  • 紅色FAILED 如果在執行任務時出現任何問題,它將返回一個失敗消息,它可能是任何東西,你需要相應地修復它。
  • 白色:表示有多個參數。

為此,創建一個劇本目錄,將它們都放在同一個地方。

$ sudo mkdir /etc/ansible/playbooks

劇本-1:在 RHEL 系統上安裝 Apache Web 服務器

 

這個示例劇本允許你在指定的目標機器上安裝 Apache Web 服務器:

$ sudo nano /etc/ansible/playbooks/apache.yml

 

---

- hosts: web

become: yes

name: "Install and Configure Apache Web server"

tasks:

- name: "Install Apache Web Server"

yum:

name: httpd

state: latest

- name: "Ensure Apache Web Server is Running"

service:

name: httpd

state: started

$ ansible-playbook apache1.yml

 

Ansible 劇本快速入門指南

如何理解 Ansible 中劇本的執行

 

使用以下命令來查看語法錯誤。如果沒有發現錯誤,它只顯示劇本文件名。如果它檢測到任何錯誤,你將得到一個如下所示的錯誤,但內容可能根據你的輸入文件而有所不同。

$ ansible-playbook apache1.yml --syntax-check

 

ERROR! Syntax Error while loading YAML.

found a tab character that violate indentation

The error appears to be in '/etc/ansible/playbooks/apache1.yml': line 10, column 1, but may

be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

state: latest

^ here

There appears to be a tab character at the start of the line.

 

YAML does not use tabs for formatting. Tabs should be replaced with spaces.

For example:

- name: update tooling

vars:

version: 1.2.3

# ^--- there is a tab there.

Should be written as:

- name: update tooling

vars:

version: 1.2.3

# ^--- all spaces here.

或者,你可以使用這個 URL YAML Lint 在線檢查 Ansible 劇本內容。

執行以下命令進行“演練”。當你運行帶有 --check 選項的劇本時,它不會對遠程機器進行任何修改。相反,它會告訴你它將要做什么改變但不是真的執行。

$ ansible-playbook apache.yml --check

 

PLAY [Install and Configure Apache Webserver] ********************************************************************

 

TASK [Gathering Facts] *******************************************************************************************

ok: [node2.2g.lab]

ok: [node1.2g.lab]

 

TASK [Install Apache Web Server] *********************************************************************************

changed: [node2.2g.lab]

changed: [node1.2g.lab]

 

TASK [Ensure Apache Web Server is Running] ***********************************************************************

changed: [node1.2g.lab]

changed: [node2.2g.lab]

 

PLAY RECAP *******************************************************************************************************

node1.2g.lab : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

node2.2g.lab : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

如果你想要知道 ansible 劇本實現的詳細信息,使用 -vv 選項,它會展示如何收集這些信息。

$ ansible-playbook apache.yml --check -vv

 

ansible-playbook 2.9.2

config file = /etc/ansible/ansible.cfg

configured module search path = ['/home/daygeek/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']

ansible python module location = /usr/lib/python3.8/site-packages/ansible

executable location = /usr/bin/ansible-playbook

python version = 3.8.1 (default, Jan 8 2020, 23:09:20) [GCC 9.2.0]

Using /etc/ansible/ansible.cfg as config file

 

PLAYBOOK: apache.yml *****************************************************************************************************

1 plays in apache.yml

 

PLAY [Install and Configure Apache Webserver] ****************************************************************************

 

TASK [Gathering Facts] ***************************************************************************************************

task path: /etc/ansible/playbooks/apache.yml:2

ok: [node2.2g.lab]

ok: [node1.2g.lab]

META: ran handlers

 

TASK [Install Apache Web Server] *****************************************************************************************

task path: /etc/ansible/playbooks/apache.yml:6

changed: [node2.2g.lab] => {"changed": true, "msg": "Check mode: No changes made, but would have if not in check mod

e", "rc": 0, "results": ["Installed: httpd"]}

changed: [node1.2g.lab] => {"changed": true, "changes": {"installed": ["httpd"], "updated": []}, "msg": "", "obsolet

es": {"urw-fonts": {"dist": "noarch", "repo": "@anaconda", "version": "2.4-16.el7"}}, "rc": 0, "results": []}

 

TASK [Ensure Apache Web Server is Running] *******************************************************************************

task path: /etc/ansible/playbooks/apache.yml:10

changed: [node1.2g.lab] => {"changed": true, "msg": "Service httpd not found on host, assuming it will exist on full run"}

changed: [node2.2g.lab] => {"changed": true, "msg": "Service httpd not found on host, assuming it will exist on full run"}

META: ran handlers

META: ran handlers

 

PLAY RECAP ***************************************************************************************************************

node1.2g.lab : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

node2.2g.lab : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

劇本-2:在 Ubuntu 系統上安裝 Apache Web 服務器

 

這個示例劇本允許你在指定的目標節點上安裝 Apache Web 服務器。

$ sudo nano /etc/ansible/playbooks/apache-ubuntu.yml

 

---

- hosts: web

become: yes

name: "Install and Configure Apache Web Server"

tasks:

- name: "Install Apache Web Server"

yum:

name: apache2

state: latest

 

- name: "Start the Apache Web Server"

service:

name: apaceh2

state: started

 

- name: "Enable mod_rewrite module"

apache2_module:

name: rewrite

state: present

 

notify:

- start apache

 

handlers:

- name: "Ensure Apache Web Server is Running"

service:

name: apache2

state: restarted

enabled: yes

劇本-3:在 Red Hat 系統上安裝軟件包列表

 

這個示例劇本允許你在指定的目標節點上安裝軟件包。

方法-1:

$ sudo nano /etc/ansible/playbooks/packages-redhat.yml

 

---

- hosts: web

become: yes

name: "Install a List of Packages on Red Hat Based System"

tasks:

- name: "Installing a list of packages"

yum:

name:

- curl

- httpd

- nano

- htop

方法-2:

$ sudo nano /etc/ansible/playbooks/packages-redhat-1.yml

 

---

- hosts: web

become: yes

name: "Install a List of Packages on Red Hat Based System"

tasks:

- name: "Installing a list of packages"

yum: name={{ item }} state=latest

with_items:

- curl

- httpd

- nano

- htop

方法-3:使用數組變量

$ sudo nano /etc/ansible/playbooks/packages-redhat-2.yml

 

---

- hosts: web

become: yes

name: "Install a List of Packages on Red Hat Based System"

vars:

packages: [ 'curl', 'git', 'htop' ]

tasks:

- name: Install a list of packages

yum: name={{ item }} state=latest

with_items: "{{ packages }}"

劇本-4:在 Linux 系統上安裝更新

 

這個示例劇本允許你在基于 Red Hat 或 Debian 的 Linux 系統上安裝更新。

$ sudo nano /etc/ansible/playbooks/security-update.yml

 

---

- hosts: web

become: yes

name: "Install Security Update"

tasks:

- name: "Installing Security Update on Red Hat Based System"

yum: name=* update_cache=yes security=yes state=latest

when: ansible_facts['distribution'] == "CentOS"

 

- name: "Installing Security Update on Ubuntu Based System"

apt: upgrade=dist update_cache=yes

when: ansible_facts['distribution'] == "Ubuntu"

原文地址:https://linux.cn/article-13167-1.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: a级免费观看| 亚洲欧美日韩精品久久亚洲区 | 24adc年龄18岁欢迎大驾光临 | 秋霞黄色片 | 深夜在线小视频 | chinese壮直男gay老年人 | 91桃色网站| 亚洲美日韩 | 亚洲无线一二三区2021 | 亚洲卡一卡2卡三卡4麻豆 | 国产福利一区二区三区 | 禁忌h1v1怀孕 | 欧美黑大吊 | 激情视频激情小说 | 娇小老少配xxxxx性视频 | 肠交女王magnet | 免费一区在线观看 | 天若有情1992国语版完整版 | 亚洲AV 日韩 国产 有码 | 2019年国产不卡在线刷新 | 国产日韩欧美在线一二三四 | 精品精品国产自在久久高清 | 国产新疆成人a一片在线观看 | bl动漫在线观看 | 热久久最新视频 | 日本中文字幕永久在线 | 天天快乐高清在线观看 | 98pao强力打造高清免费 | 亚洲视频1 | 日本十大顶级绝伦推理片 | 成人免费视频在 | 国产清纯91天堂在线观看 | 99热在线这里只有精品 | 国产精品男人的天堂 | 91麻豆精品国产自产在线 | 亚欧洲乱码专区视频 | 成人国产在线视频 | 亚洲午夜久久久久国产 | 日韩一区三区 | 爱情岛论坛亚洲自拍 | 欧美日韩人成在线观看 |