Why Ansible doesn't read the templates in relative path?

ansible

Solution

Ansible will only look in the roles/users/templates directory when you explicitly use the role "users", which you are not using in your example. To do what you want you need to change your site.yml to look something like this:

- hosts: users
  remote_user: root
  sudo: True
  roles:
    - { role: users }

Then in roles/users/tasks/main.yml you would have:

- name: templates
  template: src="fig.conf.j2" dest="/home/vagrant/fig.conf"

The role in site.yml tells Ansible to invoke the yaml file roles/users/tasks/main.yml. Tasks that refer to files or templates within that role will by default look in roles/users/files and roles/users/templates for those files/templates. You might want to read more about roles in the Ansible documentation to make sure you better understand how they fit together.

Problem

I am using Ansible and I have some problems with the templates path. Here is the error output when I execute: ``` $ ansible-playbook -i hosts site.yml PLAY [users] ****************************************************************** GATHERING FACTS *************************************************************** ok: [10.0.3.240] TASK: [templates] ************************************************************* fatal: [10.0.3.240] => {'msg': 'unable to read /home/robe/Desktop/ansible_demo/fig.conf.j2', 'failed': True} fatal: [10.0.3.240] => {'msg': 'unable to read /home/robe/Desktop/ansible_demo/fig.conf.j2', 'failed': True} FATAL: all hosts have already failed -- aborting PLAY RECAP ******************************************************************** to retry, use: --limit @/home/robe/site.retry 10.0.3.240 : ok=1 changed=0 unreachable=1 failed=0 ``` This is my project structure: ``` $ tree . ├── ansible.cfg ├── hosts ├── roles │   └── users │   ├── files │   ├── handlers │   │   └── main.yml │   ├── tasks │   │   └── main.yml │   ├── templates │   │   └── fig.conf.j2 │   └── vars │   └── main.yml ├── site.yml └── Vagrantfile ``` This is my site.yml code: ``` --- - hosts: users remote_user: root sudo: True tasks: - name: templates template: src="fig.conf.j2" dest="/home/vagrant/fig.conf" ``` Then, why Ansible doesn't look into templates directory and it only looks in the root directory.

Original source