"here doc" in shell command

1,320 views
Skip to first unread message

ru...@yahoo.com

unread,
Oct 16, 2017, 12:43:34 AM10/16/17
to Ansible Project

I am trying to use a "here doc" in a shell command like so:

- name: frustration
  shell: |
    cat <<-EOF | my_program
    input line 1
    input line 2
    EOF
    another_program

along with a number of permutations, all of which have resulted in "bash: line 3: warning: here-document at line 0 delimited by end-of-file (wanted `EOF')".

I found this:
  https://github.com/ansible/ansible/issues/12856
which seems to imply there is some way to accomplish what I'm trying do but I can't understand how, nor do I see anything else relevant in the Ansible docs or Google.

What is the magic syntax for the above?


thomas....@adtech.com

unread,
Oct 16, 2017, 7:12:40 AM10/16/17
to Ansible Project
    - name: fun fun fun
     shell:
       cmd: |
         #!/bin/bash
         SCRIPT=$(cat <<EOF
         echo "hello world!"
         EOF
         )
         ${SCRIPT}

Solution found here: https://stackoverflow.com/questions/40230184/how-to-do-multiline-shell-script-in-ansible

ru...@yahoo.com

unread,
Oct 16, 2017, 6:22:11 PM10/16/17
to Ansible Project
Ah, thank you very much!  I actually had seen that SO post before but realize now when I tried it I screwed up (I had left the vertical bar character on the "shell" line, not the command line.)  Now I see it does in fact work.

But one further question...  Where is "cmd" documented?

thomas....@adtech.com

unread,
Oct 18, 2017, 3:48:59 AM10/18/17
to Ansible Project
Good point. It's seems not officially documented. In my local Ansible installation I've found the relating code part
in playbook/tasks.py

   182          # the command/shell/script modules used to support the `cmd` arg,
   183          # which corresponds to what we now call _raw_params, so move that
   184          # value over to _raw_params (assuming it is empty)
   185          if action in ('command', 'shell', 'script'):
   186              if 'cmd' in args:
   187                  if args.get('_raw_params', '') != '':
   188                      raise AnsibleError("The 'cmd' argument cannot be used when other raw parameters are specified."
   189                                         " Please put everything in one or the other place.", obj=ds)
   190                  args['_raw_params'] = args.pop('cmd')



Reply all
Reply to author
Forward
0 new messages