循环循环的Ansible列表

时间:2017-07-13 14:17:30

标签: ansible

我遇到了问题但不确定如何妥善解决这个问题。我写了一个模块来创建系统上安装的版本的事实。我的目标是获取字典词典中包含的更改号列表。

任务[调试] ******************************************* ************************

    ok: [swsrv1-ccc01.localhost] => {
        "changed": false,
        "hostvars[inventory_hostname].version": {
            "version0217_1": {
                "change": "311",
                "Date_Deployed": "2016-11-17_11-07-08",
                "Defect": "NA",
                "Start_Date": "NA"
            },
            "version0217_11": {
                "change": "NA",
                "Date_Deployed": "2016-12-06_09-45-07",
                "Defect": "NA",
                "Start_Date": "NA"
            },
            "version0217_13": {
                "change": "NA",
                "Date_Deployed": "2016-12-06_09-45-14",
                "Defect": "NA",
                "Start_Date": "NA"
            },
            "version0217_15": {
                "change": "NA",
                "Date_Deployed": "2016-12-08_11-15-39",
                "Defect": "NA",
                "Start_Date": "NA"
            },
            "version0217_16": {
                "change": "1411",
                "Date_Deployed": "2016-12-13_16-45-42",
                "Defect": "NA",
                "Start_Date": "NA"
            },
            "version0217_17": {
                "change": "NA",
                "Date_Deployed": "2016-12-13_16-46-55",
                "Defect": "NA",
                "Start_Date": "NA"
            },  
            "version0217_19": {
                "change": "242",
                "Date_Deployed": "2016-12-19_13-29-37",
                "Defect": "NA",
                "Start_Date": "NA"
            }
        }
    }

    TASK [debug] *******************************************************************
    fatal: [swsrv1-ccc01.localhost]: FAILED! => {"failed": true, "msg": "the key change should point to a list, got 'NA'"}
- debug: var=hostvars[inventory_hostname].version

- debug: msg="{{ item.1 }}"
  with_subelements:
    - "{{ hostvars[inventory_hostname].version }}"
    - change

我的解决方案with_subelements只给了我一个对象但不是列表。提前谢谢!

1 个答案:

答案 0 :(得分:0)

您可能想要使用with_dict

- debug: msg="{{ item.value.change }}"
  with_dict: "{{ hostvars[inventory_hostname].version }}"