how to add lines into a file

30 views
Skip to first unread message

Praveen Dev

unread,
Feb 15, 2022, 3:51:49 PM2/15/22
to Ansible Project
HI All,

i have a requirement to add a few lines  after a particular match / keyword or string in a shell script .  below code is for reference when i execute this code it is adding all the bold lines at the bottom . can you please help  thank you

- name: EDIT DIP Job Runner
  lineinfile:
    path: "{{ sas_config }}/{{ sas_config_level }}/Web/Applications/SASWIPSchedulingServices9.4/dip/DIPJobRunner.sh"
    state: present
    insertafter: 'kill $pid'
    line: "{{ item }}"
  with_items:
  - sleep 3
  - if [ -f $PIDFILE ]; then
  - kill -9 $pid
  - fi

  become: true
  become_user: '{{ install_user.name }}'
  when: '"midtier" in group_names'

Praveen Dev

unread,
Feb 23, 2022, 8:20:58 AM2/23/22
to Ansible Project
any update please ?

Dick Visser

unread,
Feb 23, 2022, 8:58:25 AM2/23/22
to ansible...@googlegroups.com
This means that the regex you provide doesn't  match:



--
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 ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/f6fc4c37-1c24-4274-8509-d0873ac58dcan%40googlegroups.com.
--
Sent from a mobile device - please excuse the brevity, spelling and punctuation.

Todd Lewis

unread,
Feb 23, 2022, 10:42:15 AM2/23/22
to Ansible Project
For the regex to match, you'll need to backslash-escape (with a single backslash, since you're using single quotes) the dollar sign in your insertafter regex.

But even if you do that, it isn't going to work. There are several problems. First, each line that gets added will be added immediately after the `insertafter` line, so the order will end up being backwards. Second, there already is a "fi" line after "kill $pid" in DIPJobRunner.sh, so your "fi" won't be added. (Which is just as well, since they're in the wrong order anyway.

You can probably get what you want if you drop the with_items and change "line:" like this:
    line: "sleep 3; if [ -f $PIDFILE ] ; then kill -9 $pid ; fi"
Reply all
Reply to author
Forward
0 new messages