我正在尝试从
checksums.txt文件中过滤以
cmctl_linux_arm64
结尾的特定字符串,请参阅示例:
ff5e6efc85421bc46975ee60cf71bec1de3f983f27a3ba8e56a0953e8c9b8a07 cmctl_linux_arm64
bb5b8db1da6b4f7f046ebbf0aced0623e1b93990c474beb525296e72076b9421 cmctl_linux_arm64.spdx.sbom
6f903b8554f7e608ba3d156878f3720480b5dd693400e0354f2c88b94e4b9de0 cmctl_linux_arm64.tar.gz
考虑以下任务:
- name: Set file fact
ansible.builtin.set_fact:
release_file: cmctl_linux_arm64
run_once: true
- name: Set checksums fact
ansible.builtin.uri:
url: https://github.com/cert-manager/cmctl/releases/download/v2.1.0/checksums.txt
return_content: true
register: checksums
run_once: true
- name: Set sha256 checksum fact
ansible.builtin.set_fact:
sha256_checksum: "{{ item.split(' ')[0] }}"
loop: "{{ checksums.content.split('\n') }}"
when: release_file in item
run_once: true
我不确定如何使用 Ansible Regex 搜索,以便定义事实变量的结尾
release_file
并跳过存在的额外 2 个文件校验和。
编辑:使用以下格式修复问题:
when: item | regex_search(release_file + '$')
相比之下,我不能使用
select
:
when: item | select('search', release_file + '$')
或
in
:
when: item in regex_search(release_file + '$')
我想知道条件的正确写法是什么。
翻转物品。然后,匹配字符串开头的模式
result: "{{ checksums.content.split('\n') | map('reverse') |
select('match', release_file|reverse) |
map('reverse') | map('split') | map('first') }}"
给予
result:
- ff5e6efc85421bc46975ee60cf71bec1de3f983f27a3ba8e56a0953e8c9b8a07
用于测试的完整剧本示例
- hosts: localhost
vars:
release_file: cmctl_linux_arm64
checksums:
content: |-
ff5e6efc8542 cmctl_linux_arm64
bb5b8db1da6b cmctl_linux_arm64.spdx.sbom
6f903b8554f7 cmctl_linux_arm64.tar.gz
result: "{{ checksums.content.split('\n') | map('reverse') |
select('match', release_file|reverse) |
map('reverse') | map('split') | map('first') }}"
tasks:
- debug:
var: result