找到不在列表中的字符串/整数

时间:2018-07-27 03:49:38

标签: ansible jinja2

我正在通过API从IPAM中提取VLAN的列表,我希望能够找到未在列表中使用的未使用的“ vlanId”。我期望我可以将with_items用于JSON内容,然后将随机函数与直到循环一起使用,并且偶尔它最初会生成列表中不存在的数字。通常情况下,它只是卡住了,并且在生成的随机数已经存在时不会生成新的随机数。

剧本:

  - uri:
      url: "#"
      validate_certs: no
      headers:
        token: "{{ token }}"
      method: GET
      force_basic_auth: yes
      return_content: yes
      register: ipam

  - set_fact:
      value: "{{ 4094 | random(start=1) }}"
    until: value not in item.vlanId
    with_items: "{{ ipam.json.data }}"
    retries: 4093

  - debug: msg="{{ value }}"

相关输出:

ok: [localhost] => (item={u'domainId': u'3', u'description': u'#', u'editDate': None, u'Customer ID': None, u'number': u'2241', u'vlanId': u'548', u'name': u'2241', u'Customer Name': None, u'custom_fields': None, u'Engineer': None}) => {
    "ansible_facts": {
        "value": "2727"
    },
    "ansible_facts_cacheable": false,
    "attempts": 1,
    "changed": false,
    "item": {
        "Customer ID": null,
        "Customer Name": null,
        "Engineer": null,
        "custom_fields": null,
        "description": "#",
        "domainId": "3",
        "editDate": null,
        "name": "2241",
        "number": "2241",
        "vlanId": "548"
    }
}
ok: [localhost] => (item={u'domainId': u'3', u'description': u'#', u'editDate': None, u'Customer ID': None, u'number': u'2242', u'vlanId': u'549', u'name': u'2242', u'Customer Name': None, u'custom_fields': None, u'Engineer': None}) => {
    "ansible_facts": {
        "value": "1955"
    },
    "ansible_facts_cacheable": false,
    "attempts": 1,
    "changed": false,
    "item": {
        "Customer ID": null,
        "Customer Name": null,
        "Engineer": null,
        "custom_fields": null,
        "description": "#",
        "domainId": "3",
        "editDate": null,
        "name": "2242",
        "number": "2242",
        "vlanId": "549"
    }
}
FAILED - RETRYING: set_fact (4000 retries left).Result was: {
    "ansible_facts": {
        "value": "50"
    },
    "ansible_facts_cacheable": false,
    "attempts": 1,
    "changed": false,
    "retries": 4001
}

FAILED - RETRYING: set_fact (3999 retries left).Result was: {
    "ansible_facts": {
        "value": "50"
    },
    "ansible_facts_cacheable": false,
    "attempts": 2,
    "changed": false,
    "retries": 4001
}

FAILED - RETRYING: set_fact (3998 retries left).Result was: {
    "ansible_facts": {
        "value": "50"
    },
    "ansible_facts_cacheable": false,
    "attempts": 3,
    "changed": false,
    "retries": 4001

我当前正在使用ansible 2.4.2.0

如果这是Ansible不能/不应该做的事情,那么任何指导将不胜感激。

1 个答案:

答案 0 :(得分:0)

这就是您想要的:

foreach (ReportGeneric.ExportReportSchedulerModel report in reportList)
{
    log.Debug(report.ReportName + " Export():");
    var parametersArray = new object[1];
    parametersArray[0] = report;

    log.Debug("Thread started in ExportReport() for " + report.ReportName);

    Thread exporttoexcel = new Thread(() =>
    {
        _isStarted = false;
        //invoking DLL method
        var ret = ReportInvokeFunction(moduleName: report.ReportName.Split('_')[0], className: report.ReportName, functionName: "SelectData", parameters: parametersArray);
        if (ret > 0)
        {
            log.Debug("ExportReport() successfull " + report.ReportName);
            _isStarted = true;
        }
        else
        {
            log.Error("ExportReport() failed " + report.ReportName);
            _isStarted = false;

        }
    });

    exporttoexcel.Start();
}

说明:

  1. - debug: msg: "{{ range(1, 4095) | difference(vlanIds) | random }}" vars: vlanIds: "{{ ipam.json.data | map(attribute='vlanId') | list }}" -创建VLAN ID列表,

  2. map(attribute=...-生成一个从1到4094的序列,

  3. range-从上面选择所有元素。 difference列表中没有的

  4. vlanIds-从上面选择一个随机元素。


对于字符串(如标题所示),将random替换为可能的字符串列表。