Ansible - When condition on set_fact and array value

Viewed 38

I have this result:

ok: [localhost] => {
    "register_xpathwithbranches": {
        "changed": false,
        "msg": "All items completed",
        "results": [
            {
                "actions": {
                    "namespaces": {},
                    "state": "present",
                    "xpath": "//branches//name"
                },
                "ansible_loop_var": "validpath",
                "changed": false,
                "count": 1,
                "failed": false,
                "invocation": {
                    "module_args": {
                        "path": "mypath/config.xml",
                    }
                },
                "msg": "found 1 nodes",
                "validpath": "Anotherpath"
            },
            {
                "actions": {
                    "namespaces": {},
                    "state": "present",
[...]

And it starts over with a new action / namespace / state and so on. Lot of them, a big list. So I have lots of invocation->module_args->path, and different "count" that ranges between 0 and 1.

I'd like to find a way to get all the paths but ONLY when count > 0

I know I am pretty close with something like:

    - name: Transform the var as a list
      set_fact:
        xpathwithbranches_list: "{{ register_xpathwithbranches.results | map(attribute='invocation.module_args.path') |list }}"

With that I can get a list of all the path, but I cannot filter on the (count > 0). I've been running my jenkins jobs for the 228 times and I don't know what to do anymore.

I am not even sure if I should really set_fact now or just use a register instead looping on something. Or with_items (can't get the difference).

What do you think? Thanks.

1 Answers

filter on the (count > 0).

Sounds like a job for | selectattr

  - debug:
      msg: >-
       {{ register_xpathwithbranches.results | selectattr("count", "ne", 0)
       | map(attribute='invocation.module_args.path') |list }}

produces

ok: [localhost] => {
    "msg": [
        "mypath/config.xml"
    ]

for the very limited sample data you provided

Related