blockinfile: content="{{ item.content }}" -- Making it pretty

16 views
Skip to first unread message

John Harmon

unread,
Mar 21, 2018, 5:03:13 PM3/21/18
to Ansible Project
I have the following which works. However, I would like to make the bash script code "pretty".  It is found under the "content" section of the with_items.  How can I do this?

    - name: Ensure myscript.sh contains directory checks
      blockinfile
:
        dest
: "/home/{{ user.stdout }}/Scripts/myscript.sh"
        insertafter
: "{{ item.regexp }}"
        marker
: "## {mark} added by Ansible - {{ item.define }}"
        content
: "{{ item.content }}"
      with_items
:
       
- { regexp: "^trap.*exit\ 1.*1\ 2\ 3\ .*", define: "Check for /interface", content: "if [ -f /interface/ ]; then echo 'The /interface/$NATU directory is missing, aborting!'; exit 1; fi" }
       
- { regexp: "^NATU.*interface.*grep\ natu.*", define: "Check for /interface/natu", content: "if [ -f /interface/$NATU ]; then echo 'The /interface/$NATU directory is missing, aborting!'; exit 1; fi" }


I want to have the content injected into the file as follows (which can easily be done without a loop, but I wish to do this within the loop):
if [ -f /interface/ ]; then
  echo 'The /interface/$NATU directory is missing, aborting!'
  exit 1
fi

Thanks in advance!

Kai Stian Olstad

unread,
Mar 22, 2018, 10:39:55 AM3/22/18
to ansible...@googlegroups.com
Have you tried using \n for the newline?

--
Kai Stian Olstad

John Harmon

unread,
Mar 22, 2018, 12:01:37 PM3/22/18
to Ansible Project
That works!  Thanks Kai. 
Reply all
Reply to author
Forward
0 new messages