# replace multiple lines in multiple files using lineinfile and register

**URL:** <https://forum.ansible.com/t/replace-multiple-lines-in-multiple-files-using-lineinfile-and-register/14129>\
**Category:** Ansible Project\
**Created:** [December 11, 2013, 6:08pm UTC](https://forum.ansible.com/t/replace-multiple-lines-in-multiple-files-using-lineinfile-and-register/14129 "2013-12-11T18:08:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![iqbal.hasnan](https://avatars.discourse-cdn.com/v4/letter/i/cdc98d/32.png) [@iqbal.hasnan](https://forum.ansible.com/u/iqbal.hasnan)\
**Post date:** [December 11, 2013, 6:08pm UTC](https://forum.ansible.com/t/replace-multiple-lines-in-multiple-files-using-lineinfile-and-register/14129/1 "2013-12-11T18:08:02Z")

</div>

Hello Ansible users,

I’m trying to replace **multiple lines** in **multiple files** using the lineinfile and register, and this is what I got so far.

- name: list of the .conf files  
raw: ls /etc/httpd/conf.d/\*.conf  
register: certs\_dir  
tags: update-cert

- name: update certs with the new name in \*conf  
lineinfile: dest={{item}} backup=yes state=present regexp=“^ SSLCertificateFile” insertafter=“^ SSLCertificateFile” line=" SSLCertificateFile /etc/pki/tls/certs/new\_cert.cer"  
with\_items: certs\_dir.stdout\_lines  
tags: update-cert

the problem with this is It will only replace **one line** in **multiple files** , is there any better way of doing this other than making another copy of the second task to replace line?

please advise,

thank you

---

<div class="post-metadata">

**Author:** ![PhilEv](https://avatars.discourse-cdn.com/v4/letter/p/ee59a6/32.png) [@PhilEv](https://forum.ansible.com/u/PhilEv)\
**Post date:** [December 11, 2013, 9:05pm UTC](https://forum.ansible.com/t/replace-multiple-lines-in-multiple-files-using-lineinfile-and-register/14129/2 "2013-12-11T21:05:49Z")

</div>

For one use case where template was not optimal and to replace N lines in one file (grub.conf),  
it was convenient to have something like (IIRC):

task1: grep | wc -l  
register: the\_count

task2: lineifile regex= line=‘string’ …

with\_sequence: the count

ok not the exact syntax , just to give the idea.

do not have the real example at hands, can forward later

will need adaptation to cope with multiple files and hence multiple different count for each different file

and a caveat with with\_sequence if the\_count is 0

HTH

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [December 11, 2013, 9:07pm UTC](https://forum.ansible.com/t/replace-multiple-lines-in-multiple-files-using-lineinfile-and-register/14129/3 "2013-12-11T21:07:31Z")

</div>

So since we have a very simple way to list local files (with\_fileglob) it makes sense that we probably just have a fileglob module for use of register that lists the files in a given directory

- fileglob: path=/etc/foo/\*.d  
register: files

- shell: foo {{ item.0 }} and {{ item.1 }}  
with\_nested:

- files

- other\_things

etc
