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

Posted

技术标签:

【中文标题】Ansible:在嵌套字典变量中查找属性【英文标题】:Ansible: find attribute in nested dictionary variables 【发布时间】:2021-07-16 12:20:14 【问题描述】:

我使用来自 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 搜索 hdisXDefined,因为我想用任何 Defined搜索主机> 路径。像这样的:

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

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

【问题讨论】:

这能回答你的问题吗? Search Dictionary Values in Ansible 请更具体一点 1)您的示例数据仅包含“path_status”的定义值 2)由于有多个值,您到底想要哪个条件?所有这些都必须定义?至少我必须定义一个?全面的?每个fcs?每个高清? 更具体地说: 1) 是的,path_status 可能是可用的或已定义的。 2)需要找到任何(每个主机至少一个)定义的路径。 【参考方案1】:

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

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

以下剧本:

---
- 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

【讨论】:

以上是关于Ansible:在嵌套字典变量中查找属性的主要内容,如果未能解决你的问题,请参考以下文章

ansible中的多个嵌套循环

如何使用分隔符在Ansible中拆分值

Ansible变量嵌套解析

Django模板如何用一个变量查找字典值

如何在嵌套字典中查找所有出现的键,同时跟踪外部字典键值?

Python - 在嵌套字典中查找特定值的父键