puppetpuppet-enterprise

Manage multiple files permissions using puppet


How do I set file permission from list of file names from cat command?

For example, below command returns 3 file names:

$ cat /tmp/test | grep file
/etc/systemd/file_1.log
/etc/systemd/file_2.log
/etc/systemd/file_3.log

How do I use puppet to run the command, get the file names and then loop the 3 file names and set permission accordingly?


Solution

  • How do I set file permission from list of file names from cat command?

    There are two main alternatives, but I observe first that your example is of the output from grep, not cat, and that the cat in that example is superfluous. Nevertheless, those details don't change the big picture -- substantially the same approaches are applicable for data output by any command.


    It would be more idiomatic to write a custom fact that captures the filenames (as of the time of each catalog request), and use that information to create the appropriate File resources.

    Custom facts are not that hard, but the full details are more than would be appropriate for an SO answer. Supposing that you have a fact $facts['systemd_logs'] whose value is an array of the absolute filenames, you can compactly express the whole group of wanted File resources like so:

    file { $facts['systemd_logs']:
        mode => '0644',
    }
    

    (or whatever mode it is that you want).


    It would be quicker (and dirtier) to use an Exec resource to run an appropriate command:

    exec { 'ensure correct file permissions':
        command  => 'chmod 0644 $(/bin/grep file /tmp/test)',
        onlyif   => '/bin/grep -q file /tmp/test',
        provider => 'shell',
    }