0

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

Jyotsna Gupta
  • 41
  • 2
  • 10

1 Answers1

2

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"
    ]


Test file
$ 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
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63