Search code examples
mysqlexecpuppetsystem-administration

Cant write to file with exec


I am working with a puppet class which write to my.cnf if specified line isn't there, and it's not working. Here is the code:

class mysql-server::configure {
        exec { "enable_binlog":
        path => "/usr/bin/:/usr/sbin/:/usr/local/bin:/bin/:/sbin",
        command => "echo 'log_bin=/var/log/mysql/mysql-bin.log' >> /etc/mysql/my.cnf",
        onlyif => "grep -c log_bin=/var/log/mysql/mysql-bin.log' /etc/mysql/my.cnf",
}
}

Solution

  • I believe that your onlyif query is wrong.

    While grep -c prints a 0 if no matching line is found, it still returns 1.

    How about

    unless => 'grep -q log_bin=/var/log/mysql/mysql-bin.log /etc/mysql/my.cnf'
    

    Note that you probably want to use the file_line type from the stlib module to do the same thing more efficiently.