# Specifying template src with a variable

**URL:** <https://forum.ansible.com/t/specifying-template-src-with-a-variable/32554>\
**Category:** Ansible Project\
**Created:** [March 29, 2020, 7:14am UTC](https://forum.ansible.com/t/specifying-template-src-with-a-variable/32554 "2020-03-29T07:14:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Geo\_P.C](https://avatars.discourse-cdn.com/v4/letter/g/b3f665/32.png) [@Geo\_P.C](https://forum.ansible.com/u/Geo_P.C)\
**Post date:** [March 29, 2020, 7:14am UTC](https://forum.ansible.com/t/specifying-template-src-with-a-variable/32554/1 "2020-03-29T07:14:30Z")

</div>

In ansible we are trying to access different templates based on variable.

We have following template files like:

templates  
app1.conf.j2  
app2.conf.j2  
app3.conf.j2

taks  
app.yml

In tasks we need to copy template file based on the app name. for eg: we  
will specify a variable named "instance\_name" to either app1 or app2 or  
app3.

Now based on the variable we need to copy the app file to /opt/((  
instance\_name }}/conf.d/.

we created ansbile task as follows but its not working.

- name: 'Copy {{ instance\_name }} file to /opt/conf.d/ Directory'  
&nbsp;&nbsp;&nbsp;&nbsp;template:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: "{{ instance\_name }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: "/opt/{{ instance\_name }}/conf.d/"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;owner: root  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;group: root  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mode: 0644

When we hard code "src" to app1.conf.j2 its working for app1.

Please let us know is it possible with this method? We are having around 20  
apps and whats the best method to simplify the ansible playbook to specify  
only the variable.

Thanks & Regards  
Geo

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [March 29, 2020, 7:20am UTC](https://forum.ansible.com/t/specifying-template-src-with-a-variable/32554/2 "2020-03-29T07:20:25Z")

</div>

> In ansible we are trying to access different templates based on variable.
> 
> We have following template files like:
> 
> templates  
> app1.conf.j2  
> app2.conf.j2  
> app3.conf.j2
> 
> taks  
> app.yml
> 
> In tasks we need to copy template file based on the app name. for eg: we will specify a variable named "instance\_name"  
> to either app1 or app2 or app3.
> 
> Now based on the variable we need to copy the app file to /opt/(( instance\_name }}/conf.d/.
> 
> we created ansbile task as follows but its not working.
> 
> - name: 'Copy {{ instance\_name }} file to /opt/conf.d/ Directory'  
> template:  
> src: "{{ instance\_name }}"  
> dest: "/opt/{{ instance\_name }}/conf.d/"  
> owner: root  
> group: root  
> mode: 0644
> 
> When we hard code "src" to app1.conf.j2 its working for app1.

You don't give any details how your task fails, but I would guess you need to say:

&nbsp;&nbsp;&nbsp;&nbsp;src: "{{ instance\_name }}.conf.j2"

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Geo\_P.C](https://avatars.discourse-cdn.com/v4/letter/g/b3f665/32.png) [@Geo\_P.C](https://forum.ansible.com/u/Geo_P.C)\
**Post date:** [March 29, 2020, 7:52am UTC](https://forum.ansible.com/t/specifying-template-src-with-a-variable/32554/3 "2020-03-29T07:52:34Z")

</div>

Thanks Racke.

Sorry it was my typo. Pls see my app.yml:

- name: ‘Copy {{ instance\_name }} file to /opt/conf.d/ Directory’  
template:  
src: “{{ instance\_name }}.conf.j2”  
dest: “/opt/{{ instance\_name }}/conf.d/”  
owner: root  
group: root  
mode: 0644

In defaults main.yml defined vaiable as

instance\_name: ‘app1’

Please see the error I am getting:

TASK [test : Copy app1 file to /opt/conf.d/ Directory] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [target1]: FAILED! =\> {“changed”: false, “msg”: “Could not find or access ‘app1.conf.j2’\nSearched in:\n\t/mnt/d/Ansible/apache-playbook/roles/test/templates/app1.conf.j2\n\t/mnt/d/Ansible/apache-playbook/roles/test/app1.conf.j2\n\t/mnt/d/Ansible/apache-playbook/roles/test/tasks/templates/app1.conf.j2\n\t/mnt/d/Ansible/apache-playbook/roles/test/tasks/app1.conf.j2\n\t/mnt/d/Ansible/apache-playbook/templates/app1.conf.j2\n\t/mnt/d/Ansible/apache-playbook/app1.conf.j2 on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote\_src option”}

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
target1 : ok=2 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [March 29, 2020, 8:02am UTC](https://forum.ansible.com/t/specifying-template-src-with-a-variable/32554/4 "2020-03-29T08:02:00Z")

</div>

> Thanks Racke.
> 
> Sorry it was my typo. Pls see my app.yml:
> 
> - name: 'Copy {{ instance\_name }} file to /opt/conf.d/ Directory'  
> template:  
> src: "{{ instance\_name }}.conf.j2"  
> dest: "/opt/{{ instance\_name }}/conf.d/"  
> owner: root  
> group: root  
> mode: 0644
> 
> In defaults main.yml defined vaiable as
> 
> instance\_name: 'app1'
> 
> Please see the error I am getting:
> 
> TASK [test : Copy app1 file to /opt/conf.d/ Directory]  
> \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> fatal: [target1]: FAILED! =\> {"changed": false, "msg": "Could not find or access 'app1.conf.j2'\nSearched  
> in:\n\t/mnt/d/Ansible/apache-playbook/roles/test/templates/app1.conf.j2\n\t/mnt/d/Ansible/apache-playbook/roles/test/app1.conf.j2\n\t/mnt/d/Ansible/apache-playbook/roles/test/tasks/templates/app1.conf.j2\n\t/mnt/d/Ansible/apache-playbook/roles/test/tasks/app1.conf.j2\n\t/mnt/d/Ansible/apache-playbook/templates/app1.conf.j2\n\t/mnt/d/Ansible/apache-playbook/app1.conf.j2  
> on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote\_src  
> option"}
> 
> PLAY RECAP  
> \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> target1 : ok=2 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
> 
> Can you please help me on this.

Where is your template located? It needs to be on the Ansible controller.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Geo\_P.C](https://avatars.discourse-cdn.com/v4/letter/g/b3f665/32.png) [@Geo\_P.C](https://forum.ansible.com/u/Geo_P.C)\
**Post date:** [March 29, 2020, 8:16am UTC](https://forum.ansible.com/t/specifying-template-src-with-a-variable/32554/5 "2020-03-29T08:16:52Z")

</div>

Yes template located on controller itself. We can it’s getting the name “app1”.

For template src only its getting error.

Pls see variable instance\_name  
got it’s value as app1 in the result. Issue is only with src:

TASK [test : Copy app1 file to /opt/conf.d/ Directory

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [March 29, 2020, 8:26am UTC](https://forum.ansible.com/t/specifying-template-src-with-a-variable/32554/6 "2020-03-29T08:26:44Z")

</div>

> Yes template located on controller itself. We can it's getting the name "app1".
> 
> For template src only its getting error.
> 
> Pls see variable instance\_name  
> got it's value as app1 in the result. Issue is only with src:
> 
> TASK [test : Copyapp1file to /opt/conf.d/ Directory

What is the exact file name of the app1 template ? Also why don't you use copy: instead of template: ?

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Geo\_P.C](https://avatars.discourse-cdn.com/v4/letter/g/b3f665/32.png) [@Geo\_P.C](https://forum.ansible.com/u/Geo_P.C)\
**Post date:** [March 29, 2020, 10:43am UTC](https://forum.ansible.com/t/specifying-template-src-with-a-variable/32554/7 "2020-03-29T10:43:10Z")

</div>

Yes I am using templates and created template files as:

templates

app1.conf.j2  
app2.conf.j2  
app3.conf.j2

taks

app.yml
