I am new to Ansible and Jinja2. Any help would be appreciated!
Ansible does not recognize Jinja templates located in another role when the including role is specified by absolute path.
Directory structure .
├── files
│ └── test_2.yml
├── hosts
├── roles
│ ├── common_role
│ │ ├── tasks
│ │ │ └── main.yml
│ │ ├── templates
│ │ │ └── common.j2
│ │ └── vars
│ │ └── main.yml
│ └── role_A
│ ├── tasks
│ │ └── main.yml
│ └── templates
│ ├── mytemplate_2.j2
│ └── mytemplate.j2
└── site.yml
site.yml
- hosts: all
connection: local
gather_facts: no
roles:
- common_role
- role_A
role_A/tasks/main.yml
- name: test relative path
template:
src: mytemplate.j2
dest: "{{playbook_dir}}/files/test_1.yml"
- name: test absolute path
template:
src: mytemplate_2.j2
dest: "{{playbook_dir}}/files/test_2.yml"
role_A/templates/mytemplate.j2
{% include 'roles/common_role/templates/common.j2' %}
msg: I am mytemplate.j2 and including {{common_templates}}.
role_A/templates/mytemplate_2.j2
{% include playbook_dir + 'roles/common_role/templates/common.j2' %}
msg: I am mytemplate_2.j2 and including {{common_templates}}.
tsz@mylaptop:/mnt/c/Users/tsz/jinja-test$ ansible-playbook -i hosts site.yml
PLAY [all] **************************************************************
TASK [role_A : test relative path] ***************************************
changed: [localhost]
TASK [role_A : test absolute path] ***************************************
fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "msg": "TemplateNotFound: /mnt/c/Users/tsz/jinja-test/roles/common_role/templates/common.j2"}
to retry, use: --limit @/mnt/c/Users/tsz/jinja-test/site.retry
PLAY RECAP ***************************************************************
localhost : ok=1 changed=1 unreachable=0 failed=1
tsz@mylaptop:/mnt/c/Users/tsz/jinja-test$ ls -al roles/common_role/templates
total 13
drwxrwxrwx 0 root root 4096 Nov 10 15:56 .
drwxrwxrwx 0 root root 4096 Nov 10 16:04 ..
-rwxrwxrwx 1 root root 23 Nov 10 15:57 common.j2
tsz@mylaptop:/mnt/c/Users/tsz/jinja-test$ ls -al roles/role_A/templates
total 129
drwxrwxrwx 0 root root 4096 Nov 10 16:00 .
drwxrwxrwx 0 root root 4096 Nov 10 15:59 ..
-rwxrwxrwx 1 root root 133 Nov 10 16:40 mytemplate_2.j2
-rwxrwxrwx 1 root root 115 Nov 10 16:11 mytemplate.j2
I think this isn't going to work with the current version of jinja. My suspicion is that you can't use literal paths because they include directories outside the role space. c.f. this thread.
Someone please prove me wrong. I'm out of time to test it, but it could easily be an artifact of the odd environment setup I have to use.
The template:
$: cat roles/example/templates/tst
stuff etc
{% include '/full/path/redacted/roles/example/templates/other' %}
The included template exists:
$ ls -l /full/path/redacted/roles/example/templates/other
-rw-r--r-- 1 jenkins jenkins 180 Nov 10 10:06 /full/path/redacted/roles/example/templates/other
The output:
TemplateNotFound: /full/path/redacted/roles/example/templates/other
Sorry I couldn't be more help.
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