Editing INI style files with puppetlabs-inifile
INI files are used throughout many systems, Puppet uses INI syntax for the puppet.conf
file. The puppetlabs-inifile
module creates two types, ini_setting
and ini_subsetting
, which can be used to edit INI style files.
Getting ready
Install the module from the forge as follows:
t@mylaptop ~ $ puppet module install puppetlabs-inifile Notice: Preparing to install into /home/tuphill/.puppet/modules ... Notice: Downloading from https://forgeapi.puppetlabs.com ... Notice: Installing -- do not interrupt ... /home/tuphill/.puppet/modules └── puppetlabs-inifile (v1.1.3)
How to do it...
In this example, we will create a /tmp/server.conf
file and ensure that the server_true
setting is set in that file:
- Create an
initest.pp
manifest with the following contents:ini_setting {'server_true': path => '/tmp/server.conf', section => 'main', setting => 'server', value => 'true', }
- Apply the manifest:
t@mylaptop ~/.puppet/manifests $ puppet apply initest.pp Notice: Compiled catalog for burnaby in environment production in 0.14 seconds Notice: /Stage[main]/Main/Ini_setting[server_true]/ensure: created Notice: Finished catalog run in 0.02 seconds
- Verify the contents of the
/tmp/server.conf
file:t@mylaptop ~/.puppet/manifests $ cat /tmp/server.conf [main] server = true
How it works...
The inifile
module defines two types, ini_setting
and ini_subsetting
. Our manifest defines an ini_setting
resource that creates a server = true setting within the main section of the ini
file. In our case, the file didn't exist, so Puppet created the file, then created the main
section, and finally added the setting to the main
section.
There's more...
Using ini_subsetting
, you can have several resources added to a setting. For instance, our server.conf
file has a server's line, we could have each node append its own hostname to a server's line. Add the following to the end of the initest.pp
file:
ini_subsetting {'server_name': path => '/tmp/server.conf', section => 'main', setting => 'server_host', subsetting => "$hostname", }
Apply the manifest:
t@mylaptop ~/.puppet/manifests $ puppet apply initest.pp Notice: Compiled catalog for mylaptop in environment production in 0.34 seconds Notice: /Stage[main]/Main/Ini_subsetting[server_name]/ensure: created Notice: Finished catalog run in 0.02 seconds t@mylaptop ~/.puppet/manifests $ cat /tmp/server.conf [main] server = true server_host = mylaptop
Now temporarily change your hostname and rerun Puppet:
t@mylaptop ~/.puppet/manifests $ sudo hostname inihost t@mylaptop ~/.puppet/manifests $ puppet apply initest.pp Notice: Compiled catalog for inihost in environment production in 0.43 seconds Notice: /Stage[main]/Main/Ini_subsetting[server_name]/ensure: created Notice: Finished catalog run in 0.02 seconds t@mylaptop ~/.puppet/manifests $ cat /tmp/server.conf [main] server = true server_host = mylaptop inihost
Tip
When working with INI syntax files, using the inifile
module is an excellent choice.
If your configuration files are not in INI syntax, another tool, Augeas, can be used. In the following section, we will use augeas
to modify files.