How to set config=value in php.ini with Puppet?

14.3k views Asked by At

I'm doing my first steps in Puppet and ran into a problem. I've installed PHP on a Linux server and I want to do some slightly changes to php.ini file. I don't want to overwrite the whole ini file with one from repository, just change/create one simple config value.

I want to ensure, that the property upload_max_filesize in php.ini has the value of 10M.

How can I achieve this?

4

There are 4 answers

4
freiheit On BEST ANSWER

There's basically 3 options:

  1. Use augeas support in puppet (you'll need the ruby augeas libraries installed) like:

    augeas { "php.ini":
      notify  => Service[httpd],
      require => Package[php],
      context => "/files/etc/php.ini/PHP",
      changes => [
        "set post_max_size 10M",
        "set upload_max_filesize 10M",
      ];
    }
    

    You can use "augtool ls /files/etc/php.ini" to see the sections to understand how augeas is parsing the file and use that to work out the paths you need.

  2. You can use an exec. Something like:

    define set_php_var($value) {
      exec { "sed -i 's/^;*[[:space:]]*$name[[:space:]]*=.*$/$name = $value/g' /etc/php.ini":
        unless  => "grep -xqe '$name[[:space:]]*=[[:space:]]*$value' -- /etc/php.ini",
        path    => "/bin:/usr/bin",
        require => Package[php],
        notify  => Service[httpd];
      }
    }
    set_php_var {
      "post_max_size":       value => '10M';
      "upload_max_filesize": value => '10M';
    }
    

    Unfortunately, this solution doesn't understand the sections in php.ini, so adding a variable that's not already there would require extra effort. This will do the wrong thing if a variable appears in more than one section (but examples I'm looking at appear to have all unique variable names). This should work for a variable that's present but commented-out with a semi-colon.

  3. Copy the original php.ini file into your puppet repository and use file with source => 'puppet:///...' or content => template(...) to replace the file entirely, as you indicated you would prefer not to do.
6
mc0e On

My preferred option would be to leave php.ini alone, and have puppet create a file in php's conf.d directory to override the values you want to change.

The less changes you make to php.ini, the easier it is to see what's going on when you need to merge your changes with the package providers changes when you upgrade php.ini in future.

file {'/etc/php5/conf.d/upload_limits.conf':
  ensure => present,
  owner => root, group => root, mode => 444,
  content => "post_max_size = 10M \nupload_max_filesize = 10M \n",
}
1
Nick F On

An alternative approach, if you're using Apache as your web server, is to set the php variable in your Apache virtualhost file (which will probably be somewhere in your Puppet manifests directory).

For example:

<VirtualHost *:80>
  ServerName app.dev
  DocumentRoot /srv/app/public
  ## etc...

  php_value upload_max_filesize 10M

</VirtualHost>

This doesn't actually change php.ini, but - depending on your set-up - may be a simple way of achieving the same effect.

1
ryanneufeld On

You could also use the file_line resource found in the stdlib module.

file_line{ 'php_upload_max_filesize':
  path => '/path/to/php.ini',
  line => "upload_max_filesize = 10M",
}

Since this will append the line to the file if one exactly matching it does not exist, and since the last instance of a config value takes precedence over those earlier in the file it will work. This is how I do it when i only have a couple things to change.