使用 ansible 进行多阶段部署 [英] Multistage deployment with ansible

查看:36
本文介绍了使用 ansible 进行多阶段部署的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您有不同的阶段变量,您建议使用 Ansible 组织多阶段部署的方法是什么?

What approach would you advise to organize multistage deployment with Ansible in case you have different variables for stages?

主要思想是为不同阶段定义组变量.

The main idea is defining group variables for different stages.

有两篇文章:

我想获得更多有关组织剧本、变量和主机的示例,并了解您的方法的优缺点.

I'd like to get more examples about organizing playbooks, variables, and hosts, and understand advantages and disadvantages of your approach.

推荐答案

最近,我使用了我在问题中已经提到的方法,它是我觉得最方便的方法之一.

Recently, I used the approach I had already mentioned in the question, and it occurred to be one of the most convenient to my mind.

它取自 在 Ansible 中组织组变量文件 文章,但稍作改动,因为不幸的是,文章的标题并没有反映它的真正价值和目的,剧本的名称也令人困惑.事实上,我们花了很长时间才意识到这是关于使用 Ansible 进行多阶段部署.

It is taken from Organizing Group Vars Files in Ansible article, but altered a little, because, unfortunately, title of the article does not reflect it's real value and purpose and names of playbooks are confusing as well. In fact, it took a considerable time to realize that it is about Multistage deployment with Ansible.

您的目录布局应该是这样的:

production/
├── group_vars
│   └── server.yml
└── inventory
staging/
├── group_vars
│   └── server.yml
└── inventory
deploy.yml

而且使用非常简单:

ansible-playbook -i staging deploy.yml

其中 deploy.yml 是您的剧本的名称.

Where deploy.yml is the name of your playbook.

Ansible-playbook 当提供目录作为清单时,默认会搜索名为 inventory 的文件,因此无需指定 -i production/inventory,只需 -i production 可以正常工作.

Ansible-playbook when provided a directory as the inventory, will search by default a file named inventory so no need to specify -i production/inventory, only -i production will work just fine.

好处是:

  • 您不必维护一些不必要的组,例如 [production:children]

您不需要像 group_vars/production.yml

所有变量和主机都在不同的目录中,因此很容易使它们保持不同并且更改历史清晰.如果需要,您甚至可以将其拆分到单独的存储库中

All vars and hosts are in separate directories, therefore it is easy to keep them different and history of changes is clear. You may even split it in separate repositories if you want

您还可以使用 ansible-vault 在存储库中为生产保密,换句话说,将所有重要变量加密存储

You may also keep secrets for production in repository using ansible-vault, in other words, store all your vital variables encrypted

这篇关于使用 ansible 进行多阶段部署的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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