本文介绍了如何在ansible中读取文件的特定部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下数据的文件

I have a file which has the following data

4 和 5 相乘

5 和 6 相乘

将 3 加到 4

8 加 4

来自 6 的子 3

从 5 分 9

div 6 by 2

div 8 by 1

我只想读取文件中的添加命令.

I want to read only add commands from the file.

使用查找插件我能够读取整个文件的数据.

Using lookup plugin I was able to read the data of the entire file.

但我不知道如何只从文件中读取特定的添加命令

But I don't know how to read only the specific add commands from the file

这是我写的代码.

---
 - name: Extracting the Add commands from the File
   hosts: 127.0.0.1
   connection: local

   vars:
           contents: "{{lookup('file', 'file1.txt')}}"

   tasks:
           - name: Printing the Add commands of the File
             debug:
                     var: contents

我被困在这一点上.谁能帮助我了解如何在 ansible 中读取文件的特定部分.

I am stuck at this point.Could anyone help me out of how to read the specific part of a file in ansible.

推荐答案

使用 with_lines.下面的剧

- hosts: localhost
  vars:
    my_data_file: "{{ playbook_dir }}/data.txt"
    my_commands: []
  tasks:
    - set_fact:
        my_commands: "{{ my_commands + [ item ] }}"
      with_lines: "cat {{ my_data_file }}"
      when: item is search('^add')
    - debug:
        var: my_commands

给予

"my_commands": [
    "add 3 to 4",
    "add 8 to 4"
]

这篇关于如何在ansible中读取文件的特定部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 13:47