可言剧本查找文件字符串,然后另一个字符串

问题描述 投票:0回答:1

我想找到字符串“人”,并在均等符号之后提取值 姓氏,对于所有人的出现。

我知道如何在文件中找到字符串,例如“人”,但知道如何提取 遵循所有人出现的所有出现的名称和姓氏值。
  In the file, there could be only one occurrence of person or five.

Thank you very much from a newbie Ansible coder.

这有点棘手,但有可能。 为了简单起见,让我们考虑两个限制:

文件位于与playbook
string file ansible
1个回答
0
投票
名称的顺序是不可变的 - 否则,逻辑可能会更加复杂。

话虽如此,我们可以:

  • 找到文件
  • filter包含一个或姓氏的行
  • 删除“键”,以便只保留名称
  • 由于我们获得了平坦的列表,我们可以以2组为组,代表名字和姓氏

加入这些对以获取全名:

  • 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
  • 模块返回值的内容清理剧本日志的用法。
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.