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

问题描述

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

I have a file which has the following data

乘以4和5

乘以5和6

加3到4

加8到4

sub 3 from 6

sub 3 from 6

sub 9 from 5

sub 9 from 5

div 6乘2

div 8乘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.

推荐答案

使用。下面的游戏

- 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