I have a file which has the following data
!
multiply 4 and 5
multiply 5 and 6
!
add 3 to 4
<
Use with_lines. The play below
- 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
gives
"my_commands": [
"add 3 to 4",
"add 8 to 4"
]