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
What is pre_tasks in Ansible? pre_tasks is a task which Ansible executes before executing any tasks mentioned in . yml file. Consider this scenario. You provisioned a new instance on Amazon EC2 cloud or Google Cloud .
tl;dr A task is a single declaration (operation); a role is one of many ways for grouping tasks. A task in Ansible is a basic unit of work, a kind of counterpart to a line of code in other languages. A task is executed and has a result (ok, changed, failed).
The simplest way is to just use the following
{{role_path|basename}}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With