Specifying template src with a variable

30 views
Skip to first unread message

Geo P.C.

unread,
Mar 29, 2020, 3:15:19 AM3/29/20
to ansible...@googlegroups.com

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.

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

Stefan Hornburg (Racke)

unread,
Mar 29, 2020, 3:21:02 AM3/29/20
to ansible...@googlegroups.com
On 3/29/20 9:14 AM, Geo P.C. wrote:
>
> 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:

src: "{{ instance_name }}.conf.j2"

Regards
Racke

>
> 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
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CAKtc8vWncufK%3DvO%3DEpRCUr_MavBAzT_wSWURXybW7b3r-BO_UA%40mail.gmail.com
> <https://groups.google.com/d/msgid/ansible-project/CAKtc8vWncufK%3DvO%3DEpRCUr_MavBAzT_wSWURXybW7b3r-BO_UA%40mail.gmail.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc

Geo P.C.

unread,
Mar 29, 2020, 3:53:20 AM3/29/20
to ansible...@googlegroups.com
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.


To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/944bd884-7913-6132-f20a-801d3b52483f%40linuxia.de.

Stefan Hornburg (Racke)

unread,
Mar 29, 2020, 4:02:40 AM3/29/20
to ansible...@googlegroups.com
On 3/29/20 9:52 AM, Geo P.C. wrote:
> 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
Racke
> > ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>
> <mailto:ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>>.
> > To view this discussion on the web visit
> >
> https://groups.google.com/d/msgid/ansible-project/CAKtc8vWncufK%3DvO%3DEpRCUr_MavBAzT_wSWURXybW7b3r-BO_UA%40mail.gmail.com
> >
> <https://groups.google.com/d/msgid/ansible-project/CAKtc8vWncufK%3DvO%3DEpRCUr_MavBAzT_wSWURXybW7b3r-BO_UA%40mail.gmail.com?utm_medium=email&utm_source=footer>.
>
>
> --
> Ecommerce and Linux consulting + Perl and web application programming.
> Debian and Sympa administration. Provisioning with Ansible.
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/944bd884-7913-6132-f20a-801d3b52483f%40linuxia.de.
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CAKtc8vVHcky9Yxa76e5rwSN%3D0b-rtj9d_cMtraDkUZYDToMYvw%40mail.gmail.com
> <https://groups.google.com/d/msgid/ansible-project/CAKtc8vVHcky9Yxa76e5rwSN%3D0b-rtj9d_cMtraDkUZYDToMYvw%40mail.gmail.com?utm_medium=email&utm_source=footer>.
signature.asc

Geo P.C.

unread,
Mar 29, 2020, 4:18:21 AM3/29/20
to ansible...@googlegroups.com
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


To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/3c9fc58f-319d-2d90-eee6-4cf865ad743d%40linuxia.de.

Stefan Hornburg (Racke)

unread,
Mar 29, 2020, 4:27:19 AM3/29/20
to ansible...@googlegroups.com
On 3/29/20 10:16 AM, Geo P.C. wrote:
> 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
Racke
> <mailto:ansible-project%2Bunsu...@googlegroups.com <mailto:ansible-project%252Buns...@googlegroups.com>>
> >     <mailto:ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>
> <mailto:ansible-project%2Bunsu...@googlegroups.com <mailto:ansible-project%252Buns...@googlegroups.com>>>.
> >     > To view this discussion on the web visit
> >     >
> >   
>  https://groups.google.com/d/msgid/ansible-project/CAKtc8vWncufK%3DvO%3DEpRCUr_MavBAzT_wSWURXybW7b3r-BO_UA%40mail.gmail.com
> >     >
> >   
>  <https://groups.google.com/d/msgid/ansible-project/CAKtc8vWncufK%3DvO%3DEpRCUr_MavBAzT_wSWURXybW7b3r-BO_UA%40mail.gmail.com?utm_medium=email&utm_source=footer>.
> >
> >
> >     --
> >     Ecommerce and Linux consulting + Perl and web application programming.
> >     Debian and Sympa administration. Provisioning with Ansible.
> >
> >     --
> >     You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> >     To unsubscribe from this group and stop receiving emails from it, send an email to
> >     ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>
> <mailto:ansible-project%2Bunsu...@googlegroups.com <mailto:ansible-project%252Buns...@googlegroups.com>>.
> >     To view this discussion on the web visit
> >     https://groups.google.com/d/msgid/ansible-project/944bd884-7913-6132-f20a-801d3b52483f%40linuxia.de.
> >
> > --
> > You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> > To unsubscribe from this group and stop receiving emails from it, send an email to
> https://groups.google.com/d/msgid/ansible-project/CAKtc8vVHcky9Yxa76e5rwSN%3D0b-rtj9d_cMtraDkUZYDToMYvw%40mail.gmail.com
> >
> <https://groups.google.com/d/msgid/ansible-project/CAKtc8vVHcky9Yxa76e5rwSN%3D0b-rtj9d_cMtraDkUZYDToMYvw%40mail.gmail.com?utm_medium=email&utm_source=footer>.
>
>
> --
> Ecommerce and Linux consulting + Perl and web application programming.
> Debian and Sympa administration. Provisioning with Ansible.
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/3c9fc58f-319d-2d90-eee6-4cf865ad743d%40linuxia.de.
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CAKtc8vWO1PsjFmDHJ6J%3DZ2Ye8S8grYAjogja00M%2BXaRk7Js0Tg%40mail.gmail.com
> <https://groups.google.com/d/msgid/ansible-project/CAKtc8vWO1PsjFmDHJ6J%3DZ2Ye8S8grYAjogja00M%2BXaRk7Js0Tg%40mail.gmail.com?utm_medium=email&utm_source=footer>.
signature.asc

Geo P.C.

unread,
Mar 29, 2020, 6:44:42 AM3/29/20
to ansible...@googlegroups.com
Yes I am using templates and created template files as:

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

taks
app.yml
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/2c05a10a-fcad-ce70-ea58-e322549650ca%40linuxia.de.
Reply all
Reply to author
Forward
0 new messages