配置管理 ansible_Ansible配置管理简介
配置管理 ansible
介绍 (Introduction)
Configuration management is the process of handling changes to a system in a way that assures integrity over time, typically involving tools and processes that facilitate automation and observability. Even though this concept didn’t originate in the IT industry, the term is broadly used to refer to server configuration management.
配置管理是一种处理系统变更的过程,以确保随时间推移的完整性,通常涉及有助于自动化和可观察性的工具和过程。 即使此概念并非起源于IT行业,但该术语仍广泛用于指服务器配置管理 。
In the context of servers, configuration management is also commonly referred to as IT Automation or Server Orchestration. Both terms highlight the practical aspects of configuration management and the ability to control multiple systems from a central server.
在服务器的上下文中,配置管理通常也称为IT自动化或服务器编排 。 这两个术语都强调了配置管理的实际方面以及从中央服务器控制多个系统的能力。
This guide will walk you through the benefits of using a configuration management tool to automate your server infrastructure setup, and how one such tool, Ansible, can help you with that.
本指南将引导您了解使用配置管理工具来自动化服务器基础结构设置的好处,以及一个这样的工具Ansible可以如何帮助您。
使用配置管理工具的好处 (Benefits of Using a Configuration Management Tool)
There are a number of configuration management tools available on the market, with varying levels of complexity and diverse architectural styles. Although each of these tools have their own characteristics and work in slightly different ways, they all provide the same function: make sure a system’s state matches the state described by a set of provisioning scripts.
市场上有许多配置管理工具,具有不同级别的复杂性和不同的体系结构样式。 尽管这些工具中的每一个都有各自的特点,并且工作方式略有不同,但是它们都提供了相同的功能:确保系统状态与一组预配置脚本描述的状态相匹配。
Many of the benefits of configuration management for servers come from the ability to define your infrastructure as code. This enables you to:
服务器配置管理的许多好处来自将基础架构定义为代码的能力。 这使您能够:
- Use a version control system to keep track of any changes in your infrastructure 使用版本控制系统来跟踪基础架构中的任何更改
- Reuse provisioning scripts for multiple server environments, such as development, testing, and production 在多个服务器环境(例如开发,测试和生产)中重用预配脚本
- Share provisioning scripts between coworkers to facilitate collaboration in a standardised development environment 在同事之间共享配置脚本,以促进标准化开发环境中的协作
- Streamline the process of replicating servers, which facilitates recovery from critical errors 简化复制服务器的过程,这有助于从严重错误中进行恢复
Additionally, configuration management tools offer you a way to control one to hundreds of servers from a centralized location, which can dramatically improve efficiency and integrity of your server infrastructure.
此外,配置管理工具为您提供了一种从集中位置控制一到数百台服务器的方法,这可以大大提高服务器基础结构的效率和完整性。
Ansible概述 (Ansible Overview)
Ansible is a modern configuration management tool that facilitates the task of setting up and maintaining remote servers, with a minimalist design intended to get users up and running quickly.
Ansible是一种现代的配置管理工具,可简化设置和维护远程服务器的任务,其极简设计旨在使用户快速启动并运行。
Users write Ansible provisioning scripts in YAML, a user-friendly data serialization standard that is not tied to any particular programming language. This enables users to create sophisticated provisioning scripts more intuitively compared to similar tools in the same category.
用户使用YAML编写Ansible设置脚本, YAML是一种用户友好的数据序列化标准,不依赖于任何特定的编程语言。 与相同类别中的类似工具相比,这使用户可以更直观地创建复杂的配置脚本。
Ansible doesn’t require any special software to be installed on the nodes that will be managed with this tool. A control machine is set up with the Ansible software, which then communicates with the nodes via standard SSH.
Ansible不需要在将使用此工具管理的节点上安装任何特殊软件。 使用Ansible软件设置控制机器,然后通过标准SSH与节点进行通信。
As a configuration management tool and automation framework, Ansible encapsulates all of the common features present in other tools of the same category, while still maintaining a strong focus on simplicity and performance:
作为配置管理工具和自动化框架,Ansible封装了同一类别其他工具中存在的所有常见功能,同时仍然非常重视简单性和性能:
等幂行为 (Idempotent Behavior)
Ansible keeps track of the state of resources in managed systems in order to avoid repeating tasks that were executed before. If a package was already installed, it won’t try to install it again. The objective is that after each provisioning execution the system reaches (or keeps) the desired state, even if you run it multiple times. This is what characterizes Ansible and other configuration management tools as having an idempotent behavior. When running a playbook, you’ll see the status of each task being executed and whether or not the task performed a change in the system.
Ansible跟踪受管系统中资源的状态,以避免重复执行之前执行的任务。 如果已经安装了软件包,则不会尝试再次安装。 目的是在每次执行配置后,即使您多次运行,系统也会达到(或保持)所需的状态。 这就是Ansible和其他配置管理工具具有幂等行为的特征 。 运行剧本时,您将看到正在执行的每个任务的状态以及该任务是否在系统中进行了更改。
支持变量,条件和循环 (Support to Variables, Conditionals, and Loops)
When writing Ansible automation scripts, you can use variables, conditionals, and loops in order to make your automation more versatile and efficient.
在编写Ansible自动化脚本时,可以使用变量,条件和循环,以使自动化更加通用和高效。
系统事实 (System Facts)
Ansible collects a series of detailed information about the managed nodes, such as network interfaces and operating system, and provides it as global variables called system facts. Facts can be used within playbooks to make your automation more versatile and adaptive, behaving differently depending on the system being provisioned.
Ansible收集有关受管节点的一系列详细信息,例如网络接口和操作系统,并将其作为称为系统事实的全局变量提供。 事实可在剧本中使用,以使您的自动化更加通用和适应性强,根据所配置的系统而有所不同。
模板系统 (Templating System)
Ansible uses the Jinja2 Python templating system to allow for dynamic expressions and access to variables. Templates can be used to facilitate setting up configuration files and services. For instance, you can use a template to set up a new virtual host within Apache, while reusing the same template for multiple server installations.
Ansible使用Jinja2 Python模板系统允许动态表达式和访问变量。 模板可用于促进设置配置文件和服务。 例如,您可以使用模板在Apache内设置新的虚拟主机,同时将同一模板重新用于多个服务器安装。
支持扩展和模块 (Support for Extensions and Modules)
Ansible comes with hundreds of built-in modules to facilitate writing automation for common systems administration tasks, such as installing packages with apt and synchronizing files with rsync, and also for dealing with popular software such as database systems (like MySQL, PostgreSQL, MongoDB, and others) and dependency management tools (such as PHP’s composer, Ruby’s gem, Node’s npm, and others). Apart from that, there are various ways in which you can extend Ansible: plugins and modules are good options when you need a custom functionality that is not present by default.
Ansible随附了数百个内置模块,以简化常见系统管理任务的编写自动化,例如安装带有apt软件包以及使用rsync同步文件,以及处理诸如数据库系统(如MySQL,PostgreSQL,MongoDB,和其他)和依赖项管理工具(例如PHP的composer ,Ruby的gem ,Node的npm等)。 除此之外,还有多种扩展Ansible的方法:当您需要默认情况下不存在的自定义功能时,插件和模块是不错的选择。
You can also find third-party modules and plugins in the Ansible Galaxy portal.
您还可以在Ansible Galaxy门户中找到第三方模块和插件。
熟悉Ansible概念 (Getting Familiar with Ansible Concepts)
We’ll now have a look at Ansible terminology and concepts to help familiarize you with these terms as they come up throughout this series.
现在,我们将研究Ansible术语和概念,以帮助您熟悉本系列中出现的这些术语。
控制节点 (Control Node)
A control node is a system where Ansible is installed and set up to connect to your server. You can have multiple control nodes, and any system capable of running Ansible can be set up as a control node, including personal computers or laptops running a Linux or Unix based operating system. For the time being, Ansible can’t be installed on Windows hosts, but you can circumvent this limitation by setting up a virtual machine that runs Linux and running Ansible from there.
控制节点是安装和设置了Ansible以便连接到服务器的系统。 您可以有多个控制节点,任何可以运行Ansible的系统都可以设置为控制节点,包括运行基于Linux或Unix操作系统的个人计算机或便携式计算机。 目前,Ansible不能安装在Windows主机上,但是您可以通过设置运行Linux并从那里运行Ansible的虚拟机来规避此限制。
受管节点 (Managed Nodes)
The systems you control using Ansible are called managed nodes. Ansible requires that managed nodes are reachable via SSH, and have Python 2 (version 2.6 or higher) or Python 3 (version 3.5 or higher) installed.
使用Ansible控制的系统称为受管节点 。 Ansible 要求受管节点可以通过SSH访问,并且必须安装Python 2(2.6版或更高版本)或Python 3(3.5版或更高版本)。
Ansible supports a variety of operating systems including Windows servers as managed nodes.
Ansible支持多种操作系统,包括Windows服务器作为受管节点。
库存 (Inventory)
An inventory file contains a list of the hosts you’ll manage using Ansible. Although Ansible typically creates a default inventory file when installed, you can use per-project inventories to have a better separation of your infrastructure and avoid running commands or playbooks on the wrong server by mistake. Static inventories are usually created as .ini files, but you can also use dynamically generated inventories written in any programming language able to return JSON.
清单文件包含您将使用Ansible管理的主机的列表。 尽管Ansible通常会在安装时创建默认清单文件,但是您可以使用每个项目的清单来更好地分离基础结构,并避免错误地在错误的服务器上运行命令或剧本。 静态清单通常创建为.ini文件,但是您也可以使用以任何能够返回JSON的编程语言编写的动态生成清单。
任务 (Tasks)
In Ansible, a task is an individual unit of work to execute on a managed node. Each action to perform is defined as a task. Tasks can be executed as a one-off action via ad-hoc commands, or included in a playbook as part of an automation script.
在Ansible中, 任务是要在受管节点上执行的单个工作单元。 每个要执行的动作都定义为一项任务。 任务可以通过即席命令一次性执行,也可以作为自动化脚本的一部分包含在剧本中。
剧本 (Playbook)
A playbook contains an ordered list of tasks, and a few other directives to indicate which hosts are the target of that automation, whether or not to use a privilege escalation system to run those tasks, and optional sections to define variables or include files. Ansible executes tasks sequentially, and a full playbook execution is called a play. Playbooks are written in YAML format.
剧本包含任务的有序列表,以及一些其他指令,用于指示哪些主机是该自动化的目标,是否使用特权升级系统来运行这些任务以及可选部分来定义变量或包含文件。 Ansible按顺序执行任务,完整的剧本执行称为play 。 剧本以YAML格式编写。
处理程序 (Handlers)
Handlers are used to perform actions on a service, such as restarting or stopping a service that is actively running on the managed node’s system. Handlers are typically triggered by tasks, and their execution happens at the end of a play, after all tasks are finished. This way, if more than one task triggers a restart to a service, for instance, the service will only be restarted once and after all tasks are executed. Although the default handler behavior is more efficient and overall a better practice, it is also possible to force immediate handler execution if that is required by a task.
处理程序用于对服务执行操作,例如重新启动或停止在受管节点系统上活动运行的服务。 处理程序通常由任务触发,它们的执行发生在所有任务完成后的播放结束时。 这样,例如,如果一个以上的任务触发了服务的重新启动,则该服务将仅在执行所有任务后一次重新启动。 尽管默认处理程序行为更有效并且总体上是更好的做法,但是如果任务需要立即执行处理程序,则也可以强制执行。
的角色 (Roles)
A role is a set of playbooks and related files organized into a predefined structure that is known by Ansible. Roles facilitate reusing and repurposing playbooks into shareable packages of granular automation for specific goals, such as installing a web server, installing a PHP environment, or setting up a MySQL server.
角色是一组剧本和相关文件,它们被组织成Ansible已知的预定义结构。 角色有助于将剧本重用和重新分配到可粒度共享的自动化软件包中,以实现特定目标,例如安装Web服务器,安装PHP环境或设置MySQL服务器。
结论 (Conclusion)
Ansible is a minimalist IT automation tool that has a gentle learning curve, thanks in part to its use of YAML for its provisioning scripts. It has a great number of built-in modules that can be used to abstract tasks such as installing packages and working with templates. Its simplified infrastructure requirements and accessible syntax can be a good fit for those who are getting started with configuration management.
Ansible是一种极简主义的IT自动化工具,具有柔和的学习曲线,这在一定程度上要归功于它在配置脚本中使用了YAML。 它具有大量的内置模块,可用于抽象任务,例如安装软件包和使用模板。 其简化的基础架构要求和可访问的语法非常适合刚开始进行配置管理的人员。
In the next part of this series, we’ll see how to install and get started with Ansible on an Ubuntu 20.04 server.
在本系列的下一部分中,我们将了解如何在Ubuntu 20.04服务器上安装和开始使用Ansible 。
配置管理 ansible
更多推荐


所有评论(0)