【问题标题】:Ansible role does not read templates in playbook dir or other role's templates dirAnsible 角色不读取剧本目录或其他角色的模板目录中的模板
【发布时间】:2017-11-11 00:21:45
【问题描述】:

我是 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.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 声明您不能在角色之外包含任何内容。我想知道这是不是让你挂了?您的完整路径包括明确高于角色的目录,即使端点不是。有没有人设法使用模板的完整路径包括? (仍在测试中)

标签: ansible jinja2


【解决方案1】:

我认为这不适用于当前版本的 jinja。 我的 怀疑 是您不能使用文字路径,因为它们包含角色空间之外的目录。 参考文献this thread.

请有人证明我错了。我没有时间测试它,但它很容易成为我必须使用的奇怪环境设置的产物。

模板:

$: 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

很抱歉,我无法提供更多帮助。

【讨论】:

  • 您的回复支持了我的怀疑,即我们现在不能对模板使用文字路径,我非常感谢您的努力和时间。
  • 对不起,这不是更实际的帮助。不过对我来说是一次很好的学习经历。干杯。
猜你喜欢
  • 2021-02-23
  • 2015-11-13
  • 1970-01-01
  • 1970-01-01
  • 2015-07-31
  • 2014-02-09
  • 2020-02-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多