'Could not find or access xxx.xxx.j2 on ansible role

I'm tyring to create a role which perform the installation of Apache2 (http). I have the following structure (where instalar.yaml contains the task that i'm running):

.
├── instalar.yml
└── roles
    └── httpd
        ├── files
        │   └── httpd.conf
        ├── handlers
        │   └── main.yml
        ├── tasks
        │   └── main.yaml
        └── templates
            └── index.html.j2

My instalar.yaml contains:

---
- name: Playbook ejemplo roles
  hosts: aws_cev
  become: true
  roles:
    - httpd

And roles/httpd/tasks/main.yaml:

---
- name: Install apache packages 
  yum:
    name: httpd
    state: present

- name: ensure httpd is running
  service:
    name: httpd 
    state: started

- name: Copiar el fichero de configuracion
  copy:
    src: httpd.conf 
    dest: /etc/httpd/conf/httpd.conf

#
# FAILING TASK
#
- name: Index copy
  copy:
    src: index.html.j2
    dest: /var/www/html/index.html

All the task runs ok except Index copy. This task return an error:

fatal: [frontal_web]: FAILED! => {
    "changed": false,
    "invocation": {
        "dest": "/var/www/html/index.html",
        "module_args": {
            "dest": "/var/www/html/index.html",
            "src": "index.html.j2"
        },
        "src": "index.html.j2"
    },
    "msg": "Could not find or access 'index.html.j2'\nSearched in:

The error includes a list of directories where it have found such a /roles/httpd/files, /roles/httpd/tasks/files/ but not the directory where I put the template (following the docs which is roles/httpd/templates.

Where is the error?



Solution 1:[1]

Finally I found the mistake.

I was using the copy module instead of template module:

- name: Index copy
  template:
    src: index.html.j2
    dest: /var/www/html/index.html

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jose A. MatarĂ¡n