On 28/08/19 12:20 PM, Mohtashim S wrote:
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> 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> 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> 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.


--

Sudheer Satyanarayana
DevOps And Development Consultant
AWS Solutions Architect - Associate

--
You received this message because you are subscribed to the Google Groups "Ansible 
Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/bfc94b82-25aa-deb3-d7ff-009f2e1dcbdd%40techchorus.net.

Reply via email to