Ansible剧本与角色 [英] Ansible Playbooks vs Roles

查看:45
本文介绍了Ansible剧本与角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据Ansible文档, 剧本 是:

According to the Ansible docs, a Playbook is:

...非常简单的配置管理和多机部署系统的基础,与现有的系统不同,它非常适合于部署复杂的应用程序.

...the basis for a really simple configuration management and multi-machine deployment system, unlike any that already exist, and one that is very well suited to deploying complex applications.

同样,根据这些相同的文档, 角色 是:

And, again, according to those same docs, a Role are:

...根据已知文件结构自动加载某些vars_files,任务和处理程序的方式.按角色对内容进行分组还可以轻松与其他用户共享角色.

...ways of automatically loading certain vars_files, tasks, and handlers based on a known file structure. Grouping content by roles also allows easy sharing of roles with other users.

但是,这些与它们的不同用例之间的区别对我而言并不是立即显而易见的.例如,如果我将我的/etc/ansible/hosts 文件配置为如下所示:

However the distinction between these and their different use cases is not immediately obvious to me. For instance, if I configure my /etc/ansible/hosts file to look like:

[databases]
mydb01.example.org
mydb02.example.org

[mail_servers]
mymail01.example.org
mymail_dr.example.org

...那么这是什么" [数据库] "条目... 角色?还是某个地方的剧本YAML文件的名称?还是其他???

...then what is this "[databases]" entry...a role? Or the name of a playbook YAML file somewhere? Or something else?!?

如果有人可以向我解释这些区别,那么我对Ansible的理解将大大增强!

If someone could explain to me the differences on these, my understanding of Ansible would be greatly enhance!

  • Playbook vs Role与 [databases] 以及/etc/ansible/hosts
  • 中的类似条目
  • 如果在YAML文件中定义了Playbook,那么角色在哪里定义?
  • 除了Ansible服务器上的 ansible.cfg 之外,如何使用可用的Playbooks/Roles添加/配置Ansible?例如,当我运行 ansible-playbook someplaybook.yaml 时,Ansible如何知道在哪里可以找到该剧本?
  • Playbook vs Role vs [databases] and similar entries in /etc/ansible/hosts
  • If Playbooks are defined inside of YAML files, then where are Roles defined?
  • Aside from the ansible.cfg living on the Ansible server, how do I add/configure Ansible with available Playbooks/Roles? For instance, when I run ansible-playbook someplaybook.yaml, how does Ansible know where to find that playbook?

推荐答案

Playbook vs Role与[databases]以及/etc/ansible/hosts中的类似条目

Playbook vs Role vs [databases] and similar entries in /etc/ansible/hosts

[数据库] 是一组主机的单个名称.它允许您通过一个名称引用多个主机.

[databases] is a single name for a group of hosts. It allows you to reference multiple hosts by a single name.

角色是一组任务和其他文件,用于配置主机以为特定的角色服务.

Role is a set of tasks and additional files to configure host to serve for a certain role.

Playbook是主机和角色之间的映射.

Playbook is a mapping between hosts and roles.

文档中的示例描述了示例项目.它包含两件事:

Example from documentation describes example project. It contains two things:

  • 剧本. site.yml webservers.yml fooservers.yml 是剧本.
  • 角色: roles/common/ roles/webservers/包含相应的 common webservers 角色定义
  • Playbooks. site.yml, webservers.yml, fooservers.yml are playbooks.
  • Roles: roles/common/ and roles/webservers/ contain definitions of common and webservers roles accordingly.

在内部剧本( webservers.yml )中,您会遇到类似的事情:

Inside playbook (webservers.yml) you have something like:

---
- hosts: webservers <- this group of hosts defined in /etc/ansible/hosts, databases and mail_servers in example from your question
  roles: <- this is list of roles to assign to these hosts
     - common
     - webservers

如果在YAML文件中定义了Playbook,那么角色在哪里定义?

If Playbooks are defined inside of YAML files, then where are Roles defined?

它们在 roles/* 目录中定义.角色主要是使用YAML文件定义的,但也可以包含任何类型的资源(文件/模板/).根据文档角色定义是这样构造的:

They are defined inside roles/* directories. Roles are defined mostly using YAML files, but can also contain resources of any types (files/, templates/). According to documentation role definition is structured this way:

  • 如果存在role/x/tasks/main.yml,则其中列出的任务将被添加到剧本中
  • 如果Roles/x/handlers/main.yml存在,则其中列出的处理程序将被添加到剧本中
  • 如果存在role/x/vars/main.yml,其中列出的变量将被添加到剧本中
  • 如果存在role/x/meta/main.yml,则其中列出的所有角色依赖项都将添加到角色列表(1.3及更高版本)中
  • 任何复制任务都可以引用role/x/files/中的文件,而不必相对或绝对地对其进行路径
  • 任何脚本任务都可以引用role/x/files/中的脚本,而不必相对或绝对地对其进行路径
  • 任何模板任务都可以引用role/x/templates/中的文件,而不必相对或绝对地对其进行路径
  • 任何包含任务都可以引用role/x/tasks/中的文件,而不必相对或绝对地对其进行路径

最重要的文件是 roles/x/tasks/main.yml ,您在此处定义任务,这些角色将在执行角色时执行.

The most important file is roles/x/tasks/main.yml, here you define tasks, which will be executed, when role is executed.

除了Ansible服务器上的ansible.cfg外,如何使用可用的Playbooks/Roles添加/配置Ansible?例如,当我运行ansible-playbook someplaybook.yaml时,Ansible如何知道在哪里可以找到该剧本?

Aside from the ansible.cfg living on the Ansible server, how do I add/configure Ansible with available Playbooks/Roles? For instance, when I run ansible-playbook someplaybook.yaml, how does Ansible know where to find that playbook?

$ ansible-playbook someplaybook.yaml

将在当前目录中查找一本剧本.

Will look for a playbook inside current directory.

$ ansible-playbook somedir/somedir/someplaybook.yaml

将在 somedir/somedir/目录中寻找一本剧本.

Will look for a playbook inside somedir/somedir/ directory.

将所有带有剧本和角色的项目放在服务器上是您的责任.Ansible与之无关.

It's your responsibility to put your project with all playbooks and roles on server. Ansible has nothing to do with that.

这篇关于Ansible剧本与角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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