Ansible не может найти каталог шаблонов

#ansible #jinja2

#ansible #jinja2

Вопрос:

Я столкнулся с очень странной ошибкой при использовании Ansible. У меня есть базовый скрипт, использующий шаблоны jinja. Мой скрипт находится внутри каталога с именем junos. Я указал, что мои шаблоны будут расположены внутри каталога с именем templates Найти здесь скрипт:

     ---
- name : Load and commit the config
  hosts: access_sw
  vars:
    ansible_python_interpreter: /usr/bin/python3.6
  roles:
    - Juniper.junos
  connection: local
  gather_facts: no

  tasks:
    - name : "Load config from local file and commit"
      juniper_junos_config:
        load: "merge"
        template:
          src: "./templates/intf.j2"
          dest: "./build_conf/{{ inventory_hostname }}/intf.cfg"
        vars:
          host: "{{ inventory_hostname }}"
        format: "set"
      register: response
  

Мы видим, что файл существует:

 (prompt)--> ls ./templates/intf.j2
    ./templates/intf.j2
  

И ansible реагирует, говоря, что не может найти файл шаблона:

     ansible-playbook --connection local -vvv -i junos_hosts.yml junos_sendconfig.yml
ansible-playbook 2.10.3
  config file = None
  configured module search path = ['/home/jpbz7878/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/jpbz7878/.local/lib/python3.6/site-packages/ansible
  executable location = /usr/local/bin/ansible-playbook
  python version = 3.6.8 (default, Apr  2 2020, 13:34:55) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
No config file found; using defaults
host_list declined parsing /home/jpbz7878/Programming/Ansible/junos/junos_hosts.yml as it did not pass its verify_file() method
script declined parsing /home/jpbz7878/Programming/Ansible/junos/junos_hosts.yml as it did not pass its verify_file() method
Skipping empty key (Remote-PE) in group (remote_pe)
Parsed /home/jpbz7878/Programming/Ansible/junos/junos_hosts.yml inventory source with yaml plugin
Skipping callback 'default', as we already have a stdout callback.
Skipping callback 'minimal', as we already have a stdout callback.
Skipping callback 'oneline', as we already have a stdout callback.

PLAYBOOK: junos_sendconfig.yml **********************************************************************************************************************************************
1 plays in junos_sendconfig.yml

PLAY [Load and commit the config] *******************************************************************************************************************************************
META: ran handlers

TASK [Load config from local file and commit] *******************************************************************************************************************************
task path: /home/jpbz7878/Programming/Ansible/junos/junos_sendconfig.yml:12
<acc1> ESTABLISH LOCAL CONNECTION FOR USER: jpbz7878
<acc1> EXEC /bin/sh -c 'echo ~jpbz7878 amp;amp; sleep 0'
<acc1> EXEC /bin/sh -c '( umask 77 amp;amp; mkdir -p "` echo /home/jpbz7878/.ansible/tmp `"amp;amp; mkdir "` echo /home/jpbz7878/.ansible/tmp/ansible-tmp-1604716352.6777682-31418-79718055491155 `" amp;amp; echo ansible-tmp-1604716352.6777682-31418-79718055491155="` echo /home/jpbz7878/.ansible/tmp/ansible-tmp-1604716352.6777682-31418-79718055491155 `" ) amp;amp; sleep 0'
<acc2> ESTABLISH LOCAL CONNECTION FOR USER: jpbz7878
<acc2> EXEC /bin/sh -c 'echo ~jpbz7878 amp;amp; sleep 0'
<acc2> EXEC /bin/sh -c '( umask 77 amp;amp; mkdir -p "` echo /home/jpbz7878/.ansible/tmp `"amp;amp; mkdir "` echo /home/jpbz7878/.ansible/tmp/ansible-tmp-1604716352.6911237-31419-243034751392346 `" amp;amp; echo ansible-tmp-1604716352.6911237-31419-243034751392346="` echo /home/jpbz7878/.ansible/tmp/ansible-tmp-1604716352.6911237-31419-243034751392346 `" ) amp;amp; sleep 0'
Using module file /home/jpbz7878/.ansible/roles/Juniper.junos/library/juniper_junos_config.py
<acc1> PUT /home/jpbz7878/.ansible/tmp/ansible-local-31411hu73ud51/tmpt2duaeb7 TO /home/jpbz7878/.ansible/tmp/ansible-tmp-1604716352.6777682-31418-79718055491155/AnsiballZ_juniper_junos_config.py
Using module file /home/jpbz7878/.ansible/roles/Juniper.junos/library/juniper_junos_config.py
<acc1> EXEC /bin/sh -c 'chmod u x /home/jpbz7878/.ansible/tmp/ansible-tmp-1604716352.6777682-31418-79718055491155/ /home/jpbz7878/.ansible/tmp/ansible-tmp-1604716352.6777682-31418-79718055491155/AnsiballZ_juniper_junos_config.py amp;amp; sleep 0'
<acc2> PUT /home/jpbz7878/.ansible/tmp/ansible-local-31411hu73ud51/tmpyv9dt8vx TO /home/jpbz7878/.ansible/tmp/ansible-tmp-1604716352.6911237-31419-243034751392346/AnsiballZ_juniper_junos_config.py
<acc2> EXEC /bin/sh -c 'chmod u x /home/jpbz7878/.ansible/tmp/ansible-tmp-1604716352.6911237-31419-243034751392346/ /home/jpbz7878/.ansible/tmp/ansible-tmp-1604716352.6911237-31419-243034751392346/AnsiballZ_juniper_junos_config.py amp;amp; sleep 0'
<acc1> EXEC /bin/sh -c '/usr/bin/python3.6 /home/jpbz7878/.ansible/tmp/ansible-tmp-1604716352.6777682-31418-79718055491155/AnsiballZ_juniper_junos_config.py amp;amp; sleep 0'
<acc2> EXEC /bin/sh -c '/usr/bin/python3.6 /home/jpbz7878/.ansible/tmp/ansible-tmp-1604716352.6911237-31419-243034751392346/AnsiballZ_juniper_junos_config.py amp;amp; sleep 0'
<acc2> EXEC /bin/sh -c 'rm -f -r /home/jpbz7878/.ansible/tmp/ansible-tmp-1604716352.6911237-31419-243034751392346/ > /dev/null 2>amp;1 amp;amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
  File "/home/jpbz7878/.ansible/tmp/ansible-tmp-1604716352.6911237-31419-243034751392346/AnsiballZ_juniper_junos_config.py", line 102, in <module>
    _ansiballz_main()
  File "/home/jpbz7878/.ansible/tmp/ansible-tmp-1604716352.6911237-31419-243034751392346/AnsiballZ_juniper_junos_config.py", line 94, in _ansiballz_main
    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
  File "/home/jpbz7878/.ansible/tmp/ansible-tmp-1604716352.6911237-31419-243034751392346/AnsiballZ_juniper_junos_config.py", line 40, in invoke_module
    runpy.run_module(mod_name='ansible.modules.juniper_junos_config', init_globals=None, run_name='__main__', alter_sys=True)
  File "/usr/lib64/python3.6/runpy.py", line 205, in run_module
    return _run_module_code(code, init_globals, run_name, mod_spec)
  File "/usr/lib64/python3.6/runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "/usr/lib64/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/tmp/ansible_juniper_junos_config_payload_mt06_8b2/ansible_juniper_junos_config_payload.zip/ansible/modules/juniper_junos_config.py", line 1148, in <module>
  File "/tmp/ansible_juniper_junos_config_payload_mt06_8b2/ansible_juniper_junos_config_payload.zip/ansible/modules/juniper_junos_config.py", line 1054, in main
  File "/tmp/ansible_juniper_junos_config_payload_mt06_8b2/ansible_juniper_junos_config_payload.zip/ansible/module_utils/juniper_junos_common.py", line 1660, in load_configuration
  File "/home/jpbz7878/.local/lib/python3.6/site-packages/jnpr/junos/utils/config.py", line 557, in load
    template = self.dev.Template(path)
  File "/home/jpbz7878/.local/lib/python3.6/site-packages/jnpr/junos/device.py", line 516, in Template
    return self._j2ldr.get_template(filename, parent, gvars)
  File "/usr/local/lib/python3.6/site-packages/jinja2/environment.py", line 830, in get_template
    return self._load_template(name, self.make_globals(globals))
  File "/usr/local/lib/python3.6/site-packages/jinja2/environment.py", line 804, in _load_template
    template = self.loader.load(self, name, globals)
  File "/usr/local/lib/python3.6/site-packages/jinja2/loaders.py", line 113, in load
    source, filename, uptodate = self.get_source(environment, name)
  File "/home/jpbz7878/.local/lib/python3.6/site-packages/jnpr/junos/device.py", line 73, in get_source
    raise jinja2.TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: {'src': './templates/intf.j2', 'dest': './build_conf/acc2/intf.cfg'}
fatal: [acc2]: FAILED! => {
    "changed": false,
    "module_stderr": "Traceback (most recent call last):n  File "/home/jpbz7878/.ansible/tmp/ansible-tmp-1604716352.6911237-31419-243034751392346/AnsiballZ_juniper_junos_config.py", line 102, in <module>n    _ansiballz_main()n  File "/home/jpbz7878/.ansible/tmp/ansible-tmp-1604716352.6911237-31419-243034751392346/AnsiballZ_juniper_junos_config.py", line 94, in _ansiballz_mainn    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)n  File "/home/jpbz7878/.ansible/tmp/ansible-tmp-1604716352.6911237-31419-243034751392346/AnsiballZ_juniper_junos_config.py", line 40, in invoke_modulen    runpy.run_module(mod_name='ansible.modules.juniper_junos_config', init_globals=None, run_name='__main__', alter_sys=True)n  File "/usr/lib64/python3.6/runpy.py", line 205, in run_modulen    return _run_module_code(code, init_globals, run_name, mod_spec)n  File "/usr/lib64/python3.6/runpy.py", line 96, in _run_module_coden    mod_name, mod_spec, pkg_name, script_name)n  File "/usr/lib64/python3.6/runpy.py", line 85, in _run_coden    exec(code, run_globals)n  File "/tmp/ansible_juniper_junos_config_payload_mt06_8b2/ansible_juniper_junos_config_payload.zip/ansible/modules/juniper_junos_config.py", line 1148, in <module>n  File "/tmp/ansible_juniper_junos_config_payload_mt06_8b2/ansible_juniper_junos_config_payload.zip/ansible/modules/juniper_junos_config.py", line 1054, in mainn  File "/tmp/ansible_juniper_junos_config_payload_mt06_8b2/ansible_juniper_junos_config_payload.zip/ansible/module_utils/juniper_junos_common.py", line 1660, in load_configurationn  File "/home/jpbz7878/.local/lib/python3.6/site-packages/jnpr/junos/utils/config.py", line 557, in loadn    template = self.dev.Template(path)n  File "/home/jpbz7878/.local/lib/python3.6/site-packages/jnpr/junos/device.py", line 516, in Templaten    return self._j2ldr.get_template(filename, parent, gvars)n  File "/usr/local/lib/python3.6/site-packages/jinja2/environment.py", line 830, in get_templaten    return self._load_template(name, self.make_globals(globals))n  File "/usr/local/lib/python3.6/site-packages/jinja2/environment.py", line 804, in _load_templaten    template = self.loader.load(self, name, globals)n  File "/usr/local/lib/python3.6/site-packages/jinja2/loaders.py", line 113, in loadn    source, filename, uptodate = self.get_source(environment, name)n  File "/home/jpbz7878/.local/lib/python3.6/site-packages/jnpr/junos/device.py", line 73, in get_sourcen    raise jinja2.TemplateNotFound(template)njinja2.exceptions.TemplateNotFound: {'src': './templates/intf.j2', 'dest': './build_conf/acc2/intf.cfg'}n",
    "module_stdout": "",
    "msg": "MODULE FAILUREnSee stdout/stderr for the exact error",
    "rc": 1
}
<acc1> EXEC /bin/sh -c 'rm -f -r /home/jpbz7878/.ansible/tmp/ansible-tmp-1604716352.6777682-31418-79718055491155/ > /dev/null 2>amp;1 amp;amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
  File "/home/jpbz7878/.ansible/tmp/ansible-tmp-1604716352.6777682-31418-79718055491155/AnsiballZ_juniper_junos_config.py", line 102, in <module>
    _ansiballz_main()
  File "/home/jpbz7878/.ansible/tmp/ansible-tmp-1604716352.6777682-31418-79718055491155/AnsiballZ_juniper_junos_config.py", line 94, in _ansiballz_main
    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
  File "/home/jpbz7878/.ansible/tmp/ansible-tmp-1604716352.6777682-31418-79718055491155/AnsiballZ_juniper_junos_config.py", line 40, in invoke_module
    runpy.run_module(mod_name='ansible.modules.juniper_junos_config', init_globals=None, run_name='__main__', alter_sys=True)
  File "/usr/lib64/python3.6/runpy.py", line 205, in run_module
    return _run_module_code(code, init_globals, run_name, mod_spec)
  File "/usr/lib64/python3.6/runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "/usr/lib64/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/tmp/ansible_juniper_junos_config_payload_rjlq97ce/ansible_juniper_junos_config_payload.zip/ansible/modules/juniper_junos_config.py", line 1148, in <module>
  File "/tmp/ansible_juniper_junos_config_payload_rjlq97ce/ansible_juniper_junos_config_payload.zip/ansible/modules/juniper_junos_config.py", line 1054, in main
  File "/tmp/ansible_juniper_junos_config_payload_rjlq97ce/ansible_juniper_junos_config_payload.zip/ansible/module_utils/juniper_junos_common.py", line 1660, in load_configuration
  File "/home/jpbz7878/.local/lib/python3.6/site-packages/jnpr/junos/utils/config.py", line 557, in load
    template = self.dev.Template(path)
  File "/home/jpbz7878/.local/lib/python3.6/site-packages/jnpr/junos/device.py", line 516, in Template
    return self._j2ldr.get_template(filename, parent, gvars)
  File "/usr/local/lib/python3.6/site-packages/jinja2/environment.py", line 830, in get_template
    return self._load_template(name, self.make_globals(globals))
  File "/usr/local/lib/python3.6/site-packages/jinja2/environment.py", line 804, in _load_template
    template = self.loader.load(self, name, globals)
  File "/usr/local/lib/python3.6/site-packages/jinja2/loaders.py", line 113, in load
    source, filename, uptodate = self.get_source(environment, name)
  File "/home/jpbz7878/.local/lib/python3.6/site-packages/jnpr/junos/device.py", line 73, in get_source
    raise jinja2.TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: {'src': './templates/intf.j2', 'dest': './build_conf/acc1/intf.cfg'}
fatal: [acc1]: FAILED! => {
    "changed": false,
    "module_stderr": "Traceback (most recent call last):n  File "/home/jpbz7878/.ansible/tmp/ansible-tmp-1604716352.6777682-31418-79718055491155/AnsiballZ_juniper_junos_config.py", line 102, in <module>n    _ansiballz_main()n  File "/home/jpbz7878/.ansible/tmp/ansible-tmp-1604716352.6777682-31418-79718055491155/AnsiballZ_juniper_junos_config.py", line 94, in _ansiballz_mainn    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)n  File "/home/jpbz7878/.ansible/tmp/ansible-tmp-1604716352.6777682-31418-79718055491155/AnsiballZ_juniper_junos_config.py", line 40, in invoke_modulen    runpy.run_module(mod_name='ansible.modules.juniper_junos_config', init_globals=None, run_name='__main__', alter_sys=True)n  File "/usr/lib64/python3.6/runpy.py", line 205, in run_modulen    return _run_module_code(code, init_globals, run_name, mod_spec)n  File "/usr/lib64/python3.6/runpy.py", line 96, in _run_module_coden    mod_name, mod_spec, pkg_name, script_name)n  File "/usr/lib64/python3.6/runpy.py", line 85, in _run_coden    exec(code, run_globals)n  File "/tmp/ansible_juniper_junos_config_payload_rjlq97ce/ansible_juniper_junos_config_payload.zip/ansible/modules/juniper_junos_config.py", line 1148, in <module>n  File "/tmp/ansible_juniper_junos_config_payload_rjlq97ce/ansible_juniper_junos_config_payload.zip/ansible/modules/juniper_junos_config.py", line 1054, in mainn  File "/tmp/ansible_juniper_junos_config_payload_rjlq97ce/ansible_juniper_junos_config_payload.zip/ansible/module_utils/juniper_junos_common.py", line 1660, in load_configurationn  File "/home/jpbz7878/.local/lib/python3.6/site-packages/jnpr/junos/utils/config.py", line 557, in loadn    template = self.dev.Template(path)n  File "/home/jpbz7878/.local/lib/python3.6/site-packages/jnpr/junos/device.py", line 516, in Templaten    return self._j2ldr.get_template(filename, parent, gvars)n  File "/usr/local/lib/python3.6/site-packages/jinja2/environment.py", line 830, in get_templaten    return self._load_template(name, self.make_globals(globals))n  File "/usr/local/lib/python3.6/site-packages/jinja2/environment.py", line 804, in _load_templaten    template = self.loader.load(self, name, globals)n  File "/usr/local/lib/python3.6/site-packages/jinja2/loaders.py", line 113, in loadn    source, filename, uptodate = self.get_source(environment, name)n  File "/home/jpbz7878/.local/lib/python3.6/site-packages/jnpr/junos/device.py", line 73, in get_sourcen    raise jinja2.TemplateNotFound(template)njinja2.exceptions.TemplateNotFound: {'src': './templates/intf.j2', 'dest': './build_conf/acc1/intf.cfg'}n",
    "module_stdout": "",
    "msg": "MODULE FAILUREnSee stdout/stderr for the exact error",
    "rc": 1
}

PLAY RECAP ******************************************************************************************************************************************************************
acc1                       : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
acc2                       : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
  

Кто-нибудь сталкивался с такой проблемой?

Если да, спасибо за вашу помощь.

С уважением.

Ответ №1:

Вы должны использовать переменную playbook_dir :

 template: "{{ playbook_dir }}/templates/intf.j2"
  

Полный список специальных переменных вы найдете здесь:
ссылка на специальные переменные

Комментарии:

1. я изменил свой ответ в соответствии с документацией junos-ansible-modules.readthedocs.io/en/2.0.0 /… . Там написано: параметры src, lines, template и url являются взаимоисключающими. Поэтому, если вы используете шаблон, не используйте src и т. Д. Удачи