Add lines using Ansible

Ansible

  • Allows us to change same thing on multiple servers quickly and easily.

Install:

  • pip install --user ansible

How

  • "hosts" file where all server's ip written.
  • "filename.yml" code written to make changes in the above ip.
  • Inserting a line after a particular line.

Code

- hosts: all
  tasks:
  - name: line insert
    lineinfile:
      path: /path/to/your/file
      insertafter: 'add your line after this string match'
      line: 'your line or string'
    notify:
     - restart process_name
   become: true #allows command to run by sudo
  handlers:
   - name: restart process_name
     service:
       name: process_name
       state: restarted
     become: true

  • Spaces are sensitive
  • Save file with .yml
  • Execution: ansible-playbook -i hosts filename.yml