Search code examples
puppetmanifestini

Puppet - remove ini_setting completely


I am using puppet 4.5.3 and ini_setting module version 1.4.2. I need to be able to remove a stanza in an ini file. For example:

[header]
ip = '1.1.1.1'
hostname = 'myserver'
port = 80

I am able to remove each section of the ini file using the ensure => absent parameter but I cannot find a way to remove the stanza header, or preferably the whole thing in one command.

What I have left is

[header]

Does anyone know how this can be done? Unfortunately there are other stanzas in the same file that I need to keep so I cannot simply delete the file.

thanks,


Solution

  • Using the Augeas type:

    augeas { 'remove_ini_header':
      incl    => '/etc/example.ini',
      lens    => 'IniFile.lns_loose',
      changes => 'rm section[. = "header"]',
    }
    

    To break this down a bit, first I used the built-in IniFile.lns_loose lens (i.e. a "generic" loose parsing of INI files) and augtool to see the current state of the tree:

    $ augtool -t "IniFile.lns_loose incl /etc/example.ini"
    augtool> print /files/etc/example.ini
    /files/etc/example.ini
    /files/etc/example.ini/section = "header"
    /files/etc/example.ini/section/ip = "'1.1.1.1'"
    /files/etc/example.ini/section/hostname = "'myserver'"
    /files/etc/example.ini/section/port = "80"
    

    The entire section is in one part of the tree, so calling rm for that section will delete the entire subtree.

    To match the header section, you need to search for nodes called section where the value (the right hand side) is header. The [. = "header"] part of the command is a path expression that filters for nodes with the value header.