我正在尝试从远程文件读取文件内容。但我希望仅显示与 fstab 文件中匹配的 UUID 的消息。但当我运行下面的剧本时,它会抛出下面的错误。
剧本:
- name: read content from a file
hosts: all
become: true
tasks:
- name: read a content
slurp:
src: /etc/fstab
register: output
- name: print
debug:
msg: "{{ (output['content'] | b64decode).split('\n') | regex_search('^\\S+/UUID\b.*', multiline=True) }}"
错误:
TASK [print] *************************************************************************************************************
FAILED! => {"msg": "Unexpected templating type error occurred on ({{ (output['content'] | b64decode).split('\n') | regex_search('^\\\\S+/UUID\b.*', multiline=True) }}): expected string or bytes-like object"}
答案1
首先,当我运行你的剧本时,我没有收到错误;我只是没有从debug
任务中得到任何输出:
"msg": ""
如果您要查找以 开头的行列表UUID
,您可以这样做:
- name: read content from a file
hosts: localhost
tasks:
- name: read a content
slurp:
src: /etc/fstab
register: output
- name: print
debug:
msg: >
{{
(output['content'] | b64decode).splitlines() |
select("match", "UUID")
}}
在我的系统上,这会产生:
ok: [localhost] => {
"msg": [
"UUID=2fbad834-42b2-48fd-868d-3eea12e144de / btrfs subvol=root00,compress=zstd:1 0 0",
"UUID=c72e509c-453a-4e62-8cea-23ad112765d3 /boot ext4 defaults 1 2",
"UUID=F3B3-4F58 /boot/efi vfat umask=0077,shortname=winnt 0 2",
"UUID=2fbad834-42b2-48fd-868d-3eea12e144de /home btrfs subvol=home00,compress=zstd:1 0 0",
]
}
如果你想要一条匹配具体的uuid,你可以这样做:
- name: read content from a file
hosts: localhost
tasks:
- name: read a content
slurp:
src: /etc/fstab
register: output
- name: print
debug:
msg: >
{{
(output['content'] | b64decode).splitlines() |
select("match", "UUID=c72e509c-453a-4e62-8cea-23ad112765d3") |
first
}}
在我的系统上,这会产生:
ok: [localhost] => {
"msg": "UUID=c72e509c-453a-4e62-8cea-23ad112765d3 /boot ext4 defaults 1 2\n"
}