jmp242
jmp242

Reputation: 133

How do I get Puppet exec resource to determine if a Chocolatey source has already been disabled using the unless clause with the PowerShell provider?

The exec runs every time. When I manually execute the unless clause, it exits 0.

exec { 'disablepublicchocolatey':
    command => "C:/ProgramData/chocolatey/choco.exe source disable -n=chocolatey",
    unless  => "C:/ProgramData/chocolatey/choco.exe source list | findstr -i 'chocolatey [Disabled]'",
    require => Exec['chocolatelyinstall'],
    provider => powershell,
}

Is there a better way to check if a Chocolatey source is already disabled? Is the string matching not robust enough?


FYI, Windows 7 x64.

Upvotes: 3

Views: 1209

Answers (1)

ferventcoder
ferventcoder

Reputation: 12561

I believe this will work if you use the regular exec provider.

exec { 'disablepublicchocolatey':
  command => "C:/ProgramData/chocolatey/choco.exe source disable -n=chocolatey",
  unless  => "cmd /c C:/ProgramData/chocolatey/choco.exe source list | findstr -i 'chocolatey [Disabled]'",
  require => Exec['chocolateyinstall'],
}

If you use PowerShell, you should assign your output to a variable and then query whether the output string contains what you are looking for:

$sourceOutput = choco.exe source list
$sourceOutput.Contains('chocolatey [Disabled]')

UPDATE for the PowerShell One Liner

You can separate statements with semi-colon, so this should provide what you need

exec { 'disable_public_chocolatey':
    command => "C:/ProgramData/chocolatey/choco.exe source disable -n=chocolatey",
    unless  => "\$sourceOutput = choco.exe source list; if (\$sourceOutput.Contains('chocolatey [Disabled]')) {exit 0} else {exit 1}",
    require => Exec['chocolatelyinstall'],
    provider => powershell,
}

Update 2 - Chocolatey provider adding configuration support

This doesn't really help you right now, but something being added in the future is https://tickets.puppetlabs.com/browse/PUP-3428

The gist is:

chocolatey::source {'name':
  location => '',
  ensure => present|absent,
  enabled => true|false,
  username => '',
  password => '',
}

Which should lower the amount of work in configuring choco appropriately.

Update 3 - Chocolatey provider configuration support

I mentioned in Update 2 that the provider would add configuration support. The sources bit is in PR mode and working - https://github.com/chocolatey/puppet-chocolatey/pull/79/files

Update 4 - Chocolatey provider - chocolateysource

Back in 2016ish, the Chocolatey module was moved under Puppet supported (puppetlabs/chocolatey) and config settings, features, and sources support were added. Find that at https://forge.puppet.com/puppetlabs/chocolatey

Upvotes: 2

Related Questions