【发布时间】:2017-11-11 00:21:45
【问题描述】:
我是 Ansible 和 Jinja2 的新手。任何帮助,将不胜感激!
- Ansible 版本:2.4.1
- 执行环境:Windows bash
- 测试样本:https://bitbucket.org/tsz662/jinja-test
问题
当包含角色由绝对路径指定时,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.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
【问题讨论】:
-
它是否适用于相对路径?您可以将文件符号链接到调用角色吗?向我们展示调用任务。
-
感谢您的回复。正如您所指出的,它适用于相对路径,但不适用于绝对路径。至于simlink,对不起,真正的项目在我们团队的私人仓库中。
-
检查 /playbook/roles/common_role/templates/common.j2 的每个目录下执行用户的权限。这是这两个错误的一致且可重复的系列吗?他们是不是一蹴而就,还是变化持续存在?
-
再次感谢您的回复!问题 #2 是我没有先加载 common_role 的愚蠢错误,因此我将其从问题中删除。
-
This thread 声明您不能在角色之外包含任何内容。我想知道这是不是让你挂了?您的完整路径包括明确高于角色的目录,即使端点不是。有没有人设法使用模板的完整路径包括? (仍在测试中)