How to incorporate awk command in ansible shell module

Below is my playbook:

`

  • name: “Collecting File details”
    shell: “ls -ltr {{ item }} | awk '{$2=$4=$5=”"; print $0}’ | tr ‘\n’ ‘\t’ | tr -s " ";

with_fileglob:

  • “{{ playbook_dir }}/tmpfiles/*”
    `

I get syntax error due to the awk command as below.

ERROR! Syntax Error while loading YAML.
did not find expected key
shell: “ls -ltr {{ item }} | awk '{$2=$4=$5=”"; print $0}’ | tr ‘\n’ ‘\t’ | tr -s " ";
^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:

Can you please suggest how can I incorporate such a awk in ansible shell module ?

There are a few things you should know and consider:

From the shell command's documentation: https://docs.ansible.com/ansible/latest/modules/shell_module.html

"If you want to execute a command securely and predictably, it may be better to use the command <https://docs.ansible.com/ansible/latest/modules/command_module.html#command-module&gt; module instead. Best practices when writing playbooks will follow the trend of using command <https://docs.ansible.com/ansible/latest/modules/command_module.html#command-module&gt; unless the |shell| module is explicitly required. When running ad-hoc commands, use your best judgement."

So, consider using the command module.

From the command module's documentation: https://docs.ansible.com/ansible/latest/modules/command_module.html

"The command(s) will not be processed through the shell, so variables like |$HOME| and operations like |"<"|, |">"|, |"|"|, |";"| and |"&"| will not work. Use the shell <https://docs.ansible.com/ansible/latest/modules/shell_module.html#shell-module&gt; module if you need these features."

The pipe | won't work inline.

Also, your YAML has quoting issues. It becomes hard to quote long line commands that has a mix of single and double quotes in YAML. You are better off using the script module.

https://docs.ansible.com/ansible/latest/modules/script_module.html#script-module

Try the script module and see how it goes.

As the error message say you have a missing quote.
So lets start on the first qoute and that is the double quote in front of ls.
And if you them look at every other quotes you'll see that it's not closed anywhere.

Since you don't need a quote at the start of the shell command anyway just remove it.

Sorry for the missing quotes. It was a typo.

Thank you for the suggestion however, I’m compelled to use shell module. Using command or script is not an option.

My actual playbook looks like this:

shell: "ls -ltr {{ BASEPATH }}/{{ vars[item.split('.')[1]] }}/{{ item | basename }} | awk '{$2=$4=$5=""; print $0}' | tr '\n' '\t' | tr -s " ";"

It gives error when using command module and I had already discussed on a seperate post where using shell was recommended.

Sorry for the missing quotes. It was a typo.

Thank you for the suggestion however, I'm compelled to use shell module. Using command or script is not an option.

My actual playbook looks like this:

>
shell: "ls -ltr {{ BASEPATH }}/{{ vars[item.split('.')[1]] }}/{{ item | basename }} | awk '{$2=$4=$5=""; print $0}'
> tr '\n' '\t' | tr -s "";"
>

It gives error when using command module and I had already discussed on a seperate post where using shell was recommended.

True, but why don't you use the ansible stat module instead?

Regards
        Racke

Be careful. "vars" is a reserved word
https://docs.ansible.com/ansible/latest/reference_appendices/playbooks_keywords.html#play

Cheers,

  -vlado