Ansible:剧本在角色目录中的目录中调用角色 [英] Ansible: playbook calling Role in a directory that is in the roles directory
问题描述
我想塑造我的 ansible 角色和剧本的目录结构.
目前我有一个类似的目录结构.
group_vars* 全部* 一组- 组-vars.yml- 组 vault.yml...主机变量- server1.yml戏剧- java_plays* deploy_fun_java_stuff.yml* deploy_playbook.yml角色- 角色 1- 任务* main.yml- 处理程序-(其余所需目录)- 角色2- 爪哇- java_role1- 任务* main.yml- 处理程序-(其余所需目录)
我希望能够调用deploy_fun_java_stuff.yml
java_role1
我可以打电话
---- 名称:部署有趣的 Java 东西主机:java角色:- { 角色:role1 }
但我不能打电话(我试过多种方法).这可能吗?
<块引用><小时> - 名称:部署有趣的 java 东西主机:java角色:- { 角色:java/java_role1 }
我真正想要完成的是能够按照我的角色有序地组织我的剧本.我最终会得到大量的角色和戏剧,我想组织它们.
我可以为每个播放目录使用单独的 ansible.cfg 文件来处理这个问题,但我无法将这些 cfg 文件添加到 ansible Tower(所以我正在寻找替代解决方案).
我觉得问题是你需要正确设置相对路径.Ansible 首先应用相对于被调用的 playbooks 目录的给定路径,然后查看当前工作路径(您正在执行 ansible-playbook
命令),最后检查 /etc/ansible/roles
,因此在您的目录结构中,您可以使用 { role: ../../roles/java/java_role1 }<而不是
{ role: java/java_role1 }
/code> 或 { 角色:roles/java/java_role1 }
.另一种选择是配置 ansible 寻找角色的路径.为此,您可以在项目 ansible.cfg
中设置 roles_path
,如 Ansible 文档.
基于您的示例:
目录树:
ansible/├── 主机│ └── 开发├── 播放│ └── java_plays│ └── java.yml└── 角色├── java│ └── java_role1│ └── 任务│ └── main.yml└── 角色1└── 任务└── main.yml
为了测试它,该剧将包括java_role1
和role1
.
plays/java_plays/java.yml:
---- 名称:部署 java 的东西主机:java角色:- { 角色:角色/角色 1 }- {角色:角色/java/java_role1}
出于测试目的,这些角色只需打印调试信息.
role1/tasks/main.yml:
---- 调试:味精=内部角色1"
dev
主机文件只是将 localhost 设置为 java
组.现在我可以使用剧本了:
fishi@zeus:~/workspace/ansible$ ansible-playbook -i hosts/dev play/java_plays/java.ymlPLAY [部署java东西] ***********************************************************任务 [设置] ************************************************************************好的:[本地主机]任务 [role1 : debug] ***************************************************好的:[本地主机] =>{"msg": "内部角色 1"}任务 [java_role1 : 调试] *****************************************好的:[本地主机] =>{"msg": "java_role1 内部"}播放回顾 ****************************************************************************本地主机:ok=3 更改=0 无法访问=0 失败=0
现在使用 { role: ../../roles/java/java_role1 }
和 { role: ../../roles/role1 }
TASK
括号内的日志输出将显示整个相对路径,而不仅仅是角色名称:
fishi@zeus:~/workspace/ansible$ ansible-playbook -i hosts/dev play/java_plays/java.ymlPLAY [部署java东西] ***********************************************************任务 [设置] ************************************************************************好的:[本地主机]任务 [../../roles/role1 : 调试] ***********************************************好的:[本地主机] =>{"msg": "内部角色 1"}任务 [../../roles/java/java_role1 : 调试] ***************************************好的:[本地主机] =>{"msg": "java_role1 内部"}播放回顾 ****************************************************************************本地主机:ok=3 更改=0 无法访问=0 失败=0
I would like to shape my directory structure of my ansible roles and playbooks.
Currently I have a directory structure like.
group_vars
* all
* group-one
- group-vars.yml
- group-vault.yml
...
host_vars
- server1.yml
plays
- java_plays
* deploy_fun_java_stuff.yml
* deploy_playbook.yml
roles
- role1
- tasks
* main.yml
- handlers
- (the rest of the needed directories)
- role2
- java
- java_role1
- tasks
* main.yml
- handlers
- (the rest of the needed directories)
I would like to be able to call upon the role java_role1
in the play deploy_fun_java_stuff.yml
I can call
---
- name: deploy fun java stuff
hosts: java
roles:
- { role: role1 }
but I cannot call (I've tried multiple ways). Is this possible?
- name: deploy fun java stuff hosts: java roles: - { role: java/java_role1 }
What I really want to accomplish is to be able to structure my plays in an orderly fashion along with my roles. I will end up with a large number of both roles and plays I would like to organize them.
I can handle this with a separate ansible.cfg file for each play directory but I cannot add those cfg files to ansible tower (So I'm looking for an alternate solution).
I think the problem is that you need to set the relative path properly. Ansible first applies the given path relative to the called playbooks directory, then looks in the current working path (from which you are executing the ansible-playbook
command) and finally checks in /etc/ansible/roles
, so instead of { role: java/java_role1 }
in your dir structure you could use { role: ../../roles/java/java_role1 }
or { role: roles/java/java_role1 }
. Yet another option would be to configure the paths in which ansible is looking for roles. For that you could set the roles_path
inside your projects ansible.cfg
as described in the Ansible docs.
Based on your example:
Dir tree:
ansible/
├── hosts
│ └── dev
├── plays
│ └── java_plays
│ └── java.yml
└── roles
├── java
│ └── java_role1
│ └── tasks
│ └── main.yml
└── role1
└── tasks
└── main.yml
To test it, the play would include java_role1
and role1
.
plays/java_plays/java.yml:
---
- name: deploy java stuff
hosts: java
roles:
- { role: roles/role1 }
- { role: roles/java/java_role1 }
For testing purposes these roles simply print a debug msg.
role1/tasks/main.yml:
---
- debug: msg="Inside role1"
The dev
hosts file simply sets localhost to the java
group. Now I can use the playbook:
fishi@zeus:~/workspace/ansible$ ansible-playbook -i hosts/dev plays/java_plays/java.yml
PLAY [deploy java stuff] *******************************************************
TASK [setup] *******************************************************************
ok: [localhost]
TASK [role1 : debug] ***********************************************
ok: [localhost] => {
"msg": "Inside role1"
}
TASK [java_role1 : debug] *************************************
ok: [localhost] => {
"msg": "Inside java_role1"
}
PLAY RECAP *********************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0
Now doing the same when you use { role: ../../roles/java/java_role1 }
and { role: ../../roles/role1 }
your log output inside the TASK
brackets would show the whole relative path instead of just the role name:
fishi@zeus:~/workspace/ansible$ ansible-playbook -i hosts/dev plays/java_plays/java.yml
PLAY [deploy java stuff] *******************************************************
TASK [setup] *******************************************************************
ok: [localhost]
TASK [../../roles/role1 : debug] ***********************************************
ok: [localhost] => {
"msg": "Inside role1"
}
TASK [../../roles/java/java_role1 : debug] *************************************
ok: [localhost] => {
"msg": "Inside java_role1"
}
PLAY RECAP *********************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0
这篇关于Ansible:剧本在角色目录中的目录中调用角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!