如何在 ansible 任务中获取当前角色名称 [英] How to get current role name in an ansible task
问题描述
如何在 ansible 任务 yaml 文件中获取当前角色名称?
How can I get the current role name in an ansible task yaml file?
我想做这样的事情
---
# role/some-role-name/tasks/main.yml
- name: Create a directory which is called like the current role name
action: file
path=/tmp/"{{ role_name }}"
mode=0755
state=directory
这个任务的结果应该是服务器上的目录/tmp/some-role-name
The result of this task should be a directory /tmp/some-role-name
on the server
推荐答案
自 Ansible 2.2 起:
{{role_name}}
从 Ansible 2.1 开始:
{{role_path|basename}}
旧版本:
在当前版本的 Ansible 中没有办法做到这一点,这里有几个可能适合您的选项:
There is no way to do this in the current version of Ansible, here are a couple options that might work for you instead:
1) 使用 set_fact 将 role_name var 设置为您的 tasks/main.yml 文件中第一个任务的角色名称
1) Use set_fact to set a role_name var to the name the of role as the first task in your tasks/main.yml file
- set_fact: role_name=some-role-name
2) 将参数传递给您的角色,该角色具有名称
2) Pass a parameter to your role that has the name
- roles:
- role: some-role-name
role_name: some-role-name
这篇关于如何在 ansible 任务中获取当前角色名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!