# editing python configobj format config files

**URL:** <https://forum.ansible.com/t/editing-python-configobj-format-config-files/28083>\
**Category:** Ansible Project\
**Created:** [July 18, 2018, 8:11pm UTC](https://forum.ansible.com/t/editing-python-configobj-format-config-files/28083 "2018-07-18T20:11:10Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vince\_Skahan](https://avatars.discourse-cdn.com/v4/letter/v/5e9695/32.png) [@Vince\_Skahan](https://forum.ansible.com/u/Vince_Skahan)\
**Post date:** [July 18, 2018, 8:11pm UTC](https://forum.ansible.com/t/editing-python-configobj-format-config-files/28083/1 "2018-07-18T20:11:10Z")

</div>

I’m trying to figure out how to reliably edit a configobj format [(link)](http://www.voidspace.org.uk/python/articles/configobj.shtml#sections-and-writing) file that unfortunately has a bunch of blocks with the same “key = value” combinations initially after the piece of software is installed, so a simple in-line sed or equivalent is not a reasonable solution. Note this is ‘not’ an ini file format so that module isn’t an option for this case.

Looking for suggestions…

Example config file, lightly obfuscated for brevity:

> [Section1]
> 
> [[Thing1]]
> 
> # This section is for configuring posts to Thing1.

> enable = false
> 
> username = replace\_me
> 
> password = “replace\_me”
> 
> [[Thing2]]
> 
> # This section is for configuring posts to Thing2.
> 
> enable = false
> 
> username = replace\_me

password = “replace\_me”

> [[Thing3]]
> 
> # This section is for configuring posts to Thing3.
> 
> enable = false
> 
> username = replace\_me
> 
> password = “replace\_me”

The order of the [SectionN] blocks can be random. The order of the [[Thing1]] blocks ‘within’ a [SectionN] block can be random. The order of the entries within a [[ThingN] block can be random. The number of [[ThingN]] items within a [SectionN] block is not fixed nor predictable.

So for discussion, assume I want to configure [[Thing2]] enabled and set its values. I’m thinking something like…

> - name: configure [[Thing2]]  
> blockinfile:  
> path: /my/filename.txt

insertafter: “[Section1]”

> content: |

> > [[Thing2]]

> > > # This section is for configuring posts to Thing2.
> 
> > > enable = true
> 
> > > username = myuserhere
> 
> > password = “mypasshere”

This would ‘add’ a block, but how would I delete a pre-existing unconfigured [[Thing2]] block and do this in an idempotent way?

Or am I just overthinking and there’s a simpler way ?

Suggestions appreciated…
