Puppet exec command with variable not executed

7,360

Your first problem is that you have your command parameter surrounded with single quotes ('), which inhibit variable expansion. If you have:

$location = "/path/to/target"

Then:

file { '$location':
  ensure => directory,
}

Will attempt to create a directory called "$location", whereas this:

file { "$location":
  ensure => directory,
}

Would actually try to create /path/to/target.

With that in mind, your exec resource should probably look like:

exec { 'git-wp':
  command => "git clone https://github.com/WordPress/WordPress ${location}",
  require => Package['git'],
}

Also, it's not necessary to pre-create the target directory; git will do this for you.

You can run puppet with --debug in order to see the actual error messages output by git.

Share:
7,360

Related videos on Youtube

kaiser
Author by

kaiser

You came here to know more about me? Here you go! I have been moderator on WordPress.StackExchange for several years. You can find me @unserkaiser on Twitter, where I also talk about building @gtmifylab, my current indie side project. Some of my older code bits can be found on GitHub franz-josef-kaiser. If you want to get in touch with me, then go with [email protected]. Please understand, that this isn't an additional support route and I won't answer such requests. Best wishes and keep building! Kaiser.

Updated on September 18, 2022

Comments

  • kaiser
    kaiser over 1 year

    I have a very simple Puppet (sub)module that should use Git to clone a repository from a remote location:

    class wppuppet::git(
      $location = '/var/www/wp'
    ) {
    
      file { $location:
        ensure => 'directory',
        mode   => '0755',
      }
    
      exec { 'git-wp':
        command => 'git clone https://github.com/WordPress/WordPress ${location}',
        require => Package['git'],
      }
    
      Package['git']
      -> File[ $location ]
      -> Exec['git-wp']
    }
    

    For some reason it constantly fails with the following error:

    Error: git clone https://github.com/WordPress/WordPress ${location} returned 128 instead of one of [0]
    Error: /Stage[main]/Wppuppet::Git/Exec[git-wp]/returns: change from notrun to 0 failed: 
    git clone https://github.com/WordPress/WordPress ${location} returned 128 instead one of [0]
    

    I tried it with ${location} as well as with $location, but the result stays the same.