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":