Ansible 中的角色和任务(和标签)有什么区别? [英] What's the difference between roles and tasks (and tags) in Ansible?

查看:25
本文介绍了Ansible 中的角色和任务(和标签)有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现自己总是在角色任务之间感到困惑.

I'm finding myself getting confused between roles and tasks all the time.

我知道标签是标记单个项目的一种方式,但我对如何使用它们感到困惑.

I get that tags are a way to tag individual items, but I'm confused how I'd use them.

假设我必须执行以下操作

Let's say I had to do the following

Users
  Create a user named "deploy"
  Add ssh key for "deploy" user

Git
  Install git
  Clone some git repo

用户"和Git"会是我在顶级 YML 文件中的两个主要角色吗?每个子项(例如安装 Git")都是一项任务吗?我会用标签标记每个子任务吗?还是我用标签标记角色?

Would "Users" and "Git" be my two main roles in the top level YML file? Would each sub-item (e.g "Install Git") be a task? Would I tag each sub-task with a tag? Or do I tag roles with a tag?

只是寻找 Ansible 层次结构的概述.

Just looking for an overview of Ansible hierarchy.

推荐答案

tl;dr 任务是单个声明(操作);角色是对任务进行分组的多种方式之一.

tl;dr A task is a single declaration (operation); a role is one of many ways for grouping tasks.

  • Ansible 中的任务是一个基本的工作单元,类似于其他语言中的一行代码.

  • A task in Ansible is a basic unit of work, a kind of counterpart to a line of code in other languages.

一个任务被执行并有结果(ok、changed、failed).

A task is executed and has a result (ok, changed, failed).

在大多数情况下,任务调用处理幂等性的操作模块,使任务成为一个声明性的编程单元.

In most cases a task calls an action module which takes care of idempotence, making a task a declarative unit of programming.

但是,有些任务会执行元操作、包含和其他命令的包装器.

However there are tasks that perform meta actions, includes, and wrappers for other commands.

Ansible 中的角色是将一组任务(加上默认数据、模板、处理程序、文件)分组为更复杂的定义的众多抽象层之一.

A role in Ansible is one of many abstraction layers for grouping a set of tasks (plus default data, templates, handlers, files) into a more complex definitions.

角色隐藏实现细节(类似于其他语言中的函数、过程、方法)并允许轻松重用代码.

Roles hide implementation details (similarly to functions, procedures, methods in other languages) and allow easy reuse of code.

用户"和Git"会是我在顶级 YML 文件中的两个主要角色吗?

Would "Users" and "Git" be my two main roles in the top level YML file?

在 Ansible 中没有单一的组织方式.您可以使用不同的结构实现相同的结果:角色、包含、条件等.

There is no single way to organise things in Ansible. You can achieve the same result using different constructs: roles, includes, conditionals, etc.

您如何使用 Ansible 取决于您(和您的组织)的目标:您可以创建 Ansible 剧本,使其读起来就像一个完整的、包含的、线性的描述配置的文档;或者,您可以创建具有依赖项、抽象级别和模块化架构的复杂配置.

How you use Ansible depends on your (and your organisation's) objectives: you can create Ansible playbooks so that they read like a full, contained, linear documents describing the configuration; or you can create complex configurations with dependencies, abstraction levels, and modular architecture.

这篇关于Ansible 中的角色和任务(和标签)有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