Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ansible

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.

like image 302
Robert Avatar asked Jul 03 '14 16:07

Robert


People also ask

How do templates work in Ansible?

Ansible templates are typically saved as . tpl files and support the use of variables, loops, and conditional expressions. Templates are commonly used to configure services based on variable values that can be set up on the playbook itself, in included variable files, or obtained via facts.

Where are Ansible templates stored?

Templates usually are stored under “templates” directories with “. j2” file extension. The “dest” parameter specifies the path where to render the template on the remote machine. The “validate” parameters allow you to specify the validation command to run before copying it into place.

What extension do the template files in Ansible have?

Usually, the template files will have the . j2 extension, which denotes the Jinja2 templating engine used.

How do I find my role path in Ansible?

Role Search Path Ansible will search for roles in the following way: A roles/ directory, relative to the playbook file. By default, in /etc/ansible/roles.


2 Answers

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.

like image 138
Bruce P Avatar answered Oct 06 '22 02:10

Bruce P


If you want to access the "fig.conf.j2" template file from site.yml, you need to create a templates directory besides site.yml and put the file "fig.conf.j2" inside it.

As per your current directory structure, since the templates directory is inside a role. You need to access the template from tasks/main.yml inside that role

like image 32
Vignesh Sk Avatar answered Oct 06 '22 04:10

Vignesh Sk