# How to incorporate awk command in ansible shell module

**URL:** <https://forum.ansible.com/t/how-to-incorporate-awk-command-in-ansible-shell-module/31148>\
**Category:** Ansible Project\
**Created:** [August 28, 2019, 6:50am UTC](https://forum.ansible.com/t/how-to-incorporate-awk-command-in-ansible-shell-module/31148 "2019-08-28T06:50:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Easy\_King](https://avatars.discourse-cdn.com/v4/letter/e/c2a13f/32.png) [@Easy\_King](https://forum.ansible.com/u/Easy_King)\
**Post date:** [August 28, 2019, 6:50am UTC](https://forum.ansible.com/t/how-to-incorporate-awk-command-in-ansible-shell-module/31148/1 "2019-08-28T06:50:17Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![Sudheer\_S1](https://avatars.discourse-cdn.com/v4/letter/s/e99b99/32.png) [@Sudheer\_S1](https://forum.ansible.com/u/Sudheer_S1)\
**Post date:** [August 28, 2019, 7:07am UTC](https://forum.ansible.com/t/how-to-incorporate-awk-command-in-ansible-shell-module/31148/2 "2019-08-28T07:07:54Z")

</div>

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](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](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](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](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](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](https://docs.ansible.com/ansible/latest/modules/script_module.html#script-module)

Try the script module and see how it goes.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 28, 2019, 7:16am UTC](https://forum.ansible.com/t/how-to-incorporate-awk-command-in-ansible-shell-module/31148/3 "2019-08-28T07:16:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Easy\_King](https://avatars.discourse-cdn.com/v4/letter/e/c2a13f/32.png) [@Easy\_King](https://forum.ansible.com/u/Easy_King)\
**Post date:** [August 28, 2019, 7:35am UTC](https://forum.ansible.com/t/how-to-incorporate-awk-command-in-ansible-shell-module/31148/4 "2019-08-28T07:35:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [August 28, 2019, 7:39am UTC](https://forum.ansible.com/t/how-to-incorporate-awk-command-in-ansible-shell-module/31148/5 "2019-08-28T07:39:20Z")

</div>

> 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  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [August 28, 2019, 7:41am UTC](https://forum.ansible.com/t/how-to-incorporate-awk-command-in-ansible-shell-module/31148/6 "2019-08-28T07:41:07Z")

</div>

Be careful. "vars" is a reserved word  
[https://docs.ansible.com/ansible/latest/reference\_appendices/playbooks\_keywords.html#play](https://docs.ansible.com/ansible/latest/reference_appendices/playbooks_keywords.html#play)

Cheers,

&nbsp;&nbsp;-vlado
