0

I have a list of items (zip files) to where I am performing a match against specific characters, then extracting those filenames to create a new list.

With the new list, I'm performing an unzip -l on each item to count all of the files within the zip, and I'd like to sum the total results. I'm not sure how to perform the sum because there are multiple stdout indexes within the registered variable.

I'm not a developer and I'm sure there is probably a more efficient way of doing this, but this is where I've arrived based on my knowledge.

Original List

vars:
  - patches: [

      PB.nfm.21.sp1.551.pb2017091516976,
      PB.ps.21.sp1.551.pb2017091591424,
      PB.ns.21.sp1.551.pb2017091854308,
      PB.ums.21.sp1.551.pb2017091828149,
      PB.ps.21.sp1.551.pb2017091592773,
      PB.as.21.sp1.551.pb2017091555706,
      PB.as.21.sp1.551.pb2017091555707,
      PB.xsp.21.sp1.551.pb2017091891314 ]

Ansible Role (Extracting items containing 'PB.as')

- name: Count all files in all PB archives
  register: patch_total
  delegate_to: localhost
  shell: "/usr/bin/unzip -l bw/patches/{{ item }}.Linux-x86_64.zip | awk '{count = $2} END{print count}'"
   with_items: "{{ patches|select('match','PB.as.')|list }}"
- debug: var=patch_total.results

Debug Results (how do I sum the stdout for both items?)

"patch_total.results": [
        {
            "_ansible_delegated_vars": {
                "ansible_delegated_host": "localhost",
                "ansible_host": "localhost"
            },
            "_ansible_ignore_errors": null,
            "_ansible_item_result": true,
            "_ansible_no_log": false,
            "_ansible_parsed": true,
            "changed": true,
            "cmd": "/usr/bin/unzip -l bw/patches/PB.as.21.sp1.551.pb2017091555706.Linux-x86_64.zip | awk '{count = $2} END{print count}'",
            "delta": "0:00:00.006809",
            "end": "2018-02-21 13:53:08.438719",
            "failed": false,
            "invocation": {
                "module_args": {
                    "_raw_params": "/usr/bin/unzip -l bw/patches/PB.as.21.sp1.551.pb2017091555706.Linux-x86_64.zip | awk '{count = $2} END{print count}'",
                    "_uses_shell": true,
                    "chdir": null,
                    "creates": null,
                    "executable": null,
                    "removes": null,
                    "stdin": null,
                    "warn": true
                }
            },
            "item": "PB.as.21.sp1.551.pb2017091555706",
            "rc": 0,
            "start": "2018-02-21 13:53:08.431910",
            "stderr": "",
            "stderr_lines": [],
            "stdout": "231",
            "stdout_lines": [
                "231"
            ]
        },
        {
            "_ansible_delegated_vars": {
                "ansible_delegated_host": "localhost",
                "ansible_host": "localhost"
            },
            "_ansible_ignore_errors": null,
            "_ansible_item_result": true,
            "_ansible_no_log": false,
            "_ansible_parsed": true,
            "changed": true,
            "cmd": "/usr/bin/unzip -l bw/patches/PB.as.21.sp1.551.pb2017091555707.Linux-x86_64.zip | awk '{count = $2} END{print count}'",
            "delta": "0:00:00.005809",
            "end": "2018-02-21 13:53:08.577996",
            "failed": false,
            "invocation": {
                "module_args": {
                    "_raw_params": "/usr/bin/unzip -l bw/patches/PB.as.21.sp1.551.pb2017091555707.Linux-x86_64.zip | awk '{count = $2} END{print count}'",
                    "_uses_shell": true,
                    "chdir": null,
                    "creates": null,
                    "executable": null,
                    "removes": null,
                    "stdin": null,
                    "warn": true
                }
            },
            "item": "PB.as.21.sp1.551.pb2017091555707",
            "rc": 0,
            "start": "2018-02-21 13:53:08.572187",
            "stderr": "",
            "stderr_lines": [],
            "stdout": "231",
            "stdout_lines": [
                "231"
            ]
        }
    ]
}
lowtek
  • 1
  • What is the question? – techraf Feb 21 '18 at 19:13
  • How do I sum the stdout results when debugging the variable? There are two, both equaling 231. – lowtek Feb 21 '18 at 19:29
  • 1
    In other words: you want someone to write code for you, right? – techraf Feb 21 '18 at 19:46
  • Write code for me? The answer is no and I'm not really sure what you mean. I'm asking a simple question. Is there a filter that I could apply to the variable in my debug statement? I've already written my plays and roles, thanks. – lowtek Feb 21 '18 at 19:53
  • You posted some raw data and asked give me the code "to sum stdout results". ・ What specific problem do you have with that? – techraf Feb 21 '18 at 19:54
  • Perhaps I can simplify the raw data. patch_total.results[0] contains stdout results of "231" patch_total.results[1] contains stdout results of "231" How do I obtain a single sum of these two outputs when running the debug statement, instead of having to debug each index? I never know how many indexes there might be because the original list always differs – lowtek Feb 21 '18 at 19:56

0 Answers0