这是以下问题的后续: 计算输出中 json 对象的数量
我正在编写一本手册,用于修补我们的本地 GitLab 运行程序。 我想要的过程是:
暂停和恢复步骤很简单,我可以做到。 我遇到问题的步骤是工作检查。
我可以调用 GitLab API,通过以下方式获取虚拟机运行程序的状态作业列表:
- name: Wait for No Running Jobs
uri:
url: "{{ api_base_url }}/runners/{{ item.id }}/jobs?status=running"
headers:
PRIVATE-TOKEN: "{{ PRIVATE_TOKEN }}"
register: job_list
with_items: "{{ host_runner_ids.json }}"
until: job_list.results | json_query('[*].json[*]') | first | length == 0
我不断收到错误:
错误是: ({% if job_list.results | json_query('[].json[]') | first | length <= 20 %} True {% else %} False {% endif %}): 'NoneType' object is not iterable.
发生意外的模板类型错误
我可以取出
until
并随后执行调试任务,以确保查询和一切都正确:
- name: debug job list
debug:
msg: "{{ job_list.results | json_query('[*].json[*]') | first | length }}"
with_items: "{{ host_runner_ids.json }}"
ok: [runner01] => (item={'id': 11226, 'description': 'config01', 'ip_address': None, 'active': True, 'paused': False, 'is_shared': True, 'runner_type': 'instance_type', 'name': 'gitlab-runner', 'online': True, 'status': 'online'}) => {
"msg": "20"
}
ok: [runner01] => (item={'id': 11227, 'description': 'config02', 'ip_address': None, 'active': True, 'paused': False, 'is_shared': True, 'runner_type': 'instance_type', 'name': 'gitlab-runner', 'online': True, 'status': 'online'}) => {
"msg": "20"
}
ok: [runner01] => (item={'id': 11228, 'description': 'config03', 'ip_address': None, 'active': True, 'paused': False, 'is_shared': False, 'runner_type': 'group_type', 'name': 'gitlab-runner', 'online': True, 'status': 'online'}) => {
"msg": "20"
}
因此,我知道查询有效,并且我可以获得托管在该特定 VM 服务器上的每个运行程序的长度(也称为运行的作业数)。 我只是不知道如何使“等待没有正在运行的作业”循环并重新检查,直到所有跑步者都没有正在运行的作业(长度= 0)。在其他作品中,它不轮换,剧本可以不间断地继续升级)
谢谢。
{
"results": [
{
"http request info": "info redacted/trimmed",
"json": [
{
"id": 1016506,
"status": "canceled",
"stage": "scanning",
"name": "scan_feature_image",
"ref": "refs/merge-requests/1248/head",
"tag": false,
"coverage": null,
"allow_failure": true,
"created_at": "2023-12-20T19:10:37.989Z",
"started_at": "2023-12-20T19:19:00.418Z",
"finished_at": "2023-12-20T19:19:20.610Z",
"erased_at": null,
"duration": 20.191925,
"queued_duration": 0.953025,
"user": "info redacted/trimmed",
"commit": {},
"pipeline": "info redacted/trimmed" ,
"project": "info redacted/trimmed"
},
{
"id": 1020404,
"status": "canceled",
"stage": "deploy",
"name": "Elasticsearch_Deploy",
"ref": "Version-0.5.5",
"tag": false,
"coverage": null,
"allow_failure": false,
"created_at": "2023-12-28T21:10:28.212Z",
"started_at": "2023-12-28T21:12:41.546Z",
"finished_at": "2023-12-28T21:13:04.246Z",
"erased_at": null,
"duration": 22.699951,
"queued_duration": 0.490441,
"user": "info redacted/trimmed",
"commit": "info redacted/trimmed",
"pipeline": "info redacted/trimmed",
"project": "info redacted/trimmed"
},
{
"id": 1022991,
"status": "canceled",
"stage": "scanning",
"name": "gemnasium-maven-dependency_scanning",
"ref": "aks-dev",
"tag": false,
"coverage": null,
"allow_failure": true,
"created_at": "2024-01-03T17:44:15.751Z",
"started_at": "2024-01-03T17:44:25.035Z",
"finished_at": "2024-01-03T17:44:52.196Z",
"erased_at": null,
"duration": 27.160454,
"queued_duration": 0.374161,
"user": "info redacted/trimmed",
"commit": "info redacted/trimmed",
"pipeline": "info redacted/trimmed",
"project": "info redacted/trimmed"
},
{
"id": 1045815,
"status": "canceled",
"stage": "scanning",
"name": "scan_feature_image",
"ref": "Dev",
"tag": false,
"coverage": null,
"allow_failure": true,
"created_at": "2024-01-25T23:43:11.020Z",
"started_at": "2024-01-25T23:43:21.785Z",
"finished_at": "2024-01-25T23:43:26.569Z",
"erased_at": null,
"duration": 4.783896,
"queued_duration": 1.294567,
"user": "info redacted/trimmed",
"commit": "info redacted/trimmed",
"pipeline": "info redacted/trimmed",
"project": "info redacted/trimmed"
},
{
"id": 1046169,
"status": "canceled",
"stage": "deploy",
"name": "deploy_feature",
"ref": "Dev",
"tag": false,
"coverage": null,
"allow_failure": false,
"created_at": "2024-01-26T16:14:24.659Z",
"started_at": "2024-01-26T16:14:26.657Z",
"finished_at": "2024-01-26T16:18:37.569Z",
"erased_at": null,
"duration": 250.912905,
"queued_duration": 1.651202,
"user": "info redacted/trimmed",
"commit": "info redacted/trimmed",
"pipeline": "info redacted/trimmed",
"project": "info redacted/trimmed"
}
],
"more misc info": "trimmed for length"
}
]
}
我想我可能已经找到了我的问题。 希望它对其他人有帮助。
调试时,原始作业和后续调试作业之间的 json 结果存储方式略有不同。
例如,获取跑步者作业列表的 API 调用任务会得到如下结果:
job_list = {
misc info: and other stuff
json: [
{ job1 info },
{ job2 info },
{ etc }
]
}
使用 with_items 循环时,每个单独的项目结果将具有与上述相同的结构。
但是,当我在循环任务之后运行调试任务时,我会看到以下内容:
job_list = {
results: [
{
misc info: and other stuff from item1
json: [
{ job1 info },
{ job2 info },
{ etc }
]
},
{
misc info: and other stuff from item2
json: [
{ job1 info },
{ job2 info },
{ etc }
]
}
]
}
因此,如您所见,每个项目的 job_list 结果都打包在
results
列表/数组下。
一旦我切换了
until
条件以查看 json 作为单个结果:
until: job_list.json | length == 0
事情似乎按预期进行。
因此,给看到此内容的任何人提示:如果您正在使用 with_X 循环(例如 with_items),并且需要添加条件。 如果可能,请尝试获取适用于列表中的单个项目的条件。 就我而言,我使用单个 ID 对 API 调用进行了硬编码。 然后展开一个循环,看看事情进展如何。