问题描述
根据Ansible文档, 剧本 是:
According to the Ansible docs, a Playbookis:
同样,根据这些相同的文档, 角色 是:
And, again, according to those same docs, a Roleare:
但是,这些与它们的不同用例之间的区别对我而言并不是立即显而易见的.例如,如果我将我的/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 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是主机和角色之间的映射.
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/
androles/webservers/
contain definitions ofcommon
andwebservers
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
它们在 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:
最重要的文件是 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-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剧本与角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!