我应该在哪里组织主机特定的文件/模板? [英] Where should I be organizing host-specific files/templates?
问题描述
我正在尝试根据目录布局结构.该文档似乎没有针对主机特定文件/模板的建议.
I'm trying to organize my playbooks according to the Directory Layout structure. The documentation doesn't seem to have a recommendation for host-specific files/templates.
我在一个站点上有2个剧本
I have 2 plays for a single site
- example.com-provision.yml
- example.com-deploy.yml
这些文件位于我的结构的根目录中.供应手册仅包含其他角色
These files are located in the root of my structure. The provisioning playbook simply includes other roles
---
- hosts: example.com
roles:
- common
- application
- database
become: true
become_method: su
become_user: root
部署手册不包含角色,但具有自己的vars
和tasks
部分.我有几个template
和copy
任务,并且想知道将这些特定于主机的模板/文件放在此目录结构中的最佳方法是什么.
The deployment playbook doesn't include roles, but has it's own vars
and tasks
sections. I have a couple template
and copy
tasks, and am wondering what the 'best practice' is for where to put these host-specific templates/files within this directory structure.
现在我在./roles/example.com/templates/
和./roles/example.com/files/
上有它们,但是需要从我的部署手册中引用文件的完整路径,例如
Right now I have them at ./roles/example.com/templates/
and ./roles/example.com/files/
, but need to reference the files with their full path from my deployment playbook, like
- name: deployment | copy httpd config
template:
src: ./roles/example.com/templates/{{ host }}.conf.j2
# ...
代替
- name: deployment | copy httpd config
template:
src: {{ host }}.conf.j2
# ...
推荐答案
面对同一问题,最简洁的方法对我来说似乎是以下结构:
Facing the same problem the cleanest way seems for me the following structure:
在顶级目录(与剧本相同的目录)中,我有一个files
文件夹(如果需要,还有一个templates
文件夹).在files
文件夹中,每个主机都有一个自己的文件文件夹,该文件夹的名称与清单中的主机名相同.
(请参见以下结构:myhost1
myhost2
)
In the top-level directory (same level as playbooks) I have a files
folder (and if I needed also a templates
folder). In the files
folder there is a folder for every host with it's own files where the folder's name is the same as the host name in inventory.
(see the structure below: myhost1
myhost2
)
.
├── files
│ ├── common
│ ├── myhost1
│ ├── myhost2
|
├── inventory
│ ├── group_vars
│ └── host_vars
├── roles
│ ├── first_role
│ └── second_role
└── my_playbook.yml
现在,您以任何角色都可以相对访问带有文件模块的文件:
Now in any role you can access the files with files modules relatively:
# ./roles/first_role/main.yml
- name: Copy any host based file
copy:
src={{ inventory_hostname }}/file1
dest= /tmp
说明:
魔术变量inventory_hostname
用于获取主机,请参见
The magic variable inventory_hostname
is to get the host, see here
The any file
module (as for example copy
) looks up the files
directory in the respective role directory and the files
directory in the same level as the calling playbook. Of course same applies to templates (but if you have different templates for the same role you should reconsider your design)
通常,主机特定文件不属于角色,而是位于某个外部位置(例如host_vars
).
Semantically a host specific file does not belong into a role, but somewhere outside (like host_vars
).
这篇关于我应该在哪里组织主机特定的文件/模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!