Search code examples
linuxazurechef-infrayumrecipe

Install Azure CLI on VM using Chef


I am currently writing a script(recipe) with Chef that executes on an Azure (Linux) virtual machine. For my purposes I need to install Azure CLI on that VM. I followed these steps from the documentation. Unfortunately, I get the error that I can't use "sudo", and if I remove "sudo" it can't find "azure-cli". That part of the recipe looks like this:

cli = 'echo -e "[azure-cli] \ nname = Azure CLI \ nbaseurl = https: //packages.microsoft.com/yumrepos/azure-cli \ nenabled = 1 \ ngpgcheck = 1 \ ngpgkey = https: //packages.microsoft.com/keys/microsoft.asc "> /etc/yum.repos.d/azure-cli.repo ' 

execute "Install Azure CLI 1" do
    command "sudo rpm --import https://packages.microsoft.co/keys/microsoft.asc"
end

execute "Install Azure CLI 2" do
    command "sudo sh -c " + cli
end

execute "Install Azure CLI 3" do
    command "yes | sudo yum install azure-cli"
end

How can I make it work? Any help or suggestions would be highly appreciated!


Solution

  • You have basically Chef'ified a Shell script. IMO that is not the ideal way. Take some time to familiarize yourself with Chef Resources to get the job done.

    For example:

    At the very minimum, something like below:

    yum_repository 'azure-cli' do
      description 'Azure CLI'
      baseurl 'https://packages.microsoft.com/yumrepos/azure-cli'
      gpgkey 'https://packages.microsoft.com/keys/microsoft.asc'
    end
    
    yum_package 'azure-cli'