从 stderr_lines 中提取特定值

Extracting specific value from stderr_lines

这是我的 ansible 脚本

- name: show1
  debug:
    msg: "{{response.stderr_lines}}"

这是输出

msg:
      - Using endpoint [https://us-central1-aiplatform.googleapis.com/]
      - CustomJob [projects/123456/locations/us-central1/customJobs/112233445566] is submitted successfully.
      - ''
      - Your job is still active. You may view the status of your job with the command
      - ''
      - '  $ gcloud ai custom-jobs describe projects/123456/locations/us-central1/customJobs/112233445566'
      - ''
      - or continue streaming the logs with the command
      - ''
      - '  $ gcloud ai custom-jobs stream-logs projects/123456/locations/us-central1/customJobs/112233445566'

这里我想提取自定义作业 ID,即 112233445566

我使用了如下 select 模块

- name: show
  debug:
    msg: "{{train_custom_image_unmanaged_response.stderr_lines | select('search', 'describe') | list }}"

它给了我这个输出

msg:
  - '  $ gcloud ai custom-jobs describe projects/123456/locations/us-central1/customJobs/112233445566'

但我只想要上面指定的作业 ID。对此有什么想法吗?

谢谢。

您选择了感兴趣的行。现在您希望从中分离出作业 ID 号。您可以像这样使用正则表达式来做到这一点:

- set_fact:
    line: "{{train_custom_image_unmanaged_response.stderr_lines | select('search', 'describe') | list }}"
- debug:
    msg: "{{ line | regex_search('.*/customJobs/(\d+)', '\1') }}"

这将为您提供 /customJobs/ 之后行尾的所有数字。参见 https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#searching-strings-with-regular-expressions