我想找到字符串“人”,并在均等符号之后提取值 姓氏,对于所有人的出现。
我知道如何在文件中找到字符串,例如“人”,但知道如何提取 遵循所有人出现的所有出现的名称和姓氏值。 In the file, there could be only one occurrence of person or five.
Thank you very much from a newbie Ansible coder.
这有点棘手,但有可能。 为了简单起见,让我们考虑两个限制:
文件位于与playbook话虽如此,我们可以:
加入这些对以获取全名:
batch
# playbook.yaml
---
- name: Find the names within the files
hosts: localhost
connection: local
gather_facts: false
tasks:
- name: Find the matching files
find:
paths:
- "{{ playbook_dir }}"
patterns:
- "*.txt"
contains: person
file_type: file
depth: 1
register: find_results
- name: Find the names
set_fact:
names: >-
{{
names | default([]) +
[
lookup('file', item.path)
| split()
| select('match', '(lastname firstname)=*)
| map('regex_replace', '(lastname|firstname)=', '')
| batch(2)
| map('join','')
}}
loop: "{{ find_results.files}}"
loop_control:
label: "{{ item.path }}"
- name: List the names
debug:
var: names | flatten