如何在ansible中读取文件的特定部分

如何在ansible中读取文件的特定部分,ansible,ansible-template,Ansible,Ansible Template,我有一个包含以下数据的文件 ! 乘以4和5 乘以5和6 ! 加3至4 加8至4 ! 从6分到3分 从5分到9分 ! ! 第6组乘2组 第8分区乘1 我想从文件中只读添加命令 使用查找插件,我能够读取整个文件的数据 但是我不知道如何从文件中只读取特定的add命令 这是我写的代码 --- - name: Extracting the Add commands from the File hosts: 127.0.0.1 connection: local vars:

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

!

乘以4和5

乘以5和6

!

加3至4

加8至4

!

从6分到3分

从5分到9分

!

!

第6组乘2组

第8分区乘1

我想从文件中只读添加命令

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

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

这是我写的代码

---
 - 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中读取文件的特定部分。

使用。下面的剧本

- 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
给予

使用。下面的剧本

- 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_data_files”内容)拆分为行,对于每个项目,如果条件通过,则运行条件when语句,特定项目将添加到my_命令列表我还有一个问题,我想对另一个文件执行相同的任务,并比较输出(两个输出列表)。与使用不同的变量(my_commands_2)再次编写相同的任务不同,有没有办法编写单个任务并多次调用它?一个文件调用一个任务(比如data1.txt),另一个文件调用一个任务(比如data2.txt),然后比较结果。如果我错了,请纠正我。使用_行将输出(“my_data_files”内容)拆分为行,对于每个项目,如果条件通过,则运行条件when语句,特定项目将添加到my_命令列表我还有一个问题,我想对另一个文件执行相同的任务,并比较输出(两个输出列表)。与使用不同的变量(my_commands_2)再次编写相同的任务不同,有没有任何方法可以编写单个任务并多次调用它,一个文件调用一个任务(比如data1.txt),另一个文件调用一个任务(比如data2.txt),然后比较结果。