且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-30 10:39:46

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