Ansible:在嵌套字典变量中查找属性

Ansible: find attribute in nested dictionary variables

我使用来自 https://ibm.github.io/ansible-power-aix/modules/mpio.html 的模块 mpio。

结果如下所示:

ok: [hostX] => {
    "ansible_facts.mpio.paths": {
        "hdisk10": {
            "fscsi0": {
                "500507680b256624,9000000000000": {
                    "path_id": 18,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b256625,9000000000000": {
                    "path_id": 19,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b25c93e,9000000000000": {
                    "path_id": 16,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b25c93f,9000000000000": {
                    "path_id": 17,
                    "path_status": "Available",
                    "status": "Enabled"
                }
            },
            "fscsi1": {
                "500507680b266624,9000000000000": {
                    "path_id": 22,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b266625,9000000000000": {
                    "path_id": 23,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b26c93e,9000000000000": {
                    "path_id": 20,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b26c93f,9000000000000": {
                    "path_id": 21,
                    "path_status": "Available",
                    "status": "Enabled"
                }
            },
            "fscsi2": {
                "500507680b256624,9000000000000": {
                    "path_id": 26,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b256625,9000000000000": {
                    "path_id": 27,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b25c93e,9000000000000": {
                    "path_id": 24,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b25c93f,9000000000000": {
                    "path_id": 25,
                    "path_status": "Available",
                    "status": "Enabled"
                }
            },
            "fscsi3": {
                "500507680b266624,9000000000000": {
                    "path_id": 30,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b266625,9000000000000": {
                    "path_id": 31,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b26c93e,9000000000000": {
                    "path_id": 28,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b26c93f,9000000000000": {
                    "path_id": 29,
                    "path_status": "Available",
                    "status": "Enabled"
                }
            }
        },
        "hdisk11": {
            "fscsi0": {
                "500507680b256624,17000000000000": {
                    "path_id": 18,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b256625,17000000000000": {
                    "path_id": 19,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b25c93e,17000000000000": {
                    "path_id": 16,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b25c93f,17000000000000": {
                    "path_id": 17,
                    "path_status": "Available",
                    "status": "Enabled"
                }
            },
            "fscsi1": {
                "500507680b266624,17000000000000": {
                    "path_id": 22,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b266625,17000000000000": {
                    "path_id": 23,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b26c93e,17000000000000": {
                    "path_id": 20,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b26c93f,17000000000000": {
                    "path_id": 21,
                    "path_status": "Available",
                    "status": "Enabled"
                }
            },
            "fscsi2": {
                "500507680b256624,17000000000000": {
                    "path_id": 26,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b256625,17000000000000": {
                    "path_id": 27,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b25c93e,17000000000000": {
                    "path_id": 24,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b25c93f,17000000000000": {
                    "path_id": 25,
                    "path_status": "Available",
                    "status": "Enabled"
                }
            },
            "fscsi3": {
                "500507680b266624,17000000000000": {
                    "path_id": 30,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b266625,17000000000000": {
                    "path_id": 31,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b26c93e,17000000000000": {
                    "path_id": 28,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b26c93f,17000000000000": {
                    "path_id": 29,
                    "path_status": "Available",
                    "status": "Enabled"
                }
            }
        },
        "hdisk12": {
            "fscsi0": {
                "500507680b256624,18000000000000": {
                    "path_id": 18,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b256625,18000000000000": {
                    "path_id": 19,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b25c93e,18000000000000": {
                    "path_id": 16,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b25c93f,18000000000000": {
                    "path_id": 17,
                    "path_status": "Available",
                    "status": "Enabled"
                }
            },
            "fscsi1": {
                "500507680b266624,18000000000000": {
                    "path_id": 22,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b266625,18000000000000": {
                    "path_id": 23,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b26c93e,18000000000000": {
                    "path_id": 20,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b26c93f,18000000000000": {
                    "path_id": 21,
                    "path_status": "Available",
                    "status": "Enabled"
                }
            },
            "fscsi2": {
                "500507680b256624,18000000000000": {
                    "path_id": 26,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b256625,18000000000000": {
                    "path_id": 27,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b25c93e,18000000000000": {
                    "path_id": 24,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b25c93f,18000000000000": {
                    "path_id": 25,
                    "path_status": "Available",
                    "status": "Enabled"
                }
            },
            "fscsi3": {
                "500507680b266624,18000000000000": {
                    "path_id": 30,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b266625,18000000000000": {
                    "path_id": 31,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b26c93e,18000000000000": {
                    "path_id": 28,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b26c93f,18000000000000": {
                    "path_id": 29,
                    "path_status": "Available",
                    "status": "Enabled"
                }
            }
        },
        "hdisk13": {
            "fscsi0": {
                "500507680b256624,19000000000000": {
                    "path_id": 18,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b256625,19000000000000": {
                    "path_id": 19,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b25c93e,19000000000000": {
                    "path_id": 16,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b25c93f,19000000000000": {
                    "path_id": 17,
                    "path_status": "Available",
                    "status": "Enabled"
                }
            },
            "fscsi1": {
                "500507680b266624,19000000000000": {
                    "path_id": 22,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b266625,19000000000000": {
                    "path_id": 23,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b26c93e,19000000000000": {
                    "path_id": 20,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b26c93f,19000000000000": {
                    "path_id": 21,
                    "path_status": "Available",
                    "status": "Enabled"
                }
            },
            "fscsi2": {
                "500507680b256624,19000000000000": {
                    "path_id": 26,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b256625,19000000000000": {
                    "path_id": 27,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b25c93e,19000000000000": {
                    "path_id": 24,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b25c93f,19000000000000": {
                    "path_id": 25,
                    "path_status": "Available",
                    "status": "Enabled"
                }
            },
            "fscsi3": {
                "500507680b266624,19000000000000": {
                    "path_id": 30,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b266625,19000000000000": {
                    "path_id": 31,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b26c93e,19000000000000": {
                    "path_id": 28,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b26c93f,19000000000000": {
                    "path_id": 29,
                    "path_status": "Available",
                    "status": "Enabled"
                }
            }
        },
        "hdisk14": {
            "fscsi0": {
                "500507680b256624,1a000000000000": {
                    "path_id": 18,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b256625,1a000000000000": {
                    "path_id": 19,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b25c93e,1a000000000000": {
                    "path_id": 16,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b25c93f,1a000000000000": {
                    "path_id": 17,
                    "path_status": "Available",
                    "status": "Enabled"
                }
            },
            "fscsi1": {
                "500507680b266624,1a000000000000": {
                    "path_id": 22,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b266625,1a000000000000": {
                    "path_id": 23,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b26c93e,1a000000000000": {
                    "path_id": 20,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b26c93f,1a000000000000": {
                    "path_id": 21,
                    "path_status": "Available",
                    "status": "Enabled"
                }
            },
            "fscsi2": {
                "500507680b256624,1a000000000000": {
                    "path_id": 26,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b256625,1a000000000000": {
                    "path_id": 27,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b25c93e,1a000000000000": {
                    "path_id": 24,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b25c93f,1a000000000000": {
                    "path_id": 25,
                    "path_status": "Available",
                    "status": "Enabled"
                }
            },
            "fscsi3": {
                "500507680b266624,1a000000000000": {
                    "path_id": 30,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b266625,1a000000000000": {
                    "path_id": 31,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b26c93e,1a000000000000": {
                    "path_id": 28,
                    "path_status": "Available",
                    "status": "Enabled"
                },
                "500507680b26c93f,1a000000000000": {
                    "path_id": 29,
                    "path_status": "Available",
                    "status": "Enabled"
                }
            }
        }
    }
}

