2

I have come across a peculiar issue while using ansible. The problem is very strange and dangerous. I have a written a code to insert data in a particular section of a file i.e to add line after [database] in say /etc/cinder/cinder.conf.

The problem is I have noticed sometimes it adds the content properly after the tag [database] , but sometimes it gets confused by seeing a line like # put ur infore here for [database] in the file and adds our required line below it instead of where it should actually put it.

   - name: Adding Entries in "/etc/cinder/cinder.conf"
     lineinfile:
      dest: "/etc/cinder/cinder.conf"
      insertafter:  "{{ item.inserts }}"
      state: present
      line: "{{ item.lines }}"
     with_items:
      - { inserts: '\[database\]', lines: 'rpc_backend = rabbit' }

This situation is quite dangerous in a production environment! How can I add the data correctly?

4

To avoid matching in a comment, anchor your regexp to the beginning of the line:

- { inserts: '^\[database\]', lines: 'rpc_backend = rabbit' }
  • Oh God !! So silly of me to forget about this,, thanks a ton ,, I believe this would resolve my concern,, – Mohammed Ali Jun 25 '17 at 13:26
  • 2
    @Mohd If this solves your issue, please consider accepting the answer. If you're happy with the answer, accepting it and maybe even upvoting it is the best way to say "Thank You!" :-) – Kusalananda Jun 25 '17 at 17:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.