我应该在哪里组织特定于主机的文件/模板? [英] Where should I be organizing host-specific files/templates?

查看:27
本文介绍了我应该在哪里组织特定于主机的文件/模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据 目录布局 结构.该文档似乎没有对特定于主机的文件/模板提出建议.

我在一个网站上有 2 个播放

  • example.com-provision.yml
  • example.com-deploy.yml

这些文件位于我的结构的根目录中.供应剧本只包括其他角色

---- 主机:example.com角色:- 常见的- 应用- 数据库变成:真的成为方法:su成为用户:root

部署手册不包括角色,但有自己的varstasks 部分.我有几个 templatecopy 任务,我想知道将这些特定于主机的模板/文件放置在此目录结构中的位置的最佳实践"是什么.

现在我在 ./roles/example.com/templates/./roles/example.com/files/ 有它们,但需要参考我的部署手册中带有完整路径的文件,例如

- 名称:部署 |复制httpd配置模板:src: ./roles/example.com/templates/{{ 主机 }}.conf.j2# ...

代替

- 名称:部署 |复制httpd配置模板:源代码:{{ 主机 }}.conf.j2# ...

解决方案

面对同样的问题,在我看来,最简洁的方法是以下结构:

在顶级目录(与剧本相同的级别)中,我有一个 files 文件夹(如果我还需要一个 templates 文件夹).在 files 文件夹中,每个主机都有一个文件夹,其中包含自己的文件,其中文件夹的名称与清单中的主机名相同.(参见下面的结构:myhost1 myhost2)

<预><代码>.├── 文件│ ├── 普通│ ├── myhost1│ ├── myhost2|├── 库存│ ├── group_vars│ └── host_vars├── 角色│ ├── first_role│ └── second_role└── my_playbook.yml

现在在任何角色中,您都可以相对地访问具有文件模块的文件:

# ./roles/first_role/main.yml- 名称:复制任何基于主机的文件复制:src={{inventory_hostname}}/file1目标=/tmp

说明:

魔术变量inventory_hostname是获取主机,见此处any file 模块(例如copy)在各自的角色目录中查找files目录files 目录与调用 playbook 处于同一级别.当然同样适用于模板(但如果你为同一个角色有不同的模板,你应该重新考虑你的设计)

从语义上讲,特定于主机的文件不属于角色,而是属于外部的某个地方(例如 host_vars).

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.

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

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.

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
    # ...

instead of

- name: deployment | copy httpd config
  template:
    src: {{ host }}.conf.j2
    # ...

解决方案

Facing the same problem the cleanest way seems for me the following structure:

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

Explanation:

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)

Semantically a host specific file does not belong into a role, but somewhere outside (like host_vars).

这篇关于我应该在哪里组织特定于主机的文件/模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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