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"
]
}
]
}