Ansible 简明教程

Ansible - Introduction

Ansible 是简单开源的 IT 引擎,它自动执行应用程序部署、内部服务编排、云供应和许多其他 IT 工具。

Ansible is simple open source IT engine which automates application deployment, intra service orchestration, cloud provisioning and many other IT tools.

Ansible 易于部署,因为它不使用任何代理或自定义安全基础架构。

Ansible is easy to deploy because it does not use any agents or custom security infrastructure.

Ansible 使用 playbook 来描述自动化作业,而 playbook 使用非常简单的语言,即 YAML (它是一种人类可读的数据序列化语言,通常用于配置文件,但可用于数据存储的许多应用程序中),非常容易让人理解、阅读和编写。因此,好处还在于,即使 IT 基础设施支持人员也可以阅读和理解 playbook,并在需要时对其进行调试(YAML - 以人类可读的格式)。

Ansible uses playbook to describe automation jobs, and playbook uses very simple language i.e. YAML (It’s a human-readable data serialization language & is commonly used for configuration files, but could be used in many applications where data is being stored)which is very easy for humans to understand, read and write. Hence the advantage is that even the IT infrastructure support guys can read and understand the playbook and debug if needed (YAML – It is in human readable form).

Ansible 设计用于多层部署。Ansible 不会一次管理一个系统,而是通过描述所有系统之间的相互关联来建模 IT 基础架构。Ansible 完全不含代理,这意味着 Ansible 通过 ssh(默认情况下)连接你的节点来工作。但是,如果你想要其他连接方法,如 Kerberos,Ansible 会向你提供该选项。

Ansible is designed for multi-tier deployment. Ansible does not manage one system at time, it models IT infrastructure by describing all of your systems are interrelated. Ansible is completely agentless which means Ansible works by connecting your nodes through ssh(by default). But if you want other method for connection like Kerberos, Ansible gives that option to you.

在连接到你的节点后,Ansible 会推送被称为“Ansible 模块”的小程序。Ansible 在你的节点上运行该模块,并在完成后将其删除。Ansible 在简单文本文件中(这些是主机文件)管理你的清单。Ansible 使用主机文件,其中可以对主机进行分组,并可在 playbook 中控制对特定组的操作。

After connecting to your nodes, Ansible pushes small programs called as “Ansible Modules”. Ansible runs that modules on your nodes and removes them when finished. Ansible manages your inventory in simple text files (These are the hosts file). Ansible uses the hosts file where one can group the hosts and can control the actions on a specific group in the playbooks.

Sample Hosts File

以下是主机文件的内容 -

This is the content of hosts file −

#File name: hosts
#Description: Inventory file for your application. Defines machine type abc
node to deploy specific artifacts
# Defines machine type def node to upload
metadata.

[abc-node]
#server1 ansible_host = <target machine for DU deployment> ansible_user = <Ansible
user> ansible_connection = ssh
server1 ansible_host = <your host name> ansible_user = <your unix user>
ansible_connection = ssh

[def-node]
#server2 ansible_host = <target machine for artifact upload>
ansible_user = <Ansible user> ansible_connection = ssh
server2 ansible_host = <host> ansible_user = <user> ansible_connection = ssh

What is Configuration Management

根据 Ansible 的配置管理,它通过保留记录和更新描述企业硬件和软件的详细信息来维护产品性能配置。

Configuration management in terms of Ansible means that it maintains configuration of the product performance by keeping a record and updating detailed information which describes an enterprise’s hardware and software.

此类信息通常包括已应用于已安装软件包的确切版本和更新,以及硬件设备的位置和网络地址。例如,如果你想在你的企业中所有机器上安装 WebLogic/WebSphere 服务器的新版本,对你来说手动更新所有机器是不现实的。

Such information typically includes the exact versions and updates that have been applied to installed software packages and the locations and network addresses of hardware devices. For e.g. If you want to install the new version of WebLogic/WebSphere server on all of the machines present in your enterprise, it is not feasible for you to manually go and update each and every machine.

你可以通过以最简单的方式编写的 Ansible playbook 和清单,一次性在所有机器上安装 WebLogic/WebSphere。你要做的就是将你的节点的 IP 地址列在清单中,然后编写 playbook 以安装 WebLogic/WebSphere。从你的控制机器运行 playbook,它将安装在你的所有节点上。

You can install WebLogic/WebSphere in one go on all of your machines with Ansible playbooks and inventory written in the most simple way. All you have to do is list out the IP addresses of your nodes in the inventory and write a playbook to install WebLogic/WebSphere. Run the playbook from your control machine & it will be installed on all your nodes.

How Ansible Works?

下图显示了 Ansible 的工作原理。

The picture given below shows the working of Ansible.

Ansible works 通过连接到你的节点,并向其推送名为“ Ansible 模块”的小程序。然后 Ansible 执行这些模块(默认情况下通过 SSH),并在完成后将其删除。你的模块库可以驻留在任何机器上,并且不需要服务器、守护程序或数据库。

Ansible works by connecting to your nodes and pushing out small programs, called "Ansible modules" to them. Ansible then executes these modules (over SSH by default), and removes them when finished. Your library of modules can reside on any machine, and there are no servers, daemons, or databases required.

ansible works

上图中的管理节点是控制节点(管理节点),它控制整个 playbook 的执行。这是你运行安装的节点。清单文件提供了需要运行 Ansible 模块的主机列表,而管理节点执行 SSH 连接并在主机机器上执行小模块并安装产品/软件。

The management node in the above picture is the controlling node (managing node) which controls the entire execution of the playbook. It’s the node from which you are running the installation. The inventory file provides the list of hosts where the Ansible modules needs to be run and the management node does a SSH connection and executes the small modules on the hosts machine and installs the product/software.

Ansible 的 Beauty 是它在安装好模块后会将其删除,因此它有效地连接到主机,执行指令,如果成功安装了在主机机器上执行的代码,则将该代码删除。

Beauty of Ansible is that it removes the modules once those are installed so effectively it connects to host machine , executes the instructions and if it’s successfully installed removes the code which was copied on the host machine which was executed.