I am using below shell command and result is an array of strings'
MQSFILEPARSED=$(cat $FILE | grep ' Name=' | cut -d '=' -f2);
Is there a way to perform this operation in Ansible without using the command module
I am using below shell command and result is an array of strings'
MQSFILEPARSED=$(cat $FILE | grep ' Name=' | cut -d '=' -f2);
Is there a way to perform this operation in Ansible without using the command module
Yes. It is. For example
vars:
FILE: test.txt
tasks:
- set_fact:
MQSFILEPARSED: "{{ lookup('file', FILE).splitlines()|
select('match', '^(.*) Name=(.*)$')|
map('regex_replace', my_regex, my_replace)|
list }}"
vars:
my_regex: '^(.*)=(.*)$'
my_replace: '\2'
- debug:
var: MQSFILEPARSED
gives
"MQSFILEPARSED": [
"Value",
"Value2"
]
$ cat test.txt
line1
line2
Name=Value
line4
line5 Name=Value2
Test script
$ cat test.sh
#!/bin/sh
FILE=test.txt
MQSFILEPARSED=$(cat $FILE | grep ' Name=' | cut -d '=' -f2)
printf "$MQSFILEPARSED \n"
gives
Value
Value2