如何从Ansible Tower API查询中过滤结果

时间:2019-06-21 09:59:21

标签: django api ansible filtering

使用邮递员,我只需要在name = TestWorkflow上显示结果

以下是我的查询返回的结果: 获取http://ansible-awx.pxdtools.io:8000/api/v2/workflow_job_template_nodes/

{
    "results": [
        {
            "id": 1323,
            "summary_fields": {
                "workflow_job_template": {
                    "id": 121,
                    "name": "TestWorkflow",
                }
              }
        },
        {
            "id": 29,
            "summary_fields": {
                "workflow_job_template": {
                    "id": 61,
                    "name": "Livraison en TEST",
                },
            }
        }
    ]
}

我已经尝试了许多过滤器:https://docs.ansible.com/ansible-tower/latest/html/towerapi/filtering.html

此查询不起作用,因为名称位于results / summary_fields

获取http://ansible-awx.pxdtools.io:8000/api/v2/workflow_job_template_nodes/?name=TestWorkflow

{
    "detail": "WorkflowJobTemplateNode has no field named 'name'"
}

我想要的预期结果

{
    "results": [
        {
            "id": 1323,
            "summary_fields": {
                "workflow_job_template": {
                    "id": 121,
                    "name": "TestWorkflow",
                }
              }
        }
    ]
}

1 个答案:

答案 0 :(得分:0)

尝试使用下面的selectattr

- set_fact:
    results: "{{ data.results|
                 selectattr('summary_fields.workflow_job_template.name',
                            'match',
                            'TestWorkflow')|
                 list }}"