Updated March 31, 2023
Introduction to Ansible blockinfile
Ansible blockinfile module, which is a part of the ansible-base and comes with the default installation of the ansible, which is responsible to insert, update or remove a block of lines (multi-line text) from the file on the remote nodes, and these blocks are surrounded by the marker like begin and end which can be a custom marker and this module helps to work with the different types of the files.
syntax:
Ansible uses command blockinfile for its module syntax, and it is the short module name, so we don’t need to specify the collections:
Parameters:
- attributes: (Type: string)
- aliases: attr
- The attribute that the resulting files or the directories should have.
- This string should contain the same order as the one displayed in Isattr.https://man7.org/linux/man-pages/man1/lsattr.1.html
- The operator = is considered a default, otherwise + or – should be included in the string.
- aliases: attr
- backup: (Type: Boolean)
- choices: no (default) / yes
- If selected yes, it creates a backup file with the timestamp so you can reverse it when anything goes wrong with the original file.
- choices: no (default) / yes
- block: (Type: string)
- choices: “ ” (default)
- aliases: content
- The text should be inserted inside the marker line, and if it is missing or the empty one, then the block will be removed as the state = absent.
- create : (Type: Boolean)
- choices: no (default) / yes
- creates a new file if it doesn’t exist.
- group: (Type: string)
- Name of the group that the file or directory should own.
- choices: no (default) / yes
- insertafter: (Type: string)
- choices: EOF (default) / *regex*
- If specified and no beginning or ending marker lines are found, the block will be inserted after the last match of the specified regular expression.
- If the specified regular expression has no matches, then EOF will be used instead.
- choices: EOF (default) / *regex*
- insertbefore: (Type: string)
- choices: BOF (default) / *regex*
- If specified and no beginning or ending marker lines are found, the block will be inserted before the last match of the specified regular expression.
- If the specified regular expression has no matches, then the block will be inserted at the end of the file.
- marker: (Type: string)
- choices: “# {mark} ANSIBLE MANAGED BLOCK” (default)
- {mark} will be replaced with the values in marker_begin (default = ”BEGIN”) and marker_end (default = “END”).
- Using a custom marker without the {mark} variable may insert the blocks repeatedly on the subsequent playbook runs.
- choices: “# {mark} ANSIBLE MANAGED BLOCK” (default)
- choices: BOF (default) / *regex*
- marker_begin: (Type: string)
- choices: “BEGIN”
- This will be inserted at {mark} in the opening of the block marker.
- choices: “BEGIN”
- marker_end: (Type: string)
- choices: “END”
- This will be inserted at {mark} in the closing of the ansible marker.
- choices: “END”
- mode: (Type: raw)
- Permission the resulting file or directory should have (like 0644, 01777).
- path: (required)
- aliases: dest, destfile, name
- The file to modify. Before Ansible 2.3, the aliases names were used.
- owner: (Type: string)
- Name of the owner that the file or directory should own, as would be fed to chown.
- aliases: dest, destfile, name
How does the blockinfile module work in Ansible?
Ansible block in a file updates the multiple blocks of lines using the block parameter that is explained in the example. You can update any kind of file like properties, configuration, XML, txt, HTML, etc. You can also set the marker (comment) like when the file modifies it sets. For example,
You can modify this marker.
Once you write the block, it checks if the content exists and if it exists, then it updates the content; otherwise, add content to the file.
Unfortunately, this module is not available for the windows targets.
By default, the block content will be inserted at the end of the file.
Examples of Ansible blockinfile
In the below-explained examples, we are not modifying the actual config file of the operating systems or any other configurations so that we have copied on the different temp folders. It is also advised when you test that do not modify the default configuration settings, which can corrupt your system settings unless it is necessary.
Example1: Ansible playbook to update the ssh_config file.
The below playbook will add the two lines into the ssh_config file
---
- name: Ansislble playbook to test the block in file module.
hosts: localhost
tasks:
- name: Insert / update match user configuration
blockinfile:
path: /tmp/ssh_config
block: |
Match User ansible-agent
PasswordAuthentication no
Output:
Ssh_config output:
To take the backup of the file before modifying it, use the backup parameter.
blockinfile:
path: /tmp/ssh_config
backup: yes
block: |
Match User ansible-agent
PasswordAuthentication no
When you don’t provide the block parameter, then the content inside the markup will be removed. For example,
---
- name: Ansislble playbook to test the block in file module.
hosts: localhost
tasks:
- name: Insert / update match user configuration
blockinfile:
path: /tmp/ssh_config
Output:
File output:
You can see the content inside the markup item is removed.
Example2: Update the Html file with the block and applying the customer marker.
The sample playbook is shown below.
Playbook:
---
- name: Ansible playbook to test the block in file module.
hosts: localhost
tasks:
- name: Insert/update the html content surrounded by the custom marker
blockinfile:
path: coffeeshop.html
marker: "<!-- {mark} ANSIBLE MANAGED BLOCK -->"
insertafter: "<body>"
block: |
<h1>Welcome to {{ ansible_hostname }}</h1>
<p>Last updated on {{ ansible_date_time.iso8601 }}</p>
Output:
Coffeeshop.html file output:
As you can see, we have applied here the custom marker, which mentions the start of the ansible block and the end of it. In between, we have added a few lines.
Conclusion
Ansible blockinfile module is very in dealing with the file content, and it inserts or updates the multiple lines together on the remote nodes. This is specifically useful when we have a task to configure settings for the software on multiple remote nodes or need to update the multiple lines of the files on the remote nodes.
Recommended Articles
This is a guide to Ansible blockinfile. Here we discuss How does the blockinfile module work in Ansible and Examples along with the codes and outputs. You may also look at the following articles to learn more –