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?
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.
var google_conversion_label = "owonCMyG5nEQ0aD71QM";
0 Comments