Ansible: replace one line of code in multiple files in a directory

We are going to use lineinfile module and register statement.

Task that ‘ls’ the directory, and store in register statement.

- name: list of the .conf files and store it in register
  raw: find /etc/httpd/conf.d -type f -name "*.conf"
  register: certs_dir
  tags: update-cert

Task that replace one line on every *.conf file.

- 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

Test

ansible-playbook -i hosts sites.yaml -u root --ask-pass --tags update-cert

5 thoughts on “Ansible: replace one line of code in multiple files in a directory

  1. Kohl’s conducting a customer feedback survey. They are allowing customers a chance to leave feedback on their shopping experience. The company uses this information to improve their rules and requirements to help satisfy customers needs.
    As a reward for completing the survey at https://www.kohlsfeedbackscom.com/, you will receive a printable coupon for $5 off your next in-store purchase.

  2. Do take the Lowes monthly sweepstakes at https://www.lowescomsurvey.com.co/ official website. Enter the survey portal with your cash register receipt & fill in the necessary details to proceed further. Kindly share your experiences at the store. After successfully sharing your experiences at the store, you will enter into the $500 sweepstakes. If you are lucky enough at the monthly draw, then you will win the $500 check from Lowe’s. Lowe’s sponsors will inform the winner. Thank you – regards.

Leave a Reply

Your email address will not be published. Required fields are marked *