normalian blog

Let's talk about Microsoft Azure, ASP.NET and Java!

Linux 版 PowerShell を利用して ARM テンプレートをデプロイしてみる

最近の Microsoftクロスプラットフォームに力を入れているのはよくご存じだと思う。ご存知の方も多いと思うが PowerShell on Linux and Open Source!PowerShell on Linuxオープンソース化のうえ、Linux で利用できるようになったことが発表されている。Linux on PowerShellHello World を行う程度のサンプルは多いが、実際に Azure Resource Manager Cmdlets を利用するまでのサンプルは少ないので、今回は CentOS 7.2 を対象として ARM テンプレートをデプロイするまでの疎通をとってみた。

PowerShell on Linux のインストール

まず yum を利用して PowerShell を以下のようのインストールする。

[azureuser@XXXXXXXXXXXX ~]$ sudo su -
[root@XXXXXXXXXXXX ~]# yum install https://github.com/PowerShell/PowerShell/releases/download/v6.0.0-alpha.14/powershell-6.0.0_alpha.14-1.el7.centos.x86_64.rpm
Loaded plugins: fastestmirror, langpacks
powershell-6.0.0_alpha.14-1.el7.centos.x86_64.rpm                                                       |  39 MB  00:00:06     
Examining /var/tmp/yum-root-pz_gPk/powershell-6.0.0_alpha.14-1.el7.centos.x86_64.rpm: powershell-6.0.0_alpha.14-1.el7.centos.x86_64
Marking /var/tmp/yum-root-pz_gPk/powershell-6.0.0_alpha.14-1.el7.centos.x86_64.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package powershell.x86_64 0:6.0.0_alpha.14-1.el7.centos will be installed
--> Processing Dependency: libicu for package: powershell-6.0.0_alpha.14-1.el7.centos.x86_64
base                                                                                                    | 3.6 kB  00:00:00     
extras                                                                                                  | 3.4 kB  00:00:00     
openlogic                                                                                               | 1.3 kB  00:00:00     
updates                                                                                                 | 3.4 kB  00:00:00     
(1/5): base/7/x86_64/group_gz                                                                           | 155 kB  00:00:00     
(2/5): openlogic/7/x86_64/primary                                                                       |  11 kB  00:00:00     
(3/5): extras/7/x86_64/primary_db                                                                       | 183 kB  00:00:00     
(4/5): updates/7/x86_64/primary_db                                                                      | 1.2 MB  00:00:00     
(5/5): base/7/x86_64/primary_db                                                                         | 5.6 MB  00:00:00     
Determining fastest mirrors
openlogic                                                                                                                56/56
--> Processing Dependency: libunwind for package: powershell-6.0.0_alpha.14-1.el7.centos.x86_64
--> Processing Dependency: uuid for package: powershell-6.0.0_alpha.14-1.el7.centos.x86_64
--> Running transaction check
---> Package libicu.x86_64 0:50.1.2-15.el7 will be installed
---> Package libunwind.x86_64 2:1.1-5.el7_2.2 will be installed
---> Package uuid.x86_64 0:1.6.2-26.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===============================================================================================================================
 Package          Arch         Version                              Repository                                            Size
===============================================================================================================================
Installing:
 powershell       x86_64       6.0.0_alpha.14-1.el7.centos          /powershell-6.0.0_alpha.14-1.el7.centos.x86_64       124 M
Installing for dependencies:
 libicu           x86_64       50.1.2-15.el7                        base                                                 6.9 M
 libunwind        x86_64       2:1.1-5.el7_2.2                      base                                                  56 k
 uuid             x86_64       1.6.2-26.el7                         base                                                  55 k

Transaction Summary
===============================================================================================================================
Install  1 Package (+3 Dependent packages)

