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

查看:83
本文介绍了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

在顶级YML文件中,用户"和"Git"是否将是我的两个主要角色?每个子项目(例如"Install 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.

任务已执行并具有结果(确定,更改,失败).

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.

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

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天全站免登陆