EagleDev
EagleDev

Reputation: 1865

Appropriate way to run a block of code in Chef recipe

I can't seem to find a proof of running a block of shell code in Chef. For example, I have a script below:

for user in `awk -F: '($3 < 500) {print $1 }' /etc/passwd` ; do
    if [ $user != "root" ]; then
      usermod -L $user 
      if [ $user != "sync" ] && [ $user != "shutdown" ] && [ $user != "halt" ];
then 
        usermod -s /sbin/nologin $user
        fi
      fi
done

..and I doubt the way to run this block is as follows

bash 'run script' do
  code <<-EOH
        "for user in `awk -F: '($3 < 500) {print $1 }' /etc/passwd` ; do
            if [ $user != "root" ]; then
            usermod -L $user
            if [ $user != "sync" ] && [ $user != "shutdown" ] && [ $user != "halt" ];
        then
                usermod -s /sbin/nologin $user
            fi
        fi
    done"
 EOH
end

But I'm really unsure if this construct is valid.

Upvotes: 0

Views: 502

Answers (1)

coderanger
coderanger

Reputation: 54181

You don't need the extra "" on the outside, <<-EOH ... EOH is already a kind of quote, called a heredoc.

Upvotes: 1

Related Questions