How to get current role name in an ansible task

本小妞迷上赌 提交于 2019-12-20 10:01:11

问题


How can I get the current role name in an ansible task yaml file?

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


回答1:


As of Ansible 2.2:

{{role_name}}

As of 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:

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) Pass a parameter to your role that has the name

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



回答2:


The simplest way is to just use the following

{{role_path|basename}}



回答3:


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 }}"


来源:https://stackoverflow.com/questions/25324261/how-to-get-current-role-name-in-an-ansible-task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!