# Question: how to use variable in with\_sequence, casting to int results in zero value

**URL:** <https://forum.ansible.com/t/question-how-to-use-variable-in-with-sequence-casting-to-int-results-in-zero-value/23134>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [November 30, 2016, 8:52am UTC](https://forum.ansible.com/t/question-how-to-use-variable-in-with-sequence-casting-to-int-results-in-zero-value/23134 "2016-11-30T08:52:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![frank1](https://avatars.discourse-cdn.com/v4/letter/f/a87d85/32.png) [@frank1](https://forum.ansible.com/u/frank1)\
**Post date:** [November 30, 2016, 8:52am UTC](https://forum.ansible.com/t/question-how-to-use-variable-in-with-sequence-casting-to-int-results-in-zero-value/23134/1 "2016-11-30T08:52:49Z")

</div>

I am trying to process the result from the find module. The result contains 3 file paths which i would like to process in a loop.

The number of files depends on the number of files found in the directories and is set by result.matched

Using the with\_sequence loop should do the trick, i think. But i ran into a problem.

with\_sequence: start=0 end={{ variable }}, expects {{ variable }} to be an integer. Casting {{ variable | int}} results in a zero value, which should be 3.

Any suggestions how to solve or work around my issue?

After casting the variable correctly to an integer, off course i still have to subtract 1 to set the correct index value for the results array.

**Variables** :

file\_path: “roles/ansible-role-iamGroups/files”  
template\_path: “roles/ansible-role-iamGroups/templates”

declared\_int: 3

**Tasks** :

- name: find files  
find:  
recurse: yes  
patterns: “\*.json”  
paths: “/home/user/PycharmProjects/playbook-aws-billing/roles/ansible-role-iamGroups/templates/”  
register: result

- name: register numbers variable  
shell: “echo {{result.matched}}”  
register: number

- name: list number variable (==\> string value 3)  
debug: var=number

- name: list result hard coded (works)  
debug: “var=result.files[{{item}}].path”  
with\_sequence: “start=0 end=2 stride=1”

- name: list result with variable casted to integer (number is casted to 0, should be 3 )  
debug: “var=result.files[{{item}}].path”  
with\_sequence: “start=0 end={{ number | int }} stride=1”

- name: list result with declared integer variable minus 1 (works)  
debug: “var=result.files[{{item}}].path”  
with\_sequence: “start=0 end={{ declared\_int - 1 }} stride=1”

- name: list result without casting variable number ( results in parsing error)  
debug: “var=result.files[{{item}}].path”  
with\_sequence: “start=0 end={{ number }} stride=1”

**Output** :

ok: [aws] =\> {  
“changed”: false,  
“examined”: 25,  
“files”: [  
{  
“atime”: 1480345980.447014,  
“ctime”: 1480345980.451014,  
“dev”: 2049,  
“gid”: 1000,  
“inode”: 523227,  
“isblk”: false,  
“ischr”: false,  
“isdir”: false,  
“isfifo”: false,  
“isgid”: false,  
“islnk”: false,  
“isreg”: true,  
“issock”: false,  
“isuid”: false,  
“mode”: “0644”,  
“mtime”: 1480345980.447014,  
“nlink”: 1,  
“path”: “/home/user/PycharmProjects/playbook-aws-billing/roles/ansible-role-iamGroups/templates/prod/ReadOnly/readOnly.json”,  
“rgrp”: true,  
“roth”: true,  
“rusr”: true,  
“size”: 1655,  
“uid”: 1000,  
“wgrp”: false,  
“woth”: false,  
“wusr”: true,  
“xgrp”: false,  
“xoth”: false,  
“xusr”: false  
},  
{  
“atime”: 1480345980.523014,  
“ctime”: 1480345980.527014,  
“dev”: 2049,  
“gid”: 1000,  
“inode”: 523175,  
“isblk”: false,  
“ischr”: false,  
“isdir”: false,  
“isfifo”: false,  
“isgid”: false,  
“islnk”: false,  
“isreg”: true,  
“issock”: false,  
“isuid”: false,  
“mode”: “0644”,  
“mtime”: 1480345980.527014,  
“nlink”: 1,  
“path”: “/home/user/PycharmProjects/playbook-aws-billing/roles/ansible-role-iamGroups/templates/prod/PowerUser/iamAccess.json”,  
“rgrp”: true,  
“roth”: true,  
“rusr”: true,  
“size”: 136,  
“uid”: 1000,  
“wgrp”: false,  
“woth”: false,  
“wusr”: true,  
“xgrp”: false,  
“xoth”: false,  
“xusr”: false  
},  
{  
“atime”: 1480345980.531014,  
“ctime”: 1480345980.539014,  
“dev”: 2049,  
“gid”: 1000,  
“inode”: 523226,  
“isblk”: false,  
“ischr”: false,  
“isdir”: false,  
“isfifo”: false,  
“isgid”: false,  
“islnk”: false,  
“isreg”: true,  
“issock”: false,  
“isuid”: false,  
“mode”: “0644”,  
“mtime”: 1480345980.531014,  
“nlink”: 1,  
“path”: “/home/user/PycharmProjects/playbook-aws-billing/roles/ansible-role-iamGroups/templates/prod/PowerUser/powerUser.json”,  
“rgrp”: true,  
“roth”: true,  
“rusr”: true,  
“size”: 138,  
“uid”: 1000,  
“wgrp”: false,  
“woth”: false,  
“wusr”: true,  
“xgrp”: false,  
“xoth”: false,  
“xusr”: false  
}  
],  
“invocation”: {  
“module\_args”: {  
“age”: null,  
“age\_stamp”: “mtime”,  
“contains”: null,  
“file\_type”: “file”,  
“follow”: false,  
“get\_checksum”: false,  
“hidden”: false,  
“paths”: [  
“/home/user/PycharmProjects/playbook-aws-billing/roles/ansible-role-iamGroups/templates/”  
],  
“patterns”: [  
“\*.json”  
],  
“recurse”: true,  
“size”: null,  
“use\_regex”: false  
},  
“module\_name”: “find”  
},  
“matched”: 3,  
“msg”: “”  
}

TASK [ansible-role-iamGroups : register numbers variable] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /home/user/PycharmProjects/playbook-aws-billing/roles/ansible-role-iamGroups/tasks/main.yml:9  
Using module file /home/user/ansible/lib/ansible/modules/core/commands/command.py  
 ESTABLISH LOCAL CONNECTION FOR USER: user  
 EXEC /bin/sh -c ‘( umask 77 && mkdir -p “`echo $HOME/.ansible/tmp/ansible-tmp-1480495766.92-247180421426282`” && echo ansible-tmp-1480495766.92-247180421426282=“`echo $HOME/.ansible/tmp/ansible-tmp-1480495766.92-247180421426282`” ) && sleep 0’  
 PUT /tmp/tmpO5IsA5 TO /home/user/.ansible/tmp/ansible-tmp-1480495766.92-247180421426282/command.py  
 EXEC /bin/sh -c ‘chmod u+x /home/user/.ansible/tmp/ansible-tmp-1480495766.92-247180421426282/ /home/user/.ansible/tmp/ansible-tmp-1480495766.92-247180421426282/command.py && sleep 0’  
 EXEC /bin/sh -c ‘/usr/bin/python /home/user/.ansible/tmp/ansible-tmp-1480495766.92-247180421426282/command.py; rm -rf “/home/user/.ansible/tmp/ansible-tmp-1480495766.92-247180421426282/” \> /dev/null 2\>&1 && sleep 0’  
changed: [aws] =\> {  
“changed”: true,  
“cmd”: “echo 3”,  
“delta”: “0:00:00.003979”,  
“end”: “2016-11-30 09:49:27.332742”,  
“invocation”: {  
“module\_args”: {  
“\_raw\_params”: “echo 3”,  
“\_uses\_shell”: true,  
“chdir”: null,  
“creates”: null,  
“executable”: null,  
“removes”: null,  
“warn”: true  
},  
“module\_name”: “command”  
},  
“rc”: 0,  
“start”: “2016-11-30 09:49:27.328763”,  
“stderr”: “”,  
“stdout”: “3”,  
“stdout\_lines”: [  
“3”  
],  
“warnings”:   
}

TASK [ansible-role-iamGroups : list number variable (==\> string value 3)] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /home/user/PycharmProjects/playbook-aws-billing/roles/ansible-role-iamGroups/tasks/main.yml:13  
ok: [aws] =\> {  
“number”: {  
“changed”: true,  
“cmd”: “echo 3”,  
“delta”: “0:00:00.003979”,  
“end”: “2016-11-30 09:49:27.332742”,  
“rc”: 0,  
“start”: “2016-11-30 09:49:27.328763”,  
“stderr”: “”,  
“stdout”: “3”,  
“stdout\_lines”: [  
“3”  
],  
“warnings”:   
}  
}

TASK [ansible-role-iamGroups : list result hard coded (works)] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /home/user/PycharmProjects/playbook-aws-billing/roles/ansible-role-iamGroups/tasks/main.yml:16  
ok: [aws] =\> (item=0) =\> {  
“invocation”: {  
“module\_args”: {  
“var”: “result.files[0].path”  
},  
“module\_name”: “debug”  
},  
“item”: “0”,  
“result.files[0].path”: “/home/user/PycharmProjects/playbook-aws-billing/roles/ansible-role-iamGroups/templates/prod/ReadOnly/readOnly.json”  
}  
ok: [aws] =\> (item=1) =\> {  
“invocation”: {  
“module\_args”: {  
“var”: “result.files[1].path”  
},  
“module\_name”: “debug”  
},  
“item”: “1”,  
“result.files[1].path”: “/home/user/PycharmProjects/playbook-aws-billing/roles/ansible-role-iamGroups/templates/prod/PowerUser/iamAccess.json”  
}  
ok: [aws] =\> (item=2) =\> {  
“invocation”: {  
“module\_args”: {  
“var”: “result.files[2].path”  
},  
“module\_name”: “debug”  
},  
“item”: “2”,  
“result.files[2].path”: “/home/user/PycharmProjects/playbook-aws-billing/roles/ansible-role-iamGroups/templates/prod/PowerUser/powerUser.json”  
}

TASK [ansible-role-iamGroups : list result with variable casted to integer (number is casted to 0, should be 3 )] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /home/user/PycharmProjects/playbook-aws-billing/roles/ansible-role-iamGroups/tasks/main.yml:20  
ok: [aws] =\> (item=0) =\> {  
“invocation”: {  
“module\_args”: {  
“var”: “result.files[0].path”  
},  
“module\_name”: “debug”  
},  
“item”: “0”,  
“result.files[0].path”: “/home/user/PycharmProjects/playbook-aws-billing/roles/ansible-role-iamGroups/templates/prod/ReadOnly/readOnly.json”  
}

TASK [ansible-role-iamGroups : list result with declared integer variable minus 1 (works)] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /home/user/PycharmProjects/playbook-aws-billing/roles/ansible-role-iamGroups/tasks/main.yml:24  
ok: [aws] =\> (item=0) =\> {  
“invocation”: {  
“module\_args”: {  
“var”: “result.files[0].path”  
},  
“module\_name”: “debug”  
},  
“item”: “0”,  
“result.files[0].path”: “/home/user/PycharmProjects/playbook-aws-billing/roles/ansible-role-iamGroups/templates/prod/ReadOnly/readOnly.json”  
}  
ok: [aws] =\> (item=1) =\> {  
“invocation”: {  
“module\_args”: {  
“var”: “result.files[1].path”  
},  
“module\_name”: “debug”  
},  
“item”: “1”,  
“result.files[1].path”: “/home/user/PycharmProjects/playbook-aws-billing/roles/ansible-role-iamGroups/templates/prod/PowerUser/iamAccess.json”  
}  
ok: [aws] =\> (item=2) =\> {  
“invocation”: {  
“module\_args”: {  
“var”: “result.files[2].path”  
},  
“module\_name”: “debug”  
},  
“item”: “2”,  
“result.files[2].path”: “/home/user/PycharmProjects/playbook-aws-billing/roles/ansible-role-iamGroups/templates/prod/PowerUser/powerUser.json”  
}

TASK [ansible-role-iamGroups : list result without casting variable number ( results in parsing error)] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /home/user/PycharmProjects/playbook-aws-billing/roles/ansible-role-iamGroups/tasks/main.yml:28  
fatal: [aws]: FAILED! =\> {  
“failed”: true,  
“msg”: “unknown error parsing with\_sequence arguments: u"start=0 end={u’changed’: True, u’end’: u’2016-11-30 09:49:27.332742’, u’stdout’: u’3’, u’cmd’: u’echo 3’, u’rc’: 0, u’start’: u’2016-11-30 09:49:27.328763’, u’stderr’: u’‘, u’delta’: u’0:00:00.003979’, ‘stdout\_lines’: [u’3’], u’warnings’: } stride=1". Error was: can’t parse arg end=u"{u’changed’:" as integer”  
}  
to retry, use: --limit @/home/user/PycharmProjects/playbook-aws-billing/billing-iam-pb.retry

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
aws : ok=7 changed=1 unreachable=0 failed=1

---

<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:** [December 1, 2016, 7:02pm UTC](https://forum.ansible.com/t/question-how-to-use-variable-in-with-sequence-casting-to-int-results-in-zero-value/23134/2 "2016-12-01T19:02:11Z")

</div>

> I am trying to process the result from the find module. The result contains  
> 3 file paths which i would like to process in a loop.  
> The number of files depends on the number of files found in the directories  
> and is set by result.matched
> 
> Using the with\_sequence loop should do the trick, i think. But i ran into a  
> problem.
> 
> with\_sequence: start=0 end={{ variable }}, expects {{ variable }} to be an  
> integer. Casting {{ variable | int}} results in a zero value, which should  
> be 3.
> 
> Any suggestions how to solve or work around my issue?

Why not use with\_items?

> After casting the variable correctly to an integer, off course i still have  
> to subtract 1 to set the correct index value for the results array.
> 
> \*Variables\*:
> 
> file\_path: "roles/ansible-role-iamGroups/files"  
> template\_path: "roles/ansible-role-iamGroups/templates"
> 
> declared\_int: 3
> 
> \*Tasks\*:
> 
> - name: find files  
> &nbsp;&nbsp;find:  
> &nbsp;&nbsp;&nbsp;&nbsp;recurse: yes  
> &nbsp;&nbsp;&nbsp;&nbsp;patterns: "\*.json"  
> &nbsp;&nbsp;&nbsp;&nbsp;paths:  
> "/home/user/PycharmProjects/playbook-aws-billing/roles/ansible-role-iamGroups/templates/"  
> &nbsp;&nbsp;register: result
> 
> - name: register numbers variable  
> &nbsp;&nbsp;shell: "echo {{result.matched}}"  
> &nbsp;&nbsp;register: number
> 
> - name: list number variable (==\> string value 3)  
> &nbsp;&nbsp;debug: var=number
> 
> - name: list result hard coded (works)  
> &nbsp;&nbsp;debug: "var=result.files[{{item}}].path"  
> &nbsp;&nbsp;with\_sequence: "start=0 end=2 stride=1"

- name: list result  
&nbsp;&nbsp;&nbsp;debug: var=item.path  
&nbsp;&nbsp;&nbsp;with\_items: result.files

---

<div class="post-metadata">

**Author:** ![frank1](https://avatars.discourse-cdn.com/v4/letter/f/a87d85/32.png) [@frank1](https://forum.ansible.com/u/frank1)\
**Post date:** [December 8, 2016, 2:34pm UTC](https://forum.ansible.com/t/question-how-to-use-variable-in-with-sequence-casting-to-int-results-in-zero-value/23134/3 "2016-12-08T14:34:19Z")

</div>

Hi Kai,

Thanks for your reaction.

I think item.path won’t work because of the structure of result.files.  
result.files contains a list [..path..], [.path..] ,So in the debug var i should reference item[0].path , item[1].path

I worked my way around creating a list

# create a list of policies to be defined

- name: create list of files  
debug:  
msg: “{{ result.files | map(attribute=‘path’) | list }}”  
register: fileList
