我刚接触过Ansible和Jinja2。任何帮助都将不胜感激!
问题
当包含角色由绝对路径指定时,Ansible不识别位于另一个角色中的Jinja模板。
目录结构
.
├── 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.ymlsite.yml
- hosts: all
connection: local
gather_facts: no
roles:
- common_role
- role_Arole_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发布于 2017-11-10 16:21:45
我认为这不适用于目前版本的jinja。我怀疑您不能使用文字路径,因为它们包含角色空间之外的目录。c.f.这条线。
谁来证明我错了。我没有时间测试它,但它很容易成为我不得不使用的奇怪环境设置的工件。
模板:
$: cat roles/example/templates/tst
stuff etc
{% include '/full/path/redacted/roles/example/templates/other' %}所包含的模板存在:
$ 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产出:
TemplateNotFound: /full/path/redacted/roles/example/templates/other抱歉我帮不上忙了。
https://stackoverflow.com/questions/47123373
复制相似问题