Can't filter Find togheter with with_sequence

23 views
Skip to first unread message

Cornelis van Ginkel

unread,
Mar 25, 2021, 9:53:48 AM3/25/21
to Ansible Project
Hi All,

I'm new here and have an issue that gives me a headache ;-)
I'm pretty new to ansible although I did the RH294 course.
Here's my playbook:

---
- name: count files and get oldest file
  hosts: all
  vars:
    - tomcat_home_dir: /data/tmp
    - folder_path: temp
  tasks:

    - name: Find how many tomcat instances we have
      find:
        paths: "{{ tomcat_home_dir }}"
        patterns: "tomcat*"
        recurse: no
        excludes: "tomcat"
        file_type: directory
      register: tmp_tomcat_dirs

    - name: debug
      debug:
        msg: "{{ tmp_tomcat_dirs.matched }}"

    - name: find files
      find:
        paths: "{{ tomcat_home_dir }}/tomcat{{ item }}/{{ folder_path }}"
        age: 90d
        recurse: yes
      with_sequence: start=1 end={{ tmp_tomcat_dirs.matched }}
      register: find_result

    - name: result
      debug:
         msg: "{{ find_result.matched }}"


which results in an error:
TASK [result] **************************************************************************************************************************************************************************************************
Thursday 25 March 2021  14:42:43 +0100 (0:00:02.455)       0:00:09.868 ********
fatal: [tstcgttoml01]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'matched'\n\nThe error appears to have been in '/home/people/cginkel/automation_projects/playbooks/check_tmp_folder_tomcat/playbook.yml': line 30, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: result\n      ^ here\n"}


I understand the error, but why can't I use .matched all of a sudden?
If I run it without anything or just with .results it will give a list with all information about the files it finds. How do I get the attribute matched out of that?

here's an example list:

{
                "_ansible_ignore_errors": null,
                "_ansible_item_label": "3",
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "_ansible_parsed": true,
                "changed": false,
                "examined": 4,
                "failed": false,
                "files": [],
                "invocation": {
                    "module_args": {
                        "age": "90d",
                        "age_stamp": "mtime",
                        "contains": null,
                        "depth": null,
                        "excludes": null,
                        "file_type": "file",
                        "follow": false,
                        "get_checksum": false,
                        "hidden": false,
                        "paths": [
                            "/data/tmp/tomcat3/temp"
                        ],
                        "patterns": [
                            "*"
                        ],
                        "recurse": true,
                        "size": null,
                        "use_regex": false
                    }
                },
                "item": "3",
                "matched": 0,
                "msg": ""


Also, I am on version 2.6.6 and python 2.6.6 and can't use the map or selectattr filters.... which is a bummer, but it's a production machine I can't just upgrade.
The must be an easy way, but I spend hours reading the docs and forums.

Any help would be appreciated. If I can get around it without the with_sequence I would be glad to hear it on how to do this

With kind regards,

Cornelis


Brian Coca

unread,
Mar 26, 2021, 10:41:23 AM3/26/21
to Ansible Project
When using a loop your normal results are under a 'results' list, you
can still used matched, but now you have ONE per item in the mentioned
list:
debug:
msg: "{{ find_result.resuilts[0].matched }}"

^ will give you 'matched' for the first item

to get all matches:

debug: msg={{ find_result.results|map(attribute='matched')|list }}

^ will show matches per item in a list

change `list` for `sum` to get a total.



--
----------
Brian Coca

Cornelis van Ginkel

unread,
Mar 27, 2021, 3:14:33 PM3/27/21
to ansible...@googlegroups.com
Thanks for the input, I will give that a go.

With kind regards,

Cornelis

Op vr 26 mrt. 2021 15:41 schreef Brian Coca <bc...@redhat.com>:
--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/ugXq5aiPeio/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CACVha7co7BcKPFnmy%3Du_xjgeR0ST8ZkcVcfPPvP9JMtsiM_KRQ%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages