Ansible Blockinfile Examples
Ansible Blockinfile Examples
Ansible blockinfile module is used to insert/update/remove a block of lines in a file. This module will insert/update/remove a block of multi-line text surrounded by customizable marker lines. in this blog post i will show you list of examples to insert/update/remove lines from a file using ansible blockinfile module.
Inserting a Block of Lines using ansible blockinfile:
in this example i am going to show you how to add block of lines to a file using ansible blockinfile module. Here i have text file called abc.txt. and in this file i written some names.
[root@ip-172-31-22-205 ~]# cat abc.txt Liam Noah William James Oliver Benjamin Elijah Lucas
now by using ansible blockinfile module i will add some lines to this file.
[root@ip-172-31-22-205 ~]# 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 blockinfile module we use some extra arguments to add block of lines to a file those are path,block,backup.
path: here we have to give the path of the file which you want to modify.
block: here you have to write your lines of code(content) which you want to add to the file
backup: if you mention backup yes, it will create one file in the current directory with timestamp. it will have the content of your old file. in this way we can take backup of your file.
By default, the block will be inserted at the end of the file.
output:
[root@ip-172-31-22-205 ~]# 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 [root@ip-172-31-22-205 ~]# cat abc.txt Liam Noah William James Oliver Benjamin Elijah Lucas # BEGIN ANSIBLE MANAGED BLOCK Hi this is decoding devops Check out my blog for all devops tools # END ANSIBLE MANAGED BLOCK [root@ip-172-31-22-205 ~]# ls abc.txt abc.txt.8576.2019-07-09@08:22:38~ blockinfile.yml [root@ip-172-31-22-205 ~]#
Here you can see in the file after Lucas new lines are added and you can see one backup file abc.txt.8576.2019-07-09@08:22:38~ is created.
- ansible blockinfile examples
- ansible blockinfile insertafter example
- best devops training in hyderabad
- best devops training in dilsukhnagar
- ansible blockinfile module
- best ansible training in hyderabad
- ansible blockinfile no marker
- ansible blockinfile module regex
- ansible blockinfile module replace