replace multiple lines in multiple files using lineinfile and register

3,384 views
Skip to first unread message

iqbal....@i.edu.my

unread,
Dec 11, 2013, 1:08:02 PM12/11/13
to ansible...@googlegroups.com
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

Philippe Eveque

unread,
Dec 11, 2013, 4:05:49 PM12/11/13
to ansible...@googlegroups.com
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 <regexp pattern> | wc -l
register:  the_count

task2:  lineifile regex=<pattern> 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


2013/12/11 <iqbal....@i.edu.my>

--
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.
For more options, visit https://groups.google.com/groups/opt_out.

Michael DeHaan

unread,
Dec 11, 2013, 4:07:31 PM12/11/13
to ansible...@googlegroups.com
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



--
Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

Reply all
Reply to author
Forward
0 new messages