问题描述
根据 Ansible 文档,手册是:
...一个非常简单的配置管理和多机部署系统的基础,不同于现有的任何系统,并且非常适合部署复杂的应用程序.
再一次,根据这些相同的文档,角色是:
...基于已知文件结构自动加载某些 vars_files、任务和处理程序的方法.按角色对内容进行分组还可以轻松地与其他用户共享角色.
然而,这些和它们不同用例之间的区别对我来说并不是很明显.例如,如果我将 /etc/ansible/hosts
文件配置为如下所示:
[数据库]mydb01.example.orgmydb02.example.org[邮件服务器]mymail01.example.orgmymail_dr.example.org
...那么这个[databases]
"是什么?条目...角色?或者某个地方的剧本 YAML 文件的名称?还是别的?!?
如果有人能向我解释这些差异,我对 Ansible 的理解将大大提高!
- Playbook vs Role vs
[databases]
以及/etc/ansible/hosts
中的类似条目 - 如果 Playbook 是在 YAML 文件中定义的,那么角色是在哪里定义的?
- 除了位于 Ansible 服务器上的
ansible.cfg
之外,我如何添加/配置 Ansible 和可用的 Playbooks/Roles?例如,当我运行ansible-playbook someplaybook.yaml
时,Ansible 如何知道在哪里可以找到该剧本?
[databases]
是一组主机的单一名称.它允许您通过一个名称引用多个主机.
角色是一组任务和附加文件,用于配置主机以服务于某个角色.
Playbook 是主机和角色之间的映射.
来自文档的示例描述了示例项目.它包含两件事:
- 剧本.
site.yml
、webservers.yml
、fooservers.yml
是剧本. - 角色:
roles/common/
和roles/webservers/
包含相应的common
和webservers
角色定义.
在剧本 (webservers.yml
) 中,你有类似的东西:
---- hosts: webservers <- 这组主机定义在/etc/ansible/hosts、databases 和 mail_servers 中,例如您的问题角色:<- 这是分配给这些主机的角色列表- 常见的- 网络服务器
如果 Playbook 是在 YAML 文件中定义的,那么角色是在哪里定义的?
它们在 roles/*
目录中定义.角色主要使用 YAML 文件定义,但也可以包含任何类型的资源(files/
、templates/
).根据 documentation 角色定义的结构如下:
- 如果roles/x/tasks/main.yml存在,其中列出的任务将被添加到play中
- 如果roles/x/handlers/main.yml 存在,其中列出的处理程序将被添加到播放中
- 如果roles/x/vars/main.yml存在,其中列出的变量将被添加到play中
- 如果roles/x/meta/main.yml 存在,则其中列出的任何角色依赖项都将添加到角色列表中(1.3 及更高版本)
- 任何复制任务都可以引用roles/x/files/中的文件,而无需相对或绝对路径
- 任何脚本任务都可以引用roles/x/files/中的脚本,而无需相对或绝对路径
- 任何模板任务都可以引用roles/x/templates/中的文件,而无需相对或绝对路径
- 任何包含任务都可以引用roles/x/tasks/中的文件,而无需相对或绝对路径
最重要的文件是roles/x/tasks/main.yml
,这里定义了任务,当角色被执行时会被执行.
除了 Ansible 服务器上的 ansible.cfg,我如何添加/配置 Ansible 和可用的 Playbooks/Roles?例如,当我运行 ansible-playbook someplaybook.yaml 时,Ansible 如何知道在哪里可以找到该剧本?
$ ansible-playbook someplaybook.yaml
将在当前目录中查找剧本.
$ ansible-playbook somedir/somedir/someplaybook.yaml
将在 somedir/somedir/
目录中查找剧本.
您有责任将包含所有剧本和角色的项目放在服务器上.Ansible 与此无关.
According to the Ansible docs, a Playbookis:
And, again, according to those same docs, a Roleare:
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
...then what is this "[databases]
" entry...a role? Or the name of a playbook YAML file somewhere? Or something else?!?
If someone could explain to me the differences on these, my understanding of Ansible would be greatly enhance!
- 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 runansible-playbook someplaybook.yaml
, how does Ansible know where to find that playbook?
[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 is a mapping between hosts and roles.
Example from documentation describes example project. It contains two things:
- Playbooks.
site.yml
,webservers.yml
,fooservers.yml
are playbooks. - Roles:
roles/common/
androles/webservers/
contain definitions ofcommon
andwebservers
roles accordingly.
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
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:
The most important file is roles/x/tasks/main.yml
, here you define tasks, which will be executed, when role is executed.
$ ansible-playbook someplaybook.yaml
Will look for a playbook inside current directory.
$ ansible-playbook somedir/somedir/someplaybook.yaml
Will look for a playbook inside somedir/somedir/
directory.
It's your responsibility to put your project with all playbooks and roles on server. Ansible has nothing to do with that.
这篇关于Ansible 剧本与角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!