Search code examples
configurationpuppetiniaugeas

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


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?


Solution

  • 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.