I am new to Ansible and Jinja2. Any help would be appreciated!
- Ansible version: 2.4.1
- Execution environment: Windows bash
- Test sample: https://bitbucket.org/tsz662/jinja-test
Problem
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}}.
Execution result
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
File permissions
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