在哪里放置用于 Ansible 的 requirements.yml 并使用它来解决依赖关系? [英] Where to place requirements.yml for Ansible and use it to resolve dependencies?

查看:29
本文介绍了在哪里放置用于 Ansible 的 requirements.yml 并使用它来解决依赖关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 ansible 的新手,正在探索依赖角色.文档链接

I am new to ansible and was exploring dependent roles. documentation link

我没有看到文档是 - 在哪里放置 requirements.yml 文件.

What I did not come across the documentation was- where to place the requirements.yml file.

例如,如果我的 site.yml 看起来像这样:

For instance, if my site.yml looks like this:

---
- name: prepare system
  hosts: all
  roles:
     - role1

而且,让我们说

  • role1 依赖于 role2 和 role3
  • role2 依赖于 role4 和 role5

通常,ansible-galaxy 具有以下结构:

Typically, ansible-galaxy have the following structure:

└── test-role
    ├── defaults
    │   └── main.yml
    ├── files
    ├── handlers
    │   └── main.yml
    ├── meta
    │   └── main.yml
    ├── README.md
    ├── tasks
    │   └── main.yml
    ├── templates
    ├── tests
    │   ├── inventory
    │   └── test.yml
    └── vars
        └── main.yml

依赖项,被添加到 meta/main.yml.假设,role1 在此文件中标记了依赖项,例如(对于 role2 也是如此):

Dependencies, are added to meta/main.yml. Assuming, role1 has dependencies marked in this file like (and likewise for role2):

dependencies: 
  - role: role2
  - role: role3

而且,我还有一个 requirements.yml 文件,它看起来像:

And, I also have a requirements.yml file which looks like:

---    
- src: some git link1
  version: master
  name: role2
- src: some git link2
  version: master
  name: role3

我的问题:我应该把这个 requirements.yml 文件放在 role1 的哪里?

My question: where do I place this requirements.yml file for role1?

我知道需要通过命令安装要求,

I understand the requirements will need to be installed by the command,

ansible-galaxy install -r requirements.yml -p roles/

而且,我可以为角色 1 执行此操作,但是如何为角色 2 实现自动化?后续的依赖是否需要手动解析安装,或者有什么更好的办法?

And, I can do this for role1, but how does this get automated for role2? Do the successive dependencies need to be resolved and installed manually this way, or is there something better?

推荐答案

从技术上讲,您可以将 requirements.yml 文件放在任何您喜欢的位置,只要您在 ansible-galaxy install 命令.

Technically speaking, you could put your requirements.yml file anywhere you like as long as you reflect the correct path in your ansible-galaxy install command.

与此同时,如果您想从 Ansible Tower/Awx 运行您的剧本,我建议您坚持使用 Ansible Tower 要求 并将您的 requirements.yml 文件放在 ;/roles/requirements.yml

Meanwhile, if you ever want to run your playbooks from Ansible Tower/Awx, I suggest you stick to the Ansible Tower requirements and put your requirements.yml file in <project-top-level-directory>/roles/requirements.yml

关于角色之间的依赖关系,ansible-galaxy 能够在安装过程中遇到它们时自行跟踪它们.所以你不需要在 requirements.yml 中指定所有这些,只需要顶级的.您只需要在每个外部角色中正确指定您的依赖项即可.

Regarding dependencies between roles, ansible-galaxy is able to follow them by itself when they are encountered during installation. So you don't need to specify all of them in your requirements.yml, only top level ones. You just need to specify your dependencies correctly in each external roles.

dependencies:
  - src: https://my.scm.com/my-ansible-roles/role2.git
    scm: git
    version: master
    name: role2
  - src: https://my.scm.com/my-ansible-roles/role3.git
    scm: git
    version: master
    name: role3

meta/main.yml中为role2

In meta/main.yml for role2

dependencies:
  - src: https://my.scm.com/my-ansible-roles/role4.git
    scm: git
    version: master
    name: role4
  - src: https://my.scm.com/my-ansible-roles/role5.git
    scm: git
    version: master
    name: role5

roles/requirements.yml

---    
- src: https://my.scm.com/my-ansible-roles/role1.git
  scm: git
  version: master
  name: role1

尽可能详尽,这就是我现在通常在我的项目中处理本地依赖项以及本地/仅项目角色

To be as exhaustive as possible, this is what I now usually do on my projects to handle dependencies locally as well as local/project only roles

ansible-project-dir
└─── roles
|    └─── locally-versioned-role1
|    └─── locally-versioned-role2
|    └─── ...
|    └─── requirements.yml
|    └─── .gitignore
└─── ansible.cfg
└─── playbook1.yml
└─── playbook2.yml

ansible.cfg

我通过设置roles_path = roles强制在本地roles目录中搜索和下载角色,所以用户可以使用ansible-galaxy install而无需-p 参数.

ansible.cfg

I force roles search and downloads in the local roles directory by setting roles_path = roles, so user can use ansible-galaxy install without the -p parameter.

上面已经讨论过了.只需列出对顶级外部(即项目中未进行版本控制)的依赖项作为星系角色名称或 git uris.如果您需要完全检出这些角色以便稍后对它们进行 git commits/push,您可以使用 ansible-galaxy install -g -f -r roles/requirements

Already discussed above. Just list dependencies to top-level external (i.e. not versioned in the project) as galaxy role name or as git uris. If you need to fully checkout those roles to later make git commits/push on them, you can use ansible-galaxy install -g -f -r roles/requirements

# Ignore everything in roles dir
/*
# Except:
# the .gitignore file
!.gitignore
# the requirements file
!requirements.yml
# Readme if you have one
!README.md
# and any specific role we want to version locally
!locally-versioned-role*/


这篇关于在哪里放置用于 Ansible 的 requirements.yml 并使用它来解决依赖关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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