Total size: 131 M
Total download size: 7.0 M
Installed size: 148 M
Is this ok [y/d/N]: y
Downloading packages:
(1/3): libunwind-1.1-5.el7_2.2.x86_64.rpm                                                               |  56 kB  00:00:00     
(2/3): uuid-1.6.2-26.el7.x86_64.rpm                                                                     |  55 kB  00:00:00     
(3/3): libicu-50.1.2-15.el7.x86_64.rpm                                                                  | 6.9 MB  00:00:00     
-------------------------------------------------------------------------------------------------------------------------------
Total                                                                                          7.1 MB/s | 7.0 MB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : 2:libunwind-1.1-5.el7_2.2.x86_64                                                                            1/4 
  Installing : uuid-1.6.2-26.el7.x86_64                                                                                    2/4 
  Installing : libicu-50.1.2-15.el7.x86_64                                                                                 3/4 
  Installing : powershell-6.0.0_alpha.14-1.el7.centos.x86_64                                                               4/4 
  Verifying  : libicu-50.1.2-15.el7.x86_64                                                                                 1/4 
  Verifying  : uuid-1.6.2-26.el7.x86_64                                                                                    2/4 
  Verifying  : 2:libunwind-1.1-5.el7_2.2.x86_64                                                                            3/4 
  Verifying  : powershell-6.0.0_alpha.14-1.el7.centos.x86_64                                                               4/4 

Installed:
  powershell.x86_64 0:6.0.0_alpha.14-1.el7.centos                                                                              

Dependency Installed:
  libicu.x86_64 0:50.1.2-15.el7             libunwind.x86_64 2:1.1-5.el7_2.2             uuid.x86_64 0:1.6.2-26.el7            

Complete!

上記のコマンド実行の完了後 /opt/microsoft/powershell 以下にバージョン毎にフォルダが作成されて PowerShell のモジュールが配置される。以下のコマンドで確認できる。

[root@XXXXXXXXXXXX ~]# ls /opt/microsoft/powershell/6.0.0-alpha.14/                          
(中略)
System.IO.FileSystem.Primitives.dll                   System.Xml.Serialization.dll
System.IO.FileSystem.Watcher.dll                      System.Xml.XDocument.dll
System.IO.MemoryMappedFiles.dll                       System.Xml.XmlDocument.dll
System.IO.Packaging.dll                               System.Xml.XmlSerializer.dll
System.IO.Pipes.dll                                   System.Xml.XPath.dll
System.IO.UnmanagedMemoryStream.dll                   System.Xml.XPath.XDocument.dll
System.Linq.dll                                       System.Xml.XPath.XmlDocument.dll
System.Linq.Expressions.dll

インストールされた powershell コマンドを実行し、$PSVersionTable にてバージョン情報を表示する。Linux 版では以下となる。

[root@XXXXXXXXXXXX ~]# powershell
PS /root> $PSVersionTable                                                                                               
Name                           Value                                                                                           
----                           -----                                                                                          
PSVersion                      6.0.0-alpha                                                                                    
PSEdition                      Core                                                                                           
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                        
BuildVersion                   3.0.0.0                                                                                        
GitCommitId                    v6.0.0-alpha.14                                                                                
CLRVersion                                                                                                                    
WSManStackVersion              3.0                                                                                            
PSRemotingProtocolVersion      2.3                                                                                            
SerializationVersion           1.1.0.1                  

Windows 版は以下となるが PSEdition が Windows では Desktop となり、CLRVersion が Linux 版では存在しないことが分かる。

PS C:\Windows\System32\WindowsPowerShell\v1.0> $PSVersionTable

Name                           Value                                                                                        
----                           -----                                                                                        
PSVersion                      5.1.14986.1000                                                                               
PSEdition                      Desktop                                                                                      
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                      
BuildVersion                   10.0.14986.1000                                                                              
CLRVersion                     4.0.30319.42000                                                                              
WSManStackVersion              3.0                                                                                          
PSRemotingProtocolVersion      2.3                                                                                          
SerializationVersion           1.1.0.1 

次に Azure Resource Manager Cmdlets の環境を構築する。

Azure Resource Manager Cmdlets のインストールとセットアップ

