37 · Introducing Network Programmability網路可程式化簡介

AnsibleAnsible

Ansible is an open-source software platform for configuring and managing compute and switching infrastructure using playbooks. It was created in 2012 as an alternative to Puppet and Chef. Ansible was later acquired by Red Hat in 2015. Ansible features a state-driven resource model that describes the desired state of computer systems and services. It is used to automate the configuration of a company’s compute and switching resources in an agentless manner. Another notable difference between Puppet, Chef, Terraform, and Ansible is that Ansible is written in Python. It also uses YAML as a language to write Playbooks, resulting in human readable scripts.

Ansible 是一套開放原始碼軟體平台,使用 playbook(劇本)來設定與管理運算及交換基礎架構。它於 2012 年推出,作為 Puppet 和 Chef 的替代方案。Ansible 後來在 2015 年被 Red Hat 收購。Ansible 具有狀態驅動的資源模型,描述電腦系統與服務的期望狀態。它以無代理程式(agentless)的方式,用來自動化公司運算與交換資源的設定。Puppet、Chef、Terraform 與 Ansible 之間另一個顯著差異是,Ansible 以 Python 撰寫。它也使用 YAML 作為撰寫 Playbook 的語言,讓腳本易於閱讀。

Being natively agentless significantly lowers the barrier to entry from an automation perspective. Since Ansible is agentless, it can integrate and automate any device using any API. For example, integrations can use REST APIs, NETCONF, SSH, or even SNMP, if desired.

原生無代理程式的特性大幅降低了自動化導入的門檻。由於 Ansible 是無代理程式的,因此可以整合並自動化使用任何 API 的任何裝置。舉例來說,整合可使用 REST API、NETCONF、SSH,甚至 SNMP,視需求而定。

The key Ansible components are:

Ansible 的關鍵元件包括:

  • Inventory: Contains the hosts operated by Ansible.Inventory(清單):包含由 Ansible 操作的主機。
  • Modules: Modules are the components that do the actual work in Ansible. They are what gets executed (applied) in each playbook task.Modules(模組):模組是 Ansible 中實際執行工作的元件。它們是每個 playbook 任務中會被執行(套用)的部分。
  • Playbooks: A playbook is composed of one or more plays in an ordered list. Each play executes part of the overall goal of the playbook, running one or more tasks. Each task calls an Ansible module, which orchestrates, configures, administers, or deploys. These playbooks describe the policy to be executed to the host or hosts. People refer to these playbooks as "design plans", which are designed to be human-readable and are developed in the basic text language, YAML.Playbooks(劇本):一個 playbook 由一份有順序的 plays(劇集)清單組成。每個 play 執行 playbook 整體目標的一部分,運行一個或多個任務。每個任務會呼叫一個 Ansible 模組,用來協調、設定、管理或部署。這些 playbook 描述要在主機上執行的政策。人們將這些 playbook 稱為「設計計畫」,其設計目的是要讓人可讀,並以基本文字語言 YAML 撰寫。
  • ansible.cfg: The default configuration file that controls the operation of Ansible.ansible.cfg:控制 Ansible 運作方式的預設設定檔。

A network or DevOps engineer typically defines a playbook with the desired state of the target device on the Ansible controller. The Ansible controller then establishes a connection (usually SSH, but other plugins can be used) to a target and compares the current state of resources on a target with the desired state as defined in the playbook. If a change is needed, the controller executes the playbook and then notifies the administrator of the changed or unchanged state of the device.

網路或 DevOps 工程師通常會在 Ansible 控制器上定義一份 playbook,描述目標裝置的期望狀態。Ansible 控制器接著會建立與目標的連線(通常是 SSH,但也可使用其他外掛),並比較目標上資源的目前狀態與 playbook 中定義的期望狀態。如果需要變更,控制器會執行該 playbook,然後通知管理員該裝置狀態是否已變更。

How Ansible works

Ansible 的運作方式

  • The Ansible administrator defines a playbook on the controller .Ansible 管理員在控制器上定義一份 playbook。
  • The controller connects to the target device using SSH (or APIs) and pushes the configuration.控制器使用 SSH(或 API)連線至目標裝置並推送設定。
  • The target device applies the playbook with the plays (and its tasks) being performed in order—as defined in the playbook.目標裝置依 playbook 中定義的順序執行其中的 play(及其任務)。
  • Tasks are only performed if the resource is not already in the desired state.只有在資源尚未處於期望狀態時,任務才會被執行。

Key Properties of Ansible

Ansible 的關鍵特性

Remember the following key properties of Ansible:

請記住以下 Ansible 的關鍵特性:

  • Ansible is a procedural configuration management orchestrator that originated from configuration file management on Linux hosts that has extended to network applications.Ansible 是一種程序式(procedural)設定管理協調工具,起源於 Linux 主機上的設定檔管理,之後擴展到網路應用程式。
  • Ansible is a great way to organize scripts to allow for large collections of tasks to be run together and iterated over any number of devices.Ansible 是組織腳本的絕佳方式,可讓大量任務集合一起執行,並針對任意數量的裝置反覆運行。
  • It uses an agentless push model (easy to adopt).它使用無代理程式的推送模型(易於採用)。
  • It leverages YAML to create Ansible playbooks.它利用 YAML 建立 Ansible playbook。
    • An example of an Ansible playbook for interface configuration can be seen in the figure.圖中可看到一個用於介面設定的 Ansible playbook 範例。
Which two statements are correct regarding Ansible? (Choose two.)關於 Ansible,下列哪兩項敘述正確?(選擇兩項。)
What is the role of modules in Ansible?模組(module)在 Ansible 中扮演什麼角色?