12

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?

kenorb
  • 155,785
  • 88
  • 678
  • 743
mibutec
  • 2,929
  • 6
  • 27
  • 39
  • 1
    As an update, it looks like puppet now includes or has available something called augeas which is specifically designed to do this sort of thing. I have not actually used it myself though, so not sure how easy it is to work with. – Lee Lowder Jun 21 '12 at 16:53

4 Answers4

37

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",
}
mc0e
  • 371
  • 3
  • 2
31

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.
freiheit
  • 4,976
  • 37
  • 35
  • There are too many refrences available in Puppetforge for the augeas now the thing is which one to use, also I used this https://forge.puppetlabs.com/camptocamp/augeas @freiheit – Ankur Verma Mar 12 '14 at 10:35
  • @ankurverma: That particular puppet module will do the trick. What I actually meant, however, wasn't the puppet module, but the ruby library. Depending on your system, a package with a name like "ruby-augeas", "rubygem-ruby-augeas" or "libaugeas-ruby". The puppet module you found on rubyforge appears to correctly install those, so as long as you include it on master and client configurations, it will work. – freiheit Mar 12 '14 at 16:52
  • I installed the module I mentioned and is trying to use the augeas as in this file: https://db.tt/CZMgwmuF Please look into this and shade some light as I am not understanding why this is not working, also in my test node File resource is executing but the Augeas is not, one more thing is that since Augeas is not working I am not getting the updated configuration file. – Ankur Verma Mar 13 '14 at 05:28
  • @ankurverma: You should really ask a new question. What you're doing in that example isn't going to work well because you're telling puppet to do two conflicting things with the same file. – freiheit Mar 13 '14 at 16:57
6

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.

ryanneufeld
  • 157
  • 1
  • 4
  • One can also use the `match` parameter of `file_line` to ensure only one line `upload_max_filesize=...` exists. See [the documentation](https://forge.puppetlabs.com/puppetlabs/stdlib#resources) – Mifeet Jul 13 '15 at 09:24
2

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.

Nick F
  • 9,781
  • 7
  • 75
  • 90