# template "Missing end of comment tag"

**URL:** <https://forum.ansible.com/t/template-missing-end-of-comment-tag/37094>\
**Category:** Ansible Project\
**Created:** [September 14, 2022, 2:03pm UTC](https://forum.ansible.com/t/template-missing-end-of-comment-tag/37094 "2022-09-14T14:03:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [September 14, 2022, 2:03pm UTC](https://forum.ansible.com/t/template-missing-end-of-comment-tag/37094/1 "2022-09-14T14:03:16Z")

</div>

I am trying to use template module to send 2 shell scripts to a remote.

### TASK ##################################################################################################

&nbsp;&nbsp;- name: configure postgres - template backup scripts  
&nbsp;&nbsp;&nbsp;&nbsp;ansible.builtin.template:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: "{{ item }}.j2"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: "{{ postgres\_base }}/tools/backup/{{ item }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;backup: yes  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;owner: postgres  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;group: postgres  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mode: 0600  
&nbsp;&nbsp;&nbsp;&nbsp;become: yes  
&nbsp;&nbsp;&nbsp;&nbsp;loop:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- backup\_postgres10.sh  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- backup\_archivelogs.sh  
### TASK ##################################################################################################

while the first item is being templated out fine the second fails with a "Missing end of comment tag. String: #!/bin/bash" error. With 0 experience with jinja2 I can not make any sense of what I found in the documentation in that respect.  
Content of both files it 100% original bash script content.

### backup\_archivelogs.sh.j2 #############################################################################

#!/bin/bash

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [September 14, 2022, 3:41pm UTC](https://forum.ansible.com/t/template-missing-end-of-comment-tag/37094/2 "2022-09-14T15:41:21Z")

</div>

The problem is, valid shell expressions like “${#ARCHIVE\_LOGS[@]}” contain “{#” which is Jinja for “start-of-comment” and you have no “#}” end-of-comment.

To get the string “${#ARCHIVE\_LOGS[@]}” to be generated in a template, you have to split the “{” from the “#”. Here’s one way:

while mapfile -t -n 20 ARCHIVE\_LOGS && ((${{ ‘{’ ~ ‘#’ }}ARCHIVE\_LOGS[@]}))

There are lots of other ways to slice it up though. Good luck.

---

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [September 15, 2022, 9:58am UTC](https://forum.ansible.com/t/template-missing-end-of-comment-tag/37094/3 "2022-09-15T09:58:26Z")

</div>

sorry false alarm. my backup\_archivelogs.sh file was completely off

> I am trying to use template module to send 2 shell scripts to a remote.
> 
> ### TASK ##################################################################################################
> 
> &nbsp;&nbsp;- name: configure postgres - template backup scripts  
> &nbsp;&nbsp;&nbsp;&nbsp;ansible.builtin.template:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: "{{ item }}.j2"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: "{{ postgres\_base }}/tools/backup/{{ item }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;backup: yes  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;owner: postgres  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;group: postgres  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mode: 0600  
> &nbsp;&nbsp;&nbsp;&nbsp;become: yes  
> &nbsp;&nbsp;&nbsp;&nbsp;loop:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- backup\_postgres10.sh  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- backup\_archivelogs.sh  
> ### TASK ##################################################################################################
> 
> while the first item is being templated out fine the second fails with a "Missing end of comment tag. String: #!/bin/bash" error. With 0 experience with jinja2 I can not make any sense of what I found in the documentation in that respect.  
> Content of both files it 100% original bash script content.
> 
> ### backup\_archivelogs.sh.j2 #############################################################################
> 
> #!/bin/bash  
> #  
> # $Header$  
> #  
> # backup postgres archivelogs to TSM
> 
> # source postgres env  
> source /opt/db/postgres/bin/.pg.env
> 
> #set -x
> 
> # write logs to backup  
> # do an incremental backup of the postgres archive/wal logs  
> # take a bunch of 20 logs per dsmc incr call  
> cd $PGARCH  
> while mapfile -t -n 20 ARCHIVE\_LOGS && ((${#ARCHIVE\_LOGS[@]}))  
> do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo "\>\>\> backing up: ${ARCHIVE\_LOGS[@]}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sudo /opt/tivoli/tsm/client/ba/bin/dsmc incr "${ARCHIVE\_LOGS[@]}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# check return code, if 0/true then purge logfile  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RET=$?  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if [$RET -eq 0]; then  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo "\>\>\> TSM incremental backup successful, purging archive logs up to ${ARCHIVE\_LOGS[-1]} \<\<\<"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rm "${ARCHIVE\_LOGS[@]}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fi  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sleep 2  
> done \< \<(ls -1 0\*)
> 
> ### OUTPUT ################################################################################################
> 
> TASK [configure postgres - template backup scripts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> task path: /home/username/repos/ansible/open\_source/postgres\_create\_service/postgres\_create\_service.yml:83  
> \<server.intern\> ESTABLISH SSH CONNECTION FOR USER: None  
> \<server.intern\> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o 'ControlPath="/home/username/.ansible/cp/8ca9f05e1f"' server.intern '/bin/sh -c '"'"'echo ~ && sleep 0'"'"''  
> \<server.intern\> (0, b'/home/username\n', b'')  
> \<server.intern\> ESTABLISH SSH CONNECTION FOR USER: None  
> \<server.intern\> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o 'ControlPath="/home/username/.ansible/cp/8ca9f05e1f"' server.intern '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo /var/tmp `"&& mkdir "` echo /var/tmp/ansible-tmp-1663162681.3683012-2675848-43258002595610 `" && echo ansible-tmp-1663162681.3683012-2675848-43258002595610="` echo /var/tmp/ansible-tmp-1663162681.3683012-2675848-43258002595610 `" ) && sleep 0'"'"''  
> \<server.intern\> (0, b'ansible-tmp-1663162681.3683012-2675848-43258002595610=/var/tmp/ansible-tmp-1663162681.3683012-2675848-43258002595610\n', b'')  
> Using module file /usr/lib/python3.8/site-packages/ansible/modules/stat.py  
> \<server.intern\> PUT /home/username/.ansible/tmp/ansible-local-2675679n2q7xy3g/tmpwcgv\_g\_c TO /var/tmp/ansible-tmp-1663162681.3683012-2675848-43258002595610/AnsiballZ\_stat.py  
> \<server.intern\> SSH: EXEC sftp -b - -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o 'ControlPath="/home/username/.ansible/cp/8ca9f05e1f"' '[server.intern]'  
> \<server.intern\> (0, b'sftp\> put /home/username/.ansible/tmp/ansible-local-2675679n2q7xy3g/tmpwcgv\_g\_c /var/tmp/ansible-tmp-1663162681.3683012-2675848-43258002595610/AnsiballZ\_stat.py\n', b'')  
> \<server.intern\> ESTABLISH SSH CONNECTION FOR USER: None  
> \<server.intern\> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o 'ControlPath="/home/username/.ansible/cp/8ca9f05e1f"' server.intern '/bin/sh -c '"'"'setfacl -m u:postgres:r-x /var/tmp/ansible-tmp-1663162681.3683012-2675848-43258002595610/ /var/tmp/ansible-tmp-1663162681.3683012-2675848-43258002595610/AnsiballZ\_stat.py && sleep 0'"'"''  
> \<server.intern\> (0, b'', b'')  
> \<server.intern\> ESTABLISH SSH CONNECTION FOR USER: None  
> \<server.intern\> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o 'ControlPath="/home/username/.ansible/cp/8ca9f05e1f"' -tt server.intern '/bin/sh -c '"'"'sudo -H -S -n -u postgres /bin/sh -c '"'"'"'"'"'"'"'"'echo BECOME-SUCCESS-ztbiuccpcrwstltpesmvsnlefldjhueu ; /usr/libexec/platform-python /var/tmp/ansible-tmp-1663162681.3683012-2675848-43258002595610/AnsiballZ\_stat.py'"'"'"'"'"'"'"'"' && sleep 0'"'"''  
> Escalation succeeded  
> \<server.intern\> (0, b'\r\n\r\n{"changed": false, "stat": {"exists": true, "path": "/opt/db/postgres/tools/backup/backup\_postgres10.sh", "mode": "0600", "isdir": false, "ischr": false, "isblk": false, "isreg": true, "isfifo": false, "islnk": false, "issock": false, "uid": 1001, "gid": 1001, "size": 9567, "inode": 6553734, "dev": 64772, "nlink": 1, "atime": 1663162307.594887, "mtime": 1662108268.8151002, "ctime": 1662108269.4020917, "wusr": true, "rusr": true, "xusr": false, "wgrp": false, "rgrp": false, "xgrp": false, "woth": false, "roth": false, "xoth": false, "isuid": false, "isgid": false, "blocks": 24, "block\_size": 4096, "device\_type": 0, "readable": true, "writeable": true, "executable": false, "pw\_name": "postgres", "gr\_name": "postgres", "checksum": "c25840eb364bd104b796679eef8082ad6887bae3", "mimetype": "text/x-shellscript", "charset": "us-ascii", "version": "3998925608", "attributes": , "attr\_flags": ""}, "invocation": {"module\_args": {"path": "/opt/db/postgres/tools/ba

ck

> up/backup\_postgres10.sh", "follow": false, "get\_checksum": true, "checksum\_algorithm": "sha1", "get\_md5": false, "get\_mime": true, "get\_attributes": true}}}\r\n', b'Shared connection to server.intern closed.\r\n')  
> Using module file /usr/lib/python3.8/site-packages/ansible/modules/file.py  
> \<server.intern\> PUT /home/username/.ansible/tmp/ansible-local-2675679n2q7xy3g/tmpafre2fjl TO /var/tmp/ansible-tmp-1663162681.3683012-2675848-43258002595610/AnsiballZ\_file.py  
> \<server.intern\> SSH: EXEC sftp -b - -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o 'ControlPath="/home/username/.ansible/cp/8ca9f05e1f"' '[server.intern]'  
> \<server.intern\> (0, b'sftp\> put /home/username/.ansible/tmp/ansible-local-2675679n2q7xy3g/tmpafre2fjl /var/tmp/ansible-tmp-1663162681.3683012-2675848-43258002595610/AnsiballZ\_file.py\n', b'')  
> \<server.intern\> ESTABLISH SSH CONNECTION FOR USER: None  
> \<server.intern\> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o 'ControlPath="/home/username/.ansible/cp/8ca9f05e1f"' server.intern '/bin/sh -c '"'"'setfacl -m u:postgres:r-x /var/tmp/ansible-tmp-1663162681.3683012-2675848-43258002595610/ /var/tmp/ansible-tmp-1663162681.3683012-2675848-43258002595610/AnsiballZ\_file.py && sleep 0'"'"''  
> \<server.intern\> (0, b'', b'')  
> \<server.intern\> ESTABLISH SSH CONNECTION FOR USER: None  
> \<server.intern\> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o 'ControlPath="/home/username/.ansible/cp/8ca9f05e1f"' -tt server.intern '/bin/sh -c '"'"'sudo -H -S -n -u postgres /bin/sh -c '"'"'"'"'"'"'"'"'echo BECOME-SUCCESS-qttckqlufxsixkcjmiiesbbvfedjlfuy ; /usr/libexec/platform-python /var/tmp/ansible-tmp-1663162681.3683012-2675848-43258002595610/AnsiballZ\_file.py'"'"'"'"'"'"'"'"' && sleep 0'"'"''  
> Escalation succeeded  
> \<server.intern\> (0, b'\r\n{"path": "/opt/db/postgres/tools/backup/backup\_postgres10.sh", "changed": false, "diff": {"before": {"path": "/opt/db/postgres/tools/backup/backup\_postgres10.sh"}, "after": {"path": "/opt/db/postgres/tools/backup/backup\_postgres10.sh"}}, "uid": 1001, "gid": 1001, "owner": "postgres", "group": "postgres", "mode": "0600", "state": "file", "secontext": "system\_u:object\_r:usr\_t:s0", "size": 9567, "invocation": {"module\_args": {"owner": "postgres", "group": "postgres", "mode": 384, "dest": "/opt/db/postgres/tools/backup/backup\_postgres10.sh", "\_original\_basename": "backup\_postgres10.sh.j2", "recurse": false, "state": "file", "path": "/opt/db/postgres/tools/backup/backup\_postgres10.sh", "force": false, "follow": true, "modification\_time\_format": "%Y%m%d%H%M.%S", "access\_time\_format": "%Y%m%d%H%M.%S", "unsafe\_writes": false, "\_diff\_peek": null, "src": null, "modification\_time": null, "access\_time": null, "seuser": null, "serole": null, "selevel": null, "setype":

nu