前述の powershell コマンドを実行後、以下の様に Install-Package を利用して Azure Resource Manager Cmdlets をインストールする。さらに PowerShell環境変数を設定した後、Import-Module で AzureRM.NetCore.Preview をインポートする。

PS /root> Install-Package -Name AzureRM.NetCore.Preview -Source https://www.powershellgallery.com/api/v2 -ProviderName NuGet -ExcludeVersion -Destination /opt/microsoft/powershell/6.0.0-alpha.14/Modules              
PS /root> $env:PSModulePath = $env:PSModulePath + ":/opt/microsoft/powershell/6.0.0-alpha.14/Modules"
PS /root> Import-Module AzureRM.NetCore.Preview  

環境変数の設定時にバージョン( 6.0.0-alpha.14 の 14 部分は特に )を打ち間違えるとモジュールのインポートに失敗するので注意が必要だ。以上で Azure Resource Manager Cmdlets のインストールは完了となる。
念のため Get-Module -ListAvailable を実行し、AzureRM.NetCore.Preview, AzureRM.NetCore.Preview, AzureRM.Resources.NetCore.Preview が表示されるかを確認する。

PS /root> Get-Module -ListAvailable                                                                                            
VERBOSE: Loading module from path 
'/opt/microsoft/powershell/6.0.0-alpha.14/Modules/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psm1'.
VERBOSE: Loading module from path 
'/opt/microsoft/powershell/6.0.0-alpha.14/Modules/Microsoft.PowerShell.Archive/Microsoft.PowerShell.Archive.psm1'.
VERBOSE: Loading module from path 
'/opt/microsoft/powershell/6.0.0-alpha.14/Modules/PSDesiredStateConfiguration/PSDesiredStateConfiguration.psm1'.
VERBOSE: Loading module from path '/opt/microsoft/powershell/6.0.0-alpha.14/Modules/PSReadLine/PSReadLine.psm1'.
VERBOSE: Populating RepositorySourceLocation property for module PackageManagement.
VERBOSE: Loading module from path 
'/opt/microsoft/powershell/6.0.0-alpha.14/Modules/PackageManagement/1.1.2.0/PackageManagement.psm1'.
VERBOSE: Loading module from path '/opt/microsoft/powershell/6.0.0-alpha.14/Modules/Pester/Pester.psm1'.
VERBOSE: Populating RepositorySourceLocation property for module PowerShellGet.
VERBOSE: Loading module from path '/opt/microsoft/powershell/6.0.0-alpha.14/Modules/PowerShellGet/1.1.2.0/PSModule.psm1'.
VERBOSE: Loading module from path 
'/opt/microsoft/powershell/6.0.0-alpha.14/Modules/AzureRM.Profile.NetCore.Preview/Microsoft.Azure.Commands.Profile.dll'.
VERBOSE: Loading module from path 
'/opt/microsoft/powershell/6.0.0-alpha.14/Modules/AzureRM.Resources.NetCore.Preview/Microsoft.Azure.Commands.Resources.dll'.
VERBOSE: Loading module from path 
'/opt/microsoft/powershell/6.0.0-alpha.14/Modules/AzureRM.Resources.NetCore.Preview/Microsoft.Azure.Commands.Resources.Cmdlets
.dll'.


    Directory: /opt/microsoft/powershell/6.0.0-alpha.14/Modules


