Blockinfile module in ansible

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.

Now by using the ansible blockinfile module we will add some lines to the file.
#cat blockinfile.yaml
---
- hosts: linux
tasks:
- name: create file
file:
path: /home/ansible/blockinfile
state: touch
- name: add block of lines to a file.
blockinfile:
path: /home/ansible/blockinfile
block: |
Hi everyone
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

To run playbook,
# ansible-playbook blockinfile.yaml
PLAY [all] **************************************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************
ok: [192.168.1.27]

TASK [create file] ******************************************************************************************************************************************
changed: [192.168.1.27]

TASK [add block of lines to a file.] ************************************************************************************************************************
changed: [192.168.1.27]

PLAY RECAP **************************************************************************************************************************************************
192.168.1.27 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0



Recent Comments

No comments

Leave a Comment