Could not find or access template file

hi

I’m trying to setup a linux service on remote host

I have my file in template folder

.
├── group_vars
│ ├── artefacts_conf.yml
│ ├── jdk_wl_conf.yml
│ ├── configuration_conf.yml
├── hosts

└── templates
├── nodemanager_service.j2

my roles are using

  • name: Copy nodemanager systemd script
    template:
    src: “{{nodemanager_service_template}}”
    dest: /lib/systemd/system/nodemanager.service
    mode: 0664
    become: yes

however playbook fails with:

Could not find or access 'nodemanager_service.j2

any advise?

(In the case "Could not find 'nodemanager_service.j2')

Quoting from "The magic of ‘local’ paths"
https://docs.ansible.com/ansible/latest/user_guide/playbook_pathing.html#the-magic-of-local-paths

  "The paths will be searched from most specific to most general (i.e role
  before play). ...
  role search path is rolename/{files|vars|templates}/, rolename/tasks/.
  play search path is playdir/{files|vars|templates}/, playdir/."

It's possible to use "Special variables" in a path. For example
"playbook_dir", or "role_path". See
https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html

Or, link the "template" directory into any of the paths being searched.

Cheers,

  -vlado

hi

I'm trying to setup a linux service on remote host

I have my file in template folder
.
├── group_vars
│ ├── artefacts_conf.yml
│ ├── jdk_wl_conf.yml
│ ├── configuration_conf.yml
├── hosts
└── templates
├── nodemanager_service.j2

my roles are using
- name: Copy nodemanager systemd script
template:
src: "{{nodemanager_service_template}}"
dest: /lib/systemd/system/nodemanager.service
mode: 0664
become: yes

however playbook fails with:

Could not find or access 'nodemanager_service.j2

any advise?

Hello Marten,

try to run the playbook with -vv. This should display the locations where Ansible looks for the template file.

Regards
        Racke

hi

I used full path in my vars file. that solve it

{{template_dir}}/{{template_file}}

The templates directory is only searched within a role. A directory named templates next to a playbook is not searched. You will need to reference the path to the template file. I usually use playbook_dir variable, but you could also just use templates/. See the variables documentation for more details.

Either of these should work:

src: “{{ playbook_dir }}/templates/{{ nodemanager_service_template }}”

or

src: templates/{{ nodemanager_service_template }}

Hi Morten,

Please try :

  • name: Copy nodemanager systemd script
    template:
    src: nodemanager_service_template.j2L
    dest: /lib/systemd/system/nodemanager.service
    mode: 0664
    become: yes

Hope this will work!

Regards,
Nivedita