Specifying template src with a variable

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

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

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

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

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

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

Yes I am using templates and created template files as:

templates

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

taks

app.yml