ModuleType Version    Name                                ExportedCommands                                                    
---------- -------    ----                                ----------------                                                    
Manifest   0.3.4      AzureRM.NetCore.Preview                                                                                 
Manifest   0.3.4      AzureRM.Profile.NetCore.Preview     {Login-AzureRmAccount, Select-AzureRmSubscription}                  
Manifest   0.3.4      AzureRM.Resources.NetCore.Preview                                                                       
Manifest   1.0.1.0    Microsoft.PowerShell.Archive        {Compress-Archive, Expand-Archive}                                  
Manifest   3.0.0.0    Microsoft.PowerShell.Host           {Start-Transcript, Stop-Transcript}                                 
Manifest   3.1.0.0    Microsoft.PowerShell.Management     {Add-Content, Clear-Content, Clear-ItemProperty, Join-Path...}      
Manifest   3.0.0.0    Microsoft.PowerShell.Security       {Get-Credential, Get-ExecutionPolicy, Set-ExecutionPolicy, Conver...
Manifest   3.1.0.0    Microsoft.PowerShell.Utility        {Format-List, Format-Custom, Format-Table, Format-Wide...}          
Script     1.1.2.0    PackageManagement                   {Find-Package, Get-Package, Get-PackageProvider, Get-PackageSourc...
Script     3.3.9      Pester                              {Describe, Context, It, Should...}                                  
Script     1.1.2.0    PowerShellGet                       {Install-Module, Find-Module, Save-Module, Update-Module...}        
Script     0.0        PSDesiredStateConfiguration         {StrongConnect, IsHiddenResource, Write-MetaConfigFile, Get-Inner...
Script     1.2        PSReadLine                          {Get-PSReadlineKeyHandler, Set-PSReadlineKeyHandler, Remove-PSRea...

上記の様に表示されれば Azure Resource Manager Cmdlets のコマンドが利用可能になる。

ARM テンプレートのデプロイ

最後に Linux から ARM テンプレートのデプロイを実施する。まずは以下の様に Login-AzureRmAccount コマンドでログイン処理を実施する。

PS /root> Login-AzureRmAccount                                                                     
VERBOSE: To sign in, use a web browser to open the page https://aka.ms/devicelogin and enter the code XXXXXXXXXXX to 
authenticate.


Environment           : AzureCloud
Account               : XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
TenantId              : XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
SubscriptionId        : XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
SubscriptionName      : XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
CurrentStorageAccount : 

試しにリソースグループの一覧を表示してみる。以下の様に自身のリソースグループ一覧が表示されればログインは成功だ。

PS /root> Get-AzureRmResourceGroup | Select-Object -Property ResourceGroupName                                                 

ResourceGroupName                 
-----------------                 
adds-joined-rg                    
WGVMRG                            
WGVNetRG                 

最後に ARM テンプレートのデプロイを行う。azure-quickstart-templates と呼ばれるテンプレート一覧の 100-blank-template のデプロイを実施する。以下のコマンドを参考にすればデプロイが実施可能だ。

PS /root> New-AzureRmResourceGroup -Name "hello-linuxpowershell-rg" -Location "japanwest"                                      
WARNING: The usability of Tag parameter in this cmdlet will be modified in a future release. This will impact creating, 
updating and appending tags for Azure resources. For more details about the change, please visit 
https://github.com/Azure/azure-powershell/issues/726#issuecomment-213545494
VERBOSE: 4:30:28 PM - Created resource group 'hello-linuxpowershell-rg' in location 'japanwest'


ResourceGroupName : hello-linuxpowershell-rg
Location          : japanwest
ProvisioningState : Succeeded
Tags              : 
ResourceId        : /subscriptions/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/resourceGroups/hello-linuxpowershell-rg


PS /root> New-AzureRmResourceGroupDeployment -ResourceGroupName "hello-linuxpowershell-rg" -TemplateUri https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/100-blank-template/azuredeploy.json -TemplateParameterUri https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/100-blank-template/azuredeploy.parameters.json                         
VERBOSE: 4:33:32 PM - Template is valid.
VERBOSE: 4:33:33 PM - Create template deployment 'azuredeploy'
VERBOSE: 4:33:33 PM - Checking deployment status in 5 seconds


DeploymentName          : azuredeploy
ResourceGroupName       : hello-linuxpowershell-rg
ProvisioningState       : Succeeded
Timestamp               : 12/23/16 4:33:33 PM
Mode                    : Incremental
TemplateLink            : 
                          Uri            : https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/100-blank
                          -template/azuredeploy.json
                          ContentVersion : 1.0.0.0
                          
Parameters              : 
Outputs                 : 
DeploymentDebugLogLevel :