# Template module.  MODE all wrong when using loop

**URL:** <https://forum.ansible.com/t/template-module-mode-all-wrong-when-using-loop/25563>\
**Category:** Ansible Project\
**Created:** [September 21, 2017, 5:54pm UTC](https://forum.ansible.com/t/template-module-mode-all-wrong-when-using-loop/25563 "2017-09-21T17:54:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [September 21, 2017, 5:54pm UTC](https://forum.ansible.com/t/template-module-mode-all-wrong-when-using-loop/25563/1 "2017-09-21T17:54:21Z")

</div>

When using the template module within a loop, my mode comes out all weird/wrong. When using stand-alone it works:

`

- name: test  
template:  
src: “files/sssd.conf”  
dest: “/etc/sssd/sssd.conf”  
mode: 0600  
group: root  
owner: root

- name: Add/Update required files  
template:  
src: “{{item.src}}”  
dest: “{{item.dst}}”  
owner: root  
group: root  
mode: “{{item.mode}}”  
with\_items:

- {src: “files/sssd.conf”,  
dst: “/etc/sssd/sssd.conf”,  
mode: 0600}

- {src: “files/krb5.conf”,  
dst: “/etc/krb5.conf”,  
mode: 0644}

- {name: “dc1.cer”,  
src: “files/dc1.cer”,  
dst: “/etc/ssl/certs/dc1.cer”,  
mode: 0644}  
notify: restart sssd

`

Results :

`  
TASK [ldap\_users : test] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: [ansibletest-oel6]

TASK [ldap\_users : Add/Update required files] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
failed: [ansibletest-oel6] (item={u’src’: u’files/sssd.conf’, u’dst’: u’/etc/sssd/sssd.conf’, u’mode’: 384}) =\> {“details”: “bad symbolic permission for mode: 384”, “failed”: true, “gid”: 0, “group”: “root”, “item”: {“dst”: “/etc/sssd/sssd.conf”, “mode”: 384, “src”: “files/sssd.conf”}, “mode”: “0416”, “msg”: “mode must be in octal or symbolic form”, “owner”: “root”, “path”: “/etc/sssd/sssd.conf”, “size”: 1445, “state”: “file”, “uid”: 0}  
ok: [ansibletest-oel6] =\> (item={u’src’: u’files/krb5.conf’, u’dst’: u’/etc/krb5.conf’, u’mode’: 420})  
ok: [ansibletest-oel6] =\> (item={u’src’: u’files/dc1.cer’, u’dst’: u’/etc/ssl/certs/dc1.cer’, u’name’: u’dc1.cer’, u’mode’: 420})

`

As you can see from the above, the test works (confirmed that the mode is truly 0600 on the target server). From the second part of the results, you can see that it is trying to tweak the mode different than the test. The first one it bombs on (mode 0600) and the second two (0644) it changes to 420… like a umask is being applied.

Is this a bug? or am I doing something wrong?

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [September 21, 2017, 6:03pm UTC](https://forum.ansible.com/t/template-module-mode-all-wrong-when-using-loop/25563/2 "2017-09-21T18:03:15Z")

</div>

If I do the following it works (putting quotes around the mode). Can anybody explain to my why?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [September 21, 2017, 6:13pm UTC](https://forum.ansible.com/t/template-module-mode-all-wrong-when-using-loop/25563/3 "2017-09-21T18:13:14Z")

</div>

Yes, Ansible interprets number starting with 0 as octal number.  
0644 = 6\*8^2 + 4\*8^1 + 4\*8^0 = 420 decimal.

So to avoid this conversion you need to use the quotes.

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [September 21, 2017, 6:15pm UTC](https://forum.ansible.com/t/template-module-mode-all-wrong-when-using-loop/25563/4 "2017-09-21T18:15:46Z")

</div>

Perfect! Thank you Kai Stian Olstad

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [September 21, 2017, 6:17pm UTC](https://forum.ansible.com/t/template-module-mode-all-wrong-when-using-loop/25563/5 "2017-09-21T18:17:32Z")

</div>

Actually, that brings up a follow-up question… why doesn’t it treat it that way in the first example, when I don’t use a loop?

---

<div class="post-metadata">

**Author:** ![tkuratom](https://avatars.discourse-cdn.com/v4/letter/t/b19c9b/32.png) [@tkuratom](https://forum.ansible.com/u/tkuratom)\
**Post date:** [September 22, 2017, 8:33pm UTC](https://forum.ansible.com/t/template-module-mode-all-wrong-when-using-loop/25563/6 "2017-09-22T20:33:51Z")

</div>

let me explain this a little better....

You want to set the mode to octal 0600 which is the same as decimal  
420. So either of these would be fine:

&nbsp;&nbsp;mode: 0600  
&nbsp;&nbsp;mode: 420

Ansible's template module also accepts string versions of the mode.  
The strings are similar to what chmod would accept on the commandline.  
So this is fine:

&nbsp;&nbsp;mode: "0600"

Since chmod accepts a string "600" as a synonym for the above, if  
Ansible gets that string it will also accept it:  
&nbsp;&nbsp;mode: "600"

Now, for your second example, you aren't giving ansible any of the  
above values. Let me show you what's happening.

First, the mode is set in your data structure as 0600 octal. Which is  
420 decimal as Kai said. Then the value is getting substituted into  
the task parameters like this:  
&nbsp;&nbsp;mode: "{{ item.mode }}"

So you end up with:  
&nbsp;&nbsp;mode: "420"

This is the string, 420. not the decimal number 420. And that's why  
Ansible sets the mode to the wrong value.

-Toshio
