Ansible文件夹结构

Ansible folder structure

我来自使用 Vagrant 的 Puppet 背景,在理解 Ansible 及其差异方面遇到了一些困难。

我的 Puppet 结构如下所示:

puppet
├── servers
│   └── Backend
│       └── Vagrantfile
└── src
    ├── manifests
    │   └── nodes
    │       └── development
    │           └── backend.pp
    └── modules
        └── mysql

设置很简单,cd 到 Vagrantfile 并使用 Vagrant 启动虚拟机。

现在这是我的 Ansible 文件夹结构初稿:

ansible
├── servers
│   └── Backend
│       ├── Vagrantfile
│       └── ansible.cfg
└── sources
    ├── backend.yml
    ├── site.yml
    ├── inventories
    │   └── development
    │       ├── group_vars
    │       │   ├── all
    │       │   └── backend
    │       └── hosts
    ├── playbooks
    └── roles
        └── mysql

现在有以下问题:

来源:

http://leucos.github.io/ansible-files-layout/

https://github.com/ansible/ansible-examples

https://github.com/enginyoyen/ansible-best-practises

您找到了几个推荐布局的示例,但没有找到 the official one。这应该有望回答您的许多问题,但我也会尝试在这里解决这些问题。

Is this best practise for Ansible or too close to Puppet?

这绝对不是 Ansible 的推荐布局。在最佳实践布局中,没有 serverssources - 这些内部的内容都位于顶层(无论如何,"servers" 是什么意思?)。

Is it correct to treat backend.yml like a Puppet node manifest?

我对 Puppet 不熟悉,所以无法回答这个问题。

Where should I put site.yml and backend.yml? This example has them in the main directory while here it's in the 'plays' directory. What's the difference?

官方建议将所有剧本分散在根目录中。但是,这有点混乱,所以有些人将它们放在子目录中(在您的示例中,plays)。这相当有效,但您需要相应地调整剧本中的路径。

I think my group_vars in group_vars/backend aren't being used correctly, how do I access them?

您不应将它们放在库存文件夹的子目录中,因为它们不是库存的一部分。有很多地方可以定义变量,您应该阅读 the documentation 以了解它们是什么以及何时应该使用它们,但是 group_vars 应该与其他所有内容一起位于目录的根目录下。

就我而言,我根据环境复杂性使用以下结构(检查 directory-layout):

环境简单

I use this structure when there is one environment or when I use playbooks in provision mode

ansible
├── inventory
│   ├── hosts
│   └── group_vars
│       └── my_group.yml
├── roles
│   └── mysql
├── ansible.cfg
├── README.md
├── playbook1.yml
└── playbook2.yml

ansible.cfg 中,我在 [default] 中使用变量 inventory = ./inventory 以避免使用命令 ansible-*.

设置库存路径

Medium/Complex环境

I use this structure when there are more than one environment

ansible
├── inventories
│   ├── production
│   │   ├── hosts
│   │   └── group_vars
│   │       └── my_group.yml
│   └── development
│       ├── hosts
│       └── group_vars
│           └── my_group.yml
├── playbooks
│   ├── playbook1
│   │   ├── group_vars
│   │   │   └── my_group.yml
│   │   ├── roles
│   │   │   └── mysql
│   │   ├── README.md
│   │   └── site.yml
│   ...
├── README.md
└── ansible.cfg

在这种情况下,./inventories 中的每个环境都有一个文件夹。

我也更喜欢为每个剧本使用特定的文件夹,以便能够轻松地使用 variable precedence section 中定义的剧本级别的文件夹 group_vars。随着环境变得越来越复杂,变数也越来越多。剧本中的 group_vars(和 host_vars)允许为所有环境定义通用变量,从而减少库存变量。

终极关卡环境

我已经使用 Ansible 对超过 5000 台服务器的系统进行寻址,这里有一些针对更复杂环境的提示:

拆分清单文件

使用多个文件而不是单个 hosts 文件来定义库存服务器。在这种情况下 hosts 文件仅包含服务器名称,其他文件包含具有不同视角的组:

└── production
    ├── hosts
    ├── middleware
    └── trigram
  • middleware:映射到使用的中间件或其他 stuf 的组。例如,我使用此文件将服务器映射到 tomcatjavapostgresql 等。例如,我将它用于部署监控代理的剧本:如何检索来自 tomcatjavapostgresql 等的指标、日志
  • trigram:在我的项目中,我通常使用固定长度(3 或 4)的代码来标识我的业务组件(例如:'STK' 用于库存管理)然后我创建一个组文件将业务组件映射到我的服务器(哪些服务器用于部署 'STK')

创建新剧本时,选择您的视角来应对不同的环境。

警告:我认为 ansible 加载文件按字母顺序命名,你不能定义一个引用尚未加载的组的组。

使用文件夹 group_vars

group_vars 中,您可以使用包含子文件的文件夹来代替文件:

└── production
    └── group_vars
        └── my_group
            ├── vars1.yml
            └── vars2.yml

这对于拆分大文件很有用,或者如果您有生成变量的工具,在这种情况下,您在 git 下有 vars1.yml 并生成 vars2.yml

拆分 git 回购

当你为大型系统使用 ansible 时,有很多提交并且经常出现一个问题:如何拆分我的大型 git 存储库?

就我而言,我为 ./inventories 中的每个文件夹使用一个 git 存储库,具有不同的访问规则。 ./playbooks 中的每个文件夹的 git 存储库也具有不同的访问规则。