Ansible:剧本在Roles目录中的目录中调用Role [英] Ansible: playbook calling Role in a directory that is in the roles directory
问题描述
我想根据自己的角色和剧本来调整目录结构.
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)
我希望能够在剧本deploy_fun_java_stuff.yml
我可以打电话
---
- 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.
我可以为每个播放目录使用单独的ansible.cfg文件来处理此问题,但无法将这些cfg文件添加到ansible塔中(因此,我正在寻找替代解决方案).
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).
推荐答案
我认为问题在于您需要正确设置相对路径. Ansible首先应用相对于被调用的playbooks目录的给定路径,然后查找当前的工作路径(从中执行ansible-playbook
命令),最后检入/etc/ansible/roles
,因此在目录结构中而不是{ role: java/java_role1 }
您可以使用{ role: ../../roles/java/java_role1 }
或{ role: roles/java/java_role1 }
.另一个选择是配置ansible在其中寻找角色的路径.为此,您可以按照 Ansible docs ansible.cfg中设置roles_path
. a>.
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.
根据您的示例:
D树:
ansible/
├── hosts
│ └── dev
├── plays
│ └── java_plays
│ └── java.yml
└── roles
├── java
│ └── java_role1
│ └── tasks
│ └── main.yml
└── role1
└── tasks
└── main.yml
要进行测试,该剧将包含java_role1
和role1
.
To test it, the play would include java_role1
and role1
.
播放/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.
角色1/任务/main.yml:
---
- debug: msg="Inside role1"
dev
主机文件仅将localhost设置为java
组.现在,我可以使用剧本了:
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
现在在使用{ role: ../../roles/java/java_role1 }
和{ role: ../../roles/role1 }
时执行相同的操作,在TASK
括号内的日志输出将显示整个相对路径,而不仅仅是角色名称:
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:剧本在Roles目录中的目录中调用Role的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!