本文介绍了在哪里将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

然后,让我们说

  • 角色1取决于角色2和角色3
  • 角色2取决于角色4和角色5

通常,ansible星系具有以下结构:

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文件放在角色1的什么位置?

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/

而且,我可以对role1执行此操作,但是如何对role2自动执行此操作?后续的依赖关系是否需要通过这种方式解决并手动安装,还是有更好的选择?

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?

推荐答案

从技术上讲,只要您在ansible-galaxy install命令中反映正确的路径,就可以将requirements.yml文件放在任意位置.

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文件放入<project-top-level-directory>/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中代表角色2

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-versionned-role1
|    └─── locally-versionned-role2
|    └─── ...
|    └─── requirements.yml
|    └─── .gitignore
└─── ansible.cfg
└─── playbook1.yml
└─── playbook2.yml

ansible.cfg

我通过设置roles_path = roles强制在本地roles目录中搜索和下载角色,因此用户可以在不使用-p参数的情况下使用ansible-galaxy install.

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 commit/push,则可以使用ansible-galaxy install -g -f -r roles/requirements

Already discussed above. Just list dependencies to top-level external (i.e. not versionned 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-versionned-role*/


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

10-28 03:25