Ansible 2.6:有没有办法在角色任务中引用剧本的名称?

Ansible 2.6: Is there a way to reference the playbook's name in a role task?

给定这样的剧本:

- name: "Tasks for service XYZ"
  hosts: apiservers
  roles:
    - { role: common }

有没有办法引用剧本的名称 ("Tasks for service XYZ")? (即一个变量)


编辑:

我的意图是能够在角色任务中引用 playbook 的名称,即通过 slack 发送消息,例如

- name: "Send Slack notification indicating deploy has started"
  slack:
    channel: '#project-deploy'
    token: '{{ slack_token }}'
    msg: '*Deploy started* to _{{ inventory_hostname }}_ of `{{ PLAYBOOK_NAME }}` version *{{ service_version }}*'
  delegate_to: localhost
  tags: deploy

不,Ansible 的特殊变量已记录 here,您可以看到 return 剧本名称没有变量。

但是,如评论中所述,您始终可以这样做:

---
- name: "{{ task_name }}"
  hosts: localhost
  vars:
    task_name: "Tasks for service XYZ"
  tasks:
    - debug:
        msg: "{{ task_name }}"

从您的情况来看,您似乎只是为了 audit/notification 目的才想要这个?在那种情况下(并假设 unixy 客户端),使用

lookup('file', '/proc/self/cmdline') | regex_replace('\u0000',' ')

将为您提供调用 ansible-playbook 时使用的整个命令行、参数和所有内容,其中包括剧本名称。根据您的情况,这可能是一个足够有用的权衡。

添加于2.8:

ansible_play_name
The name of the currently executed play. Added in 2.8.