3 Ways to Modify Files with Ansible: lineinfile, replace, and blockinfile

Ansible is a powerful and flexible automation tool, capable of handling many complex tasks, including modifying files.

Three of the key modules that Ansible provides for file modification are lineinfile, replace, and blockinfile.

  • The lineinfile module is used when you need to manage lines in text files. It can ensure a particular line is present or absent in the file.
  • The replace module is similar to lineinfile, but it operates on complete file content or can replace all instances of a specified pattern within a file.
  • The blockinfile module is used when you want to insert/update/remove a block of lines in a file.

 

 

The lineinfile module – add, remove, or replace a line in a text file

The lineinfile module is an important module when dealing with text files. It is mainly used when you want to add, remove, or replace a line in a text file.

Here are some examples of how to use the lineinfile module:

  • Example 1: Ensure a particular line is present in a file:
    - name: add a particular line from a file
    ansible.builtin.lineinfile:
    path: /etc/somefile.conf
    regexp: 'Ensure this line from the file'
    state: present

    In this example, /etc/somefile.conf is the file to check, and ‘Ensure this line is in the file’ is the line to ensure is present. If the line is not in the file, Ansible will add it. If the line is already in the file, Ansible will not do anything.

  • Example 2: Remove a line from a file:
     - name: Remove a particular line from a file
    ansible.builtin.lineinfile:
    path: /etc/somefile.conf
    line: 'Remove this line from the file'
    state: absent

    In this example, /etc/somefile.conf is the file to check, and ‘Remove this line from the file’ is the line to remove. If the line is in the file, Ansible will remove it. If the line is not in the file, Ansible will not do anything.

  • Example 3: Remove a line from a file with regexp parameter
    - name: Remove a particular line from a file
    ansible.builtin.lineinfile:
    path: /etc/somefile.conf
    regexp: '^Remove this line from the file'
    state: absent


    In this example, if a line starting with ‘^Remove this line from the file’ exists, it will be removed.

    See also: Mastering the Linux Command Line — Your Complete Free Training Guide



  • Example 4: Replace a line in a file:
    - name: Replace a line in a file
    ansible.builtin.lineinfile:
    path: /etc/somefile.conf
    regexp: '^#Ensure this line is in the file'
    line: 'Ensure this line is in the file'


    In this example, if a line starting with ‘#Ensure this line is in the file’ exists, it will be replaced with ‘Ensure this line is in the file’. If no such line exists, the line ‘Ensure this line is in the file’ will be added.



     

  • Example 5: Insert a line after a match:
    - name: Insert a line after a match
    ansible.builtin.lineinfile:
    path: /etc/somefile.conf
    insertafter: 'Some existing line'
    line: 'New line to insert'

    In this example, /etc/somefile.conf is the file to modify, ‘Some existing line’ is the line to match, and ‘New line to insert’ is the line to insert after the matching line.


    The insertafter parameter can also accept regular expressions. If a line matching the regular expression exists, the new line will be inserted after it. If no match exists, the line will be added at the end of the file.


     

The replace module – replace all occurrences of a text pattern, not just a single line

The replace module allows for replacing all instances of a pattern within a file. It’s useful when you want to replace all occurrences of a text pattern, not just a single line.

Here are some examples of how to use the replace module:

  • Example 1: Replace all instances of a particular string in a file:
    - name: Replace all instances of a string in a file
    ansible.builtin.replace:
    path: /etc/somefile.conf
    regexp: 'Old string'
    replace: 'New string'


    In this example, /etc/somefile.conf is the file to modify, ‘Old string’ is the string to replace, and ‘New string’ is the string to replace it with.


    The regexp parameter is a regular expression that matches the string to replace. The replace parameter is the string to replace it with.


    The replace module will replace all instances of the string that match the regular expression. If no match exists, Ansible will not do anything.

  • Example 2: Replace a pattern using a backreference:
    - name: Swap name and age using backreferences
    replace:
    path: /path/to/file.txt
    regexp: 'name=(\w+) age=(\d+)'
    replace: 'age=\2 name=\1'

    In this example:


    (\w+) captures any word character (equivalent to [a-zA-Z0-9_]) one or more times. This captures the name.

    (\d+) captures any digit (equivalent to [0-9]) one or more times. This captures the age value.


    In the replace parameter:


    \1 refers to the first captured group (name).

    \2 refers to the second captured group (age).


    Thus, using backreferences, you can easily swap, reorder, or utilize captured groups in your replacement patterns.

  • Example 3: Replace a pattern using a dictionary of replacements:
    replacements:
    PLACEHOLDER_1: value1
    PLACEHOLDER_2: value2
    PLACEHOLDER_3: value3
    - name: Replace placeholders using a dictionary
    replace:
    path: /path/to/config/file.conf
    regexp: "{{ item.key }}"
    replace: "{{ item.value }}"
    loop: "{{ replacements | dict2items }}"

    In this example:


    dict2items is a Jinja2 filter that converts a dictionary into a list of key-value pairs.


    The loop iterates over each key-value pair in the dictionary. {{ item.key }} and {{ item.value }} refer to the current key and value, respectively, during each iteration of the loop.

The blockinfile module – insert update or remove a block of lines in a file

The blockinfile module is very useful when you want to insert/update/remove a block of lines in a file.

Here are some examples of how to use the blockinfile module:

  • Example 1: Insert a block of text:
    - name: Insert/update "my block" in a file
    ansible.builtin.blockinfile:
    path: /etc/myfile.conf
    block: |
    This is the content
    of my block
    marker: "# {mark} My Block"

    In this example, the block of text is inserted into /etc/myfile.conf. The marker line is used to identify the block of text for future replacement.

  • Example 2: Insert a block of text after a marker:
    - name: Insert a block of lines after a specific line
    ansible.builtin.blockinfile:
    path: /etc/myfile.conf
    insertafter: '^This is the line after which to insert'
    block: |
    This is the block to insert
    It has multiple lines

     

  • Example 3: Remove a block of text:
    - name: Remove block of text
    ansible.builtin.blockinfile:
    path: /path/to/your/file
    state: absent
    marker: "# {mark} ANSIBLE MANAGED BLOCK"

    In this example, the block identified by the marker ‘# {mark} My Block’ is removed from the file.

  • Example 4: Insert a block of text only if it does not already exist:
     name: Insert a block at the beginning of a file
    ansible.builtin.blockinfile:
    path: /etc/myfile.conf
    insertbefore: BOF
    block: |
    This is the block to insert
    It goes at the start of the file


    In this example, the block is inserted at the beginning of the file. The insertbefore parameter is set to ‘BOF’, which stands for Beginning Of File.



These are three powerful tools within Ansible that allow for efficient and precise file modification. By understanding and using these modules, you can automate and simplify many file-related tasks in your infrastructure.

David Cao
David Cao

David is a Cloud & DevOps Enthusiast. He has years of experience as a Linux engineer. He had working experience in AMD, EMC. He likes Linux, Python, bash, and more. He is a technical blogger and a Software Engineer. He enjoys sharing his learning and contributing to open-source.

Articles: 538

One comment

Leave a Reply

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