我不知道 hdiskXfscsiX 路径的名称(例如 500507680b256624,9000000000000)。它们在每个主机上可能不同。

我想用 path_status 搜索 hdisX: Defined, 因为我想搜索具有任何 Defined 路径的主机。像这样:

 - name: Host
   debug:
     var: inventory_hostname
   when: "{{ ansible_facts.mpio.paths.*.*.*.path_status == 'Defined'}}"

我不知道如何在嵌套字典变量中查找属性。谁能给个提示?

希望以下内容能够满足您的要求,并能为您提供足够的线索,帮助您了解您可能想要搜索、研究和使用的一组过滤器。

我基本上将您数据中的所有 path_status 条目减少为符合我们标准的唯一值的单个一维列表,并检查其长度是否等于 1(即至少有一个 path_status 在主机的整个数据中包含 'Defined')。

以下剧本:

---
- hosts: localhost
  gather_facts: false

  vars:
    ansible_facts:
      mpio:
        # Your orig data on a single line for playbook readability
        # Modified with two positive results for below tests.
        paths: {"hdisk10":{"fscsi0":{"500507680b256624,9000000000000":{"path_id":18,"path_status":"Defined","status":"Enabled"},"500507680b256625,9000000000000":{"path_id":19,"path_status":"Available","status":"Enabled"},"500507680b25c93e,9000000000000":{"path_id":16,"path_status":"Available","status":"Enabled"},"500507680b25c93f,9000000000000":{"path_id":17,"path_status":"Available","status":"Enabled"}},"fscsi1":{"500507680b266624,9000000000000":{"path_id":22,"path_status":"Available","status":"Enabled"},"500507680b266625,9000000000000":{"path_id":23,"path_status":"Available","status":"Enabled"},"500507680b26c93e,9000000000000":{"path_id":20,"path_status":"Defined","status":"Enabled"},"500507680b26c93f,9000000000000":{"path_id":21,"path_status":"Available","status":"Enabled"}},"fscsi2":{"500507680b256624,9000000000000":{"path_id":26,"path_status":"Available","status":"Enabled"},"500507680b256625,9000000000000":{"path_id":27,"path_status":"Available","status":"Enabled"},"500507680b25c93e,9000000000000":{"path_id":24,"path_status":"Available","status":"Enabled"},"500507680b25c93f,9000000000000":{"path_id":25,"path_status":"Available","status":"Enabled"}},"fscsi3":{"500507680b266624,9000000000000":{"path_id":30,"path_status":"Available","status":"Enabled"},"500507680b266625,9000000000000":{"path_id":31,"path_status":"Available","status":"Enabled"},"500507680b26c93e,9000000000000":{"path_id":28,"path_status":"Available","status":"Enabled"},"500507680b26c93f,9000000000000":{"path_id":29,"path_status":"Available","status":"Enabled"}}},"hdisk11":{"fscsi0":{"500507680b256624,17000000000000":{"path_id":18,"path_status":"Available","status":"Enabled"},"500507680b256625,17000000000000":{"path_id":19,"path_status":"Available","status":"Enabled"},"500507680b25c93e,17000000000000":{"path_id":16,"path_status":"Available","status":"Enabled"},"500507680b25c93f,17000000000000":{"path_id":17,"path_status":"Available","status":"Enabled"}},"fscsi1":{"500507680b266624,17000000000000":{"path_id":22,"path_status":"Available","status":"Enabled"},"500507680b266625,17000000000000":{"path_id":23,"path_status":"Available","status":"Enabled"},"500507680b26c93e,17000000000000":{"path_id":20,"path_status":"Available","status":"Enabled"},"500507680b26c93f,17000000000000":{"path_id":21,"path_status":"Available","status":"Enabled"}},"fscsi2":{"500507680b256624,17000000000000":{"path_id":26,"path_status":"Available","status":"Enabled"},"500507680b256625,17000000000000":{"path_id":27,"path_status":"Available","status":"Enabled"},"500507680b25c93e,17000000000000":{"path_id":24,"path_status":"Available","status":"Enabled"},"500507680b25c93f,17000000000000":{"path_id":25,"path_status":"Available","status":"Enabled"}},"fscsi3":{"500507680b266624,17000000000000":{"path_id":30,"path_status":"Available","status":"Enabled"},"500507680b266625,17000000000000":{"path_id":31,"path_status":"Available","status":"Enabled"},"500507680b26c93e,17000000000000":{"path_id":28,"path_status":"Available","status":"Enabled"},"500507680b26c93f,17000000000000":{"path_id":29,"path_status":"Available","status":"Enabled"}}},"hdisk12":{"fscsi0":{"500507680b256624,18000000000000":{"path_id":18,"path_status":"Available","status":"Enabled"},"500507680b256625,18000000000000":{"path_id":19,"path_status":"Available","status":"Enabled"},"500507680b25c93e,18000000000000":{"path_id":16,"path_status":"Available","status":"Enabled"},"500507680b25c93f,18000000000000":{"path_id":17,"path_status":"Available","status":"Enabled"}},"fscsi1":{"500507680b266624,18000000000000":{"path_id":22,"path_status":"Available","status":"Enabled"},"500507680b266625,18000000000000":{"path_id":23,"path_status":"Available","status":"Enabled"},"500507680b26c93e,18000000000000":{"path_id":20,"path_status":"Available","status":"Enabled"},"500507680b26c93f,18000000000000":{"path_id":21,"path_status":"Available","status":"Enabled"}},"fscsi2":{"500507680b256624,18000000000000":{"path_id":26,"path_status":"Available","status":"Enabled"},"500507680b256625,18000000000000":{"path_id":27,"path_status":"Available","status":"Enabled"},"500507680b25c93e,18000000000000":{"path_id":24,"path_status":"Available","status":"Enabled"},"500507680b25c93f,18000000000000":{"path_id":25,"path_status":"Available","status":"Enabled"}},"fscsi3":{"500507680b266624,18000000000000":{"path_id":30,"path_status":"Available","status":"Enabled"},"500507680b266625,18000000000000":{"path_id":31,"path_status":"Available","status":"Enabled"},"500507680b26c93e,18000000000000":{"path_id":28,"path_status":"Available","status":"Enabled"},"500507680b26c93f,18000000000000":{"path_id":29,"path_status":"Available","status":"Enabled"}}},"hdisk13":{"fscsi0":{"500507680b256624,19000000000000":{"path_id":18,"path_status":"Available","status":"Enabled"},"500507680b256625,19000000000000":{"path_id":19,"path_status":"Available","status":"Enabled"},"500507680b25c93e,19000000000000":{"path_id":16,"path_status":"Available","status":"Enabled"},"500507680b25c93f,19000000000000":{"path_id":17,"path_status":"Available","status":"Enabled"}},"fscsi1":{"500507680b266624,19000000000000":{"path_id":22,"path_status":"Available","status":"Enabled"},"500507680b266625,19000000000000":{"path_id":23,"path_status":"Available","status":"Enabled"},"500507680b26c93e,19000000000000":{"path_id":20,"path_status":"Available","status":"Enabled"},"500507680b26c93f,19000000000000":{"path_id":21,"path_status":"Available","status":"Enabled"}},"fscsi2":{"500507680b256624,19000000000000":{"path_id":26,"path_status":"Available","status":"Enabled"},"500507680b256625,19000000000000":{"path_id":27,"path_status":"Available","status":"Enabled"},"500507680b25c93e,19000000000000":{"path_id":24,"path_status":"Available","status":"Enabled"},"500507680b25c93f,19000000000000":{"path_id":25,"path_status":"Available","status":"Enabled"}},"fscsi3":{"500507680b266624,19000000000000":{"path_id":30,"path_status":"Available","status":"Enabled"},"500507680b266625,19000000000000":{"path_id":31,"path_status":"Available","status":"Enabled"},"500507680b26c93e,19000000000000":{"path_id":28,"path_status":"Available","status":"Enabled"},"500507680b26c93f,19000000000000":{"path_id":29,"path_status":"Available","status":"Enabled"}}},"hdisk14":{"fscsi0":{"500507680b256624,1a000000000000":{"path_id":18,"path_status":"Available","status":"Enabled"},"500507680b256625,1a000000000000":{"path_id":19,"path_status":"Available","status":"Enabled"},"500507680b25c93e,1a000000000000":{"path_id":16,"path_status":"Available","status":"Enabled"},"500507680b25c93f,1a000000000000":{"path_id":17,"path_status":"Available","status":"Enabled"}},"fscsi1":{"500507680b266624,1a000000000000":{"path_id":22,"path_status":"Available","status":"Enabled"},"500507680b266625,1a000000000000":{"path_id":23,"path_status":"Available","status":"Enabled"},"500507680b26c93e,1a000000000000":{"path_id":20,"path_status":"Available","status":"Enabled"},"500507680b26c93f,1a000000000000":{"path_id":21,"path_status":"Available","status":"Enabled"}},"fscsi2":{"500507680b256624,1a000000000000":{"path_id":26,"path_status":"Available","status":"Enabled"},"500507680b256625,1a000000000000":{"path_id":27,"path_status":"Available","status":"Enabled"},"500507680b25c93e,1a000000000000":{"path_id":24,"path_status":"Available","status":"Enabled"},"500507680b25c93f,1a000000000000":{"path_id":25,"path_status":"Available","status":"Enabled"}},"fscsi3":{"500507680b266624,1a000000000000":{"path_id":30,"path_status":"Available","status":"Enabled"},"500507680b266625,1a000000000000":{"path_id":31,"path_status":"Available","status":"Enabled"},"500507680b26c93e,1a000000000000":{"path_id":28,"path_status":"Available","status":"Enabled"},"500507680b26c93f,1a000000000000":{"path_id":29,"path_status":"Available","status":"Enabled"}}}}

  tasks:
    - name: Show the data (run with -v to see)
      debug:
        var: ansible_facts.mpio.paths
        verbosity: 1

    # The following requires:
    # - `pip install jmespath` on the controller
    # - if using ansible >= 2.10 => `ansible-galaxy collection install community.general`
    - name: Check we have at least one path defined with json_query
      vars:
        jmesquery: >-
          *[].*[].*[] | [?path_status == 'Defined'].path_status
      debug:
        msg: "There is at least one defined path on {{ inventory_hostname }}"
      when: >-
        ansible_facts.mpio.paths
        | json_query(jmesquery)
        | unique
        | length == 1

    # Although not really sexy for this particular case,
    # the following should work out of the box with any decent ansible version
    - name: Check we have at least one path defined with stock filters
      debug:
        msg: "There is at least one defined path on {{ inventory_hostname }}"
      when: >-
        ansible_facts.mpio.paths
        | dict2items
        | map(attribute='value')
        | map('dict2items')
        | flatten
        | map(attribute='value')
        | map('dict2items')
        | flatten
        | selectattr('value.path_status', '==', 'Defined')
        | map(attribute='value.path_status')
        | unique
        | length == 1

给出:


PLAY [localhost] **************************************************************************************************************************************************************************************************************

TASK [Show the data (run with -v to see)] *************************************************************************************************************************************************************************************
skipping: [localhost]

TASK [Check we have at least one path defined with json_query] ****************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "There is at least one defined path on localhost"
}

TASK [Check we have at least one path defined with stock filters] *************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "There is at least one defined path on localhost"
}

PLAY RECAP ********************************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0