官术网_书友最值得收藏!

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:

  1. Create an initest.pp manifest with the following contents:
      ini_setting {'server_true':
        path    => '/tmp/server.conf',
        section => 'main',
        setting => 'server',
        value   => 'true',
      }
  2. 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
    
  3. 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.

主站蜘蛛池模板: 济宁市| 兴城市| 肃北| 济南市| 楚雄市| 黑龙江省| 苍山县| 安顺市| 高雄市| 沂水县| 镇康县| 玉门市| 都江堰市| 新兴县| 尼勒克县| 新丰县| 涿州市| 浮山县| 柘荣县| 黄浦区| 乐至县| 称多县| 石首市| 龙川县| 易门县| 大理市| 卓资县| 淄博市| 石首市| 务川| 石棉县| 香河县| 库尔勒市| 桐庐县| 阳春市| 和龙市| 开平市| 延庆县| 平阳县| 浮山县| 弋阳县|