How to template tasks file?

You cannot template a whole task file.

What you need is to put a with_indexed_items on each of those lineinfile tasks, as opposed to using a jinja for loop around them.

http://docs.ansible.com/ansible/playbooks_loops.html#looping-over-a-list-with-an-index