Bobcares

Ansible Blockinfile: Explained

by | Nov 7, 2022

Let us learn more about the ansible blockinfile configuration and its functions with the support of our Server Management Support Services at Bobcares.

 

What is Ansible blockinfile?

ansible blockinfile

The blockinfile module of Ansible can insert, update or remove a block of lines. To make the task idempotent, the block surrounds itself with a marker, such as starting and end.  

 

Ansible blockinfile module has the default installation of ansible. It must deal with and manage the inserting, updating, or removing a block of lines from the file on the remote nodes. These blocks are within the makers such as begin and end, and these can also be custom markers, and the module will help the user to deal with multiple file types.      

 

syntax:

 

We don’t need to provide the collections because Ansible’s module syntax utilizes the command blockinfile, which is also the short name for the module:

 

Parameters of Ansible Blockinfile 1:

attributes: (Type: string)

 

aliases: attr

  • The attribute that should be assigned to the resultant files or folders.
  • This string should have the same order as that shown in Isattr.
  • The operator = is assumed to be the default; otherwise, the operators + or – should be included in the string.

backup: (Type: Boolean)

 

choices: no (default) / yes

  • If the option is yes, a backup file with the date is created so that we can reverse it if something goes wrong with the original file.

block: (Type: string).

 

choices: “ ” (default)

 

aliases: content

 
  • If the text is not entered inside the marker line, the block will be eliminated as the state = absence.

create : (Type: Boolean)

  • choices: no (default) / yes
  • I fit does not exist it will immediately create a new file group: (Type: string)
  • The name of the group that the file or directory should be a member of.
 

2: Parameters

insertafter: (Type: string)

 

choices: EOF (default) / *regex*

  • In supply, the block will place after the final match of the regular expression if there is no detachment of the no starting or ending marker lines.
  • If there are no matches in the given regular expression, it will use the EOF instead. 

insertbefore: (Type: string)

 

choices: BOF (default) / *regex*

  • If the supply happens, it will place the block before the last match of the regular expression if no it will detach the  starting or ending marker lines.
  • If there are no matches for the regular expression, the block will attach to the end of the file.

marker: (Type: string)

 

choices: “# {mark} ANSIBLE MANAGED BLOCK” (default)

  • The values in marker_begin (default = ”BEGIN”) and marker_end (default = “END”) replaces the {mark}.
  • Using a custom marker without the mark variable may result in the inserting of the blocks twice on subsequent playbook executions. 

marker_begin: (Type: string)

 

choices: “BEGIN”

  • Kept at the mark in the block marker’s opening.’

marker_end: (Type: string)

 

choices: “END”

  • This will insert itself at {mark} in the closing of the ansible marker.

mode: (Type: raw)

 

The resultant file or directory should have the following permissions: (like 0644, 01777). mode: (Type: raw)

 

path: (required) aliases: dest, destfile, name

  • The file is to be changed. The aliases names were used prior to Ansible 2.3. owner: (Type: string)
  • The name of the owner that the file or directory should have, as passed to chown.
 

How does the blockinfile module work in Ansible?

Using the block option shown in the example, an Ansible block in a file modifies several blocks of lines.

 

We can change any type of file, including properties, configuration, XML, txt, HTML, and so on. We may also set the marker (comment) when modifying the file. As an example:

 

#BegiN ANSIBLE MANAGED BLOCK
Match User ansible-agent
#END ANSIBLE MANAGED BLOCK

 

We can change this marker. After we write the block, it checks to see whether the content exists, and if so, it changes the content; otherwise, it adds material to the file.

 

Sadly, this module is not available for Windows targets. It will add the block content at the end of the file by default.

 

Inserting a Block of Lines using ansible blockinfile:

In this example, we will use the Ansible blockinfile module to add a block of lines to a file. The text file abc.txt is located here.

 

Consider that we have typed some names in this file, as shown below:

 

Step 1

[root ~]# cat abc.txt
Liam
Noah
William
James
Oliver
Benjamin
Elijah
Lucas

Step 2

After completing the first step of the process we have to use the Ansible blockinfile module to add some command lines to this file as shown below:

 

[root ~]# cat blockinfile.yml
---
- hosts: localhost gather_facts: no

 

tasks:
- name: add block of lines to a file.
blockinfile:
path: /root/abc.txt
block: |
Hi this is decoding devops
Check out my blog for all devops tools
backup: yes

 

Along with the blockinfile module, we may use some more parameters to add blocks of lines to a file, which are path,block,and backup.

 

path: This is the path of the file that we wish to change.

 

block: This is where we will write the lines of code (content) that we wish to add to the file.

 

If we specify backup: it will create one file in the current directory with a date. It will include the content of the previous file. We can now make a backup of the file in this manner.

 

output

[root ~]# ansible-playbook blockinfile.yml [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

 

PLAY [localhost] *********************************************************************************************************************************************************** TASK [add block of lines to a file.] *************************************************************************************************************************************** changed: [localhost] PLAY RECAP ***************************************************************************************************************************************************************** localhost : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

 
 

After Lucas, it will upload new lines to the file, and we can see one backup file, abc.txt. The time is vvv:22:38.

 

[Need assistance with similar queries? We are here to help]

 

Conclusion

To conclude we have now learned more about the ansible blockinfile its uses and the configurations associated with it with the support of our server management support Services at Bobcares. 

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

     

0 Comments

Submit a Comment

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

Never again lose customers to poor
server speed! Let us help you.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF