How to use piping multiple shell commands

Hi,

I am trying the below snippet to detect the established ports. but Ansible throws an error.

`

  • name: List the port connections
    shell: "netstat -nautp |grep -iE “137|389|445|636” |awk ‘{print $5}’ |cut -d: -f2 |grep -iwE “137|389|445|636”
    register: dc_stat
    debug:
    var: dc_stat.stdout_lines
    `

this through’s the error

`
The offending line appears to be:

  • name: List the port connections
    shell: netstat -nautp |grep -iE “137|389|445|636” |awk ‘{print $5}’ |cut -d: -f2 |grep -iwE “137|389|445|636”
    ^ here
    We could be wrong, but this one looks like it might be an issue with
    unbalanced quotes. If starting a value with a quote, make sure the
    line ends with the same set of quotes. For instance this arbitrary
    example:

foo: “bad” “wolf”

Could be written as:

foo: ‘“bad” “wolf”’

`

I tried inserted a " " for the shell commands and using vars/pipe also …
How can i direct the output of "netstat -nautp |grep -iE “137|389|445|636” |awk ‘{print $5}’ |cut -d: -f2 |grep -iwE “137|389|445|636"” to a variable ?

Or do we have to use backslash() as in shell ?

Error clearly says to use proper quotes

‘netstat -nautp |grep -iE “137|389|445|636” |awk ‘{print $5}’ |cut -d: -f2 |grep -iwE “137|389|445|636”’

Save it in Regiter
Use set_facts to store as a variable

Thanks Vinoth. It worked

Yes . I learnt that we can use the formatting as in python either with ’ ’ or " ".