Reputation: 382
I use this task to access files:
- name: Find logs files
find:
paths: /var/log/
patterns: "*.log"
recurse: yes
register: list_matched
- debug:
var: list_matched.files
Output is:
ok: [127.0.0.1] => {
"changed": false,
"examined": 17,
"files": [
{
"atime": 1576496687.5594354,
"ctime": 1576496687.5594354,
"dev": 2049,
"gid": 0,
"gr_name": "root",
"inode": 922589,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"mode": "",
"mtime": ,
"nlink": 1,
"path": "/var/log/mikrotik/mikrotik.log",
"pw_name": "root",
"rgrp": true,
"roth": true,
"rusr": true,
"size": 0,
"uid": 0,
"wgrp": false,
"woth": false,
"wusr": true,
"xgrp": false,
"xoth": false,
"xusr": false
},
How can I get path parameter? I was trying also something like this:
- debug:
var: list_matched.files.path
But an error occured:
ok: [127.0.0.1] => {
"files_matched.path": "VARIABLE IS NOT DEFINED!: 'dict object' has no attribute 'path'"
I want to capture path parameter, but no idea how. It was also hard to find some information about this nested debugging case.
Upvotes: 1
Views: 1012
Reputation: 3790
There is an open bracket "[" after "files:" so you need to go that position on the array before going to path.
Try:
var: list_matched.files[0].path
Or:
var: list_matched.files[0]["path"]
To answer the comment, "how should I use it when I get more paths?" try getting the length of the array of "list_matched.files" and looping through it like this:
many_paths = []
for i in range(len(list_matched.files)):
print(list_matched.files[i].path)
many_paths.append(list_matched.files[i].path)
print(many_paths)
Maybe you want to save them into an array of paths.
Upvotes: 1
Reputation: 338
Find returns a dict
of values, so you must iterate over the list.
- name: Show file paths
debug:
msg: "{{ item.path }}"
with_items: "{{ list_matched.files }}"
Upvotes: 0