Reputation: 2115
I have two playbooks pb1.yml
and dir/pb2.yml
. I am calling pb2.yml
from pb1.yml
playbook. I need to print the name of the directory which called the pb2.yml
playbook.
cat pb1.yml
---
- hosts: localhost
tasks:
- debug: var={{ playbook_dir |basename }}
- import_playbook: dir/pb2.yml
cat dir/pb2.yml
---
- hosts: localhost
tasks:
- debug: var={{ playbook_dir |basename }}
In the following example , I am able to get the directory name of pb2.yml
but I am interested to know the directory name of pb1.yml
from pb2.yml
.
home/monk>ansible-playbook pb1.yml
PLAY [localhost] *******************************************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************************
ok: [localhost]
TASK [debug] ***********************************************************************************************************************************************************
ok: [localhost] => {
"samples": "VARIABLE IS NOT DEFINED!"
}
PLAY [localhost] *******************************************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************************
ok: [localhost]
TASK [debug] ***********************************************************************************************************************************************************
ok: [localhost] => {
"dir": "VARIABLE IS NOT DEFINED!"
}
PLAY RECAP *************************************************************************************************************************************************************
localhost : ok=4 changed=0 unreachable=0 failed=0
Expected result:
from pb2.yml, print /home/monk/
Upvotes: 3
Views: 844
Reputation: 68084
Either the variable is properly expanded and the value printed with the parameter msg
- debug:
msg: "{{ playbook_dir|basename }}"
, or the variable is printed with the parameter var
without expansion
- debug:
var: playbook_dir|basename
The code {{ playbook_dir|basename }}
evaluates to "sample" or "dir" respectively
tasks:
- debug: var={{ playbook_dir|basename }}
Such variables are not defined and the play will fail
"samples": "VARIABLE IS NOT DEFINED!
Quoting from debug
var: A variable name to debug.Mutually exclusive with the msg option. Be aware that this option already runs in Jinja2 context and has an implicit {{ }} wrapping, so you should not be using Jinja2 delimiters unless you are looking for double interpolation.
"Double interpolation" is actually "indirect addressing". The play below
- hosts: localhost
vars:
x: y
y: z
tasks:
- debug:
var: "{{ x }}"
gives
ok: [localhost] => {
"y": "z"
}
Upvotes: 1