How to get current role name in an ansible task

前端 未结 4 1439
刺人心
刺人心 2021-02-01 16:05

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         


        
相关标签:
4条回答
  • 2021-02-01 16:55

    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 }}"
    
    0 讨论(0)
  • 2021-02-01 17:05

    As of Ansible 2.8 there is ansible_play_name which contains the name of the currently executed play.

    • https://github.com/ansible/ansible/pull/48562
    • https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html
    0 讨论(0)
  • 2021-02-01 17:06

    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
    
    0 讨论(0)
  • 2021-02-01 17:07

    The simplest way is to just use the following

    {{role_path|basename}}
    
    0 讨论(0)
提交回复
热议问题