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.
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.
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.
Usually, the template files will have the . j2 extension, which denotes the Jinja2 templating engine used.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With