Banthita Limwilai
Banthita Limwilai

Reputation: 181

How to check if package is installed by using unless?

I've created a puppet script to install Azure client and in the last step before using yum install, I want to make sure that the package haven't installed before for prevent from duplicate install.

My concept is Execute the script if the output from az --help give nothing (which mean there's no Azure install)

or if you guys have any better choices please guide me, thanks!

And my code is

#install azure client
        exec { 'install-azure':
        command         => '/bin/yum install azure-cli -y',
        path            => '/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:',
        unless          => 'az --help',
        #require         => Exec['yumrepolist']
        }

It seems like there're something wrong with the code, I've checked a particular agent by using

puppet agent -t

Notice: /Stage[main]/Os_preparation::Azure_install/Exec[yumrepolist]/returns: executed successfully Error: /Stage[main]/Os_preparation::Azure_install/Exec[install-azure]: Could not evaluate: Could not find command 'az'

Any ideas? Thanks

Upvotes: 0

Views: 210

Answers (1)

Jon
Jon

Reputation: 3671

You should install azure-cli using a package resource. Also, you should add its Yum repository as a yumrepo resource.

Try something like the following, which replicates the instructions on https://learn.microsoft.com/en-us/cli/azure/install-azure-cli-yum?view=azure-cli-latest.

yumrepo { 'azure-cli':
  descr    => 'Azure CLI',
  baseurl  => 'https://packages.microsoft.com/yumrepos/azure-cli',
  enabled  => 1,
  gpgcheck => 1,
  gpgkey   => 'https://packages.microsoft.com/keys/microsoft.asc',
}

package { 'azure-cli':
  ensure  => installed,
  require => Yumrepo['azure-cli'],
}

Upvotes: 1

Related Questions