简体   繁体   English

如何在 ansible 任务中获取当前角色名称

[英]How to get current role name in an ansible task

How can I get the current role name in an ansible task yaml file?如何在 ansible 任务 yaml 文件中获取当前角色名称?

I would like to do something like this我想做这样的事情

---
# 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

The result of this task should be a directory /tmp/some-role-name on the server此任务的结果应该是服务器上的目录/tmp/some-role-name

最简单的方法是使用以下

{{role_path|basename}}

As of Ansible 2.2 :Ansible 2.2 开始

{{role_name}}

As of Ansible 2.1 :Ansible 2.1 开始

{{role_path|basename}}

Older versions:旧版本:

There is no way to do this in the current version of Ansible, here are a couple options that might work for you instead:在当前版本的 Ansible 中没有办法做到这一点,这里有几个可能适合你的选项:

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 1) 使用 set_fact 将 role_name var 设置为您的 tasks/main.yml 文件中的第一个任务的角色名称

- set_fact: role_name=some-role-name

2) Pass a parameter to your role that has the name 2)将参数传递给您的角色,该角色具有名称

- roles:
  - role: some-role-name
    role_name: some-role-name

See this post :看到这个帖子

To get the role directory:获取角色目录:

role_dir: "{{ lookup('pipe', 'pwd') | dirname }}"

To get the role name:获取角色名称:

role_name: "{{ lookup('pipe', 'pwd') | dirname | basename }}"

As of Ansible 2.8 there is ansible_play_name which contains the name of the currently executed play.从 Ansible 2.8 开始, ansible_play_name包含当前执行的播放名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM