# win\_blockinfile module

**URL:** <https://forum.ansible.com/t/win-blockinfile-module/23127>\
**Category:** Ansible Developer\
**Tags:** windows\
**Created:** [November 29, 2016, 6:18pm UTC](https://forum.ansible.com/t/win-blockinfile-module/23127 "2016-11-29T18:18:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexander\_Luders](https://avatars.discourse-cdn.com/v4/letter/a/7ba0ec/32.png) [@Alexander\_Luders](https://forum.ansible.com/u/Alexander_Luders)\
**Post date:** [November 29, 2016, 6:18pm UTC](https://forum.ansible.com/t/win-blockinfile-module/23127/1 "2016-11-29T18:18:23Z")

</div>

Hi all,

do you know of an existing Windows port of the blockinfile module?  
Or is anybody already working on it?

If not, I’d like to go ahead and create a win\_blockinfile module.

Greets,  
Alex

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [December 6, 2016, 9:18am UTC](https://forum.ansible.com/t/win-blockinfile-module/23127/2 "2016-12-06T09:18:48Z")

</div>

Just catching up with the group messages. I haven’t heard of anyone doing this, so I reckon go ahead.  
I would just make sure you can’t already do what you need using template / win\_template before spending your time on this though.

Jon

---

<div class="post-metadata">

**Author:** ![Alexander\_Luders](https://avatars.discourse-cdn.com/v4/letter/a/7ba0ec/32.png) [@Alexander\_Luders](https://forum.ansible.com/u/Alexander_Luders)\
**Post date:** [December 31, 2016, 9:27am UTC](https://forum.ansible.com/t/win-blockinfile-module/23127/3 "2016-12-31T09:27:34Z")

</div>

Hi Joe,

thanks for your reply and sorry that I am catching up so late.

I thought about using win\_template but I does not work for me, as I do not have all the necessary input at that point to assemble the whole file myself. It would require shifting quite some application knowledge which is just not feasible (yet).  
Nevertheless, thanks for the suggestion.

Instead I started working on the win\_blockinfile module. It is basically a Windows port of the existing blockinfile Unix module. Some features are stripped like the permission stuff or the validation of the modified file.  
For the backup functionality I was looking for a powershell helper function for creating backups like there is with the python method AnsibleModule#backup\_local (basic.py). I currently copied the function win\_lineinfile#BackupFile, which I do not like at all. Is there a central location for utility powershell functions like this?  
On your ‘Developing Modules’ page in appendix ‘Module Utilities’ you list a bunch of python utilities but only one powershell utility file: powershell.ps1.  
Is there anything else? Could you give me some advice here?

Anyway, I progressed quite far. Most of the implementation is done, tests as well…

Greets,  
Alex

---

<div class="post-metadata">

**Author:** ![user80](https://avatars.discourse-cdn.com/v4/letter/u/e480ec/32.png) [@user80](https://forum.ansible.com/u/user80)\
**Post date:** [May 16, 2019, 3:00pm UTC](https://forum.ansible.com/t/win-blockinfile-module/23127/4 "2019-05-16T15:00:06Z")

</div>

Hi Alex!

I really appreciate if you can share your module.

I couldn’t find any solution to my problem of block insertion in arbitrary existing file on windows host.

---

<div class="post-metadata">

**Author:** ![Alexander\_Luders](https://avatars.discourse-cdn.com/v4/letter/a/7ba0ec/32.png) [@Alexander\_Luders](https://forum.ansible.com/u/Alexander_Luders)\
**Post date:** [May 22, 2019, 5:40pm UTC](https://forum.ansible.com/t/win-blockinfile-module/23127/5 "2019-05-22T17:40:18Z")

</div>

Sure, I can share it on Github.  
It was developed quite some time ago and was using lineinfile windows module as a basis.  
It is working and actively used but some things are missing:

- Check fixes to lineinfile module and see if they are relevant for blockinfile
- Tests probably need to be updated

I always wanted to share it, so maybe we can move it forward together.  
I will post the Github link once I’ve shared it.
