Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Given a playbook like this:

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

Is there a way to reference the playbook's name ("Tasks for service XYZ")? (i.e. a variable)


EDIT:

My intention is to be able to reference the playbook's name in a role task, i.e. sending a msg via slack like

- 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
like image 614
Diego Shevek Avatar asked Sep 13 '18 11:09

Diego Shevek


3 Answers

It was added in 2.8:

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

like image 53
alexb Avatar answered Oct 02 '22 13:10

alexb


No, the special variables for Ansible are documented here, and you can see that there is no variable to return the playbook name.

As mentioned in the comments, however, you can always do this:

---
- name: "{{ task_name }}"
  hosts: localhost
  vars:
    task_name: "Tasks for service XYZ"
  tasks:
    - debug:
        msg: "{{ task_name }}"
like image 25
Alex Harvey Avatar answered Oct 02 '22 12:10

Alex Harvey


From your circumstances, it looks like you only want this for audit/notification purposes? In that case (and assuming unixy clients), using

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

will give you the entire command line that ansible-playbook was called with, parameters and all, which would include the playbook name. Depending on your circumstances, that might be a useful enough tradeoff.

like image 28
Ulrich Schwarz Avatar answered Oct 02 '22 13:10

Ulrich Schwarz