Monday, July 20, 2015

Bootstrapping Chef client on Azure Virtual machine

Using PowerShell cmdlets we can bootstrap chef client on Azure virtual machines. Once bootstrapped these virtual machines can be managed from Chef server and bring up to the desired state based on the configured run list.

The Set-AzureVMChefExtension cmdlet can be used to add a chef client to the virtual machine by providing the details. Let’s see how we can create a VM on Azure and then install a chef client on the machine using PowerShell.

$VMName = "demochefvm01"
$CSName = "demochefcs01"
$StorageName = "demochefstr01"
$Username = "adminuser"
$Password = "mysupersecretpassword"
$ImageName = "a699494373c04fc0bc8f2bb1389d6106__Windows-Server-2012-Datacenter-201506.01-en.us-127GB.vhd"

$Key = "C:\chef-repo\.chef\demoorg01-validator.pem"
$Server = "https://manage.chef.io/organizations/demoorg01"
$ClientName = "demoorg01-validator"

New-AzureStorageAccount -StorageAccountName $StorageName -Label $StorageName -Location "West Europe"

Set-AzureSubscription -SubscriptionName 'Prajeesh Prathap' -CurrentStorageAccount $StorageName

$VM  =  New-AzureVMConfig -Name $VMName `
            -InstanceSize ExtraSmall `
            -ImageName $ImageName | `
        Add-AzureProvisioningConfig -Windows `
            -AdminUsername $Username `
            -Password $Password | `
        Add-AzureDataDisk -CreateNew `
            -DiskSizeInGB 200 `
            -DiskLabel "datadisk1" `
            -LUN 0 | `
        Add-AzureEndpoint -Name "HTTP" `
            -Protocol TCP `
            -LocalPort 80 `
            -PublicPort 80 | `
        Add-AzureEndpoint -Name "PowerShell" `
            -Protocol TCP `
            -LocalPort 5986 `
            -PublicPort 5986
New-AzureVM -ServiceName $CSName -VMs $VM

Get-AzureVM -ServiceName $CSName `
            -Name $VMName | `
        Set-AzureVMChefExtension `
            -Windows `
            -AutoUpdateChefClient `
            -ValidationPem $Key `
            -ChefServerUrl $Server `
            -ValidationClientName $ClientName | `
        Update-AzureVM


The script will create a VM and install Chef client on the VM. You can now open the Azure Portal and Chef server to see the details of the node.


No comments: