กําหนดพารามิเตอร์สคริปต์การปรับใช้

เสร็จสมบูรณ์เมื่อ

ในหน่วยก่อนหน้า คุณได้เพิ่มลักษณะการทํางานแบบกําหนดเองบางอย่างไปยังเทมเพลต Azure Resource Manager (ARM) เพื่อเริ่มเนื้อหาในบัญชีเก็บข้อมูลสําหรับสภาพแวดล้อมของแอปพลิเคชันใหม่ วิธีการแก้ปัญหาเฉพาะสําหรับทีมแอปพลิเคชันหนึ่ง

วิธีหนึ่งในการทําให้สคริปต์การปรับใช้สามารถปรับตัวได้มากขึ้นคือการให้ข้อมูลไปยังสคริปต์ คุณมีสองตัวเลือก อาร์กิวเมนต์บรรทัดคําสั่ง และตัวแปรสภาพแวดล้อม

โน้ต

คําสั่งในหน่วยนี้จะแสดงเพื่อแสดงตัวอย่างแนวคิด อย่าเพิ่งเรียกใช้คําสั่ง คุณจะได้ฝึกฝนในสิ่งที่คุณได้เรียนรู้ที่นี่ในเร็ว ๆ นี้

การใช้อาร์กิวเมนต์บรรทัดคําสั่ง

ตัวเลือกแรกสําหรับการส่งผ่านข้อมูลลงในแหล่งข้อมูล deploymentScripts คือการปรับแต่งคุณสมบัติ arguments คุณสมบัติ arguments ใช้สตริงของอาร์กิวเมนต์เช่นเดียวกับที่คุณใส่ที่บรรทัดคําสั่ง อาร์กิวเมนต์เหล่านี้มีให้กับ command คุณสมบัติของอินสแตนซ์คอนเทนเนอร์ Azure ที่เรียกใช้สคริปต์

โน้ต

การแยกวิเคราะห์บางอย่างเกิดขึ้น ดังนั้นให้ทดสอบการเปลี่ยนแปลงบางอย่างของคุณสมบัติ arguments ของคุณ ค่าคุณสมบัติจะแบ่งเป็นอาร์เรย์ของสตริงแบบเดียวกับที่เชลล์ Windows แยกวิเคราะห์บรรทัดคําสั่ง

"properties": {
   "arguments": "-Name Learner",
   "azPowerShellVersion": "3.0",
   "scriptContent": "
       param ([string]$Name)
       $output = \"Hello $Name!\"
       Write-Output $output
       $DeploymentScriptOutputs = @{}
       $DeploymentScriptOutputs['text'] = $output
   ",
   "retentionInterval":"P1D"
}
properties: {
  arguments: '-Name Learner'
  azPowerShellVersion: '3.0'
  scriptContent: '''
    param ([string]$Name)
    $output = "Hello $Name!"
    Write-Output $output
    $DeploymentScriptOutputs = @{}
    $DeploymentScriptOutputs['text'] = $output
  '''
  retentionInterval: 'P1D'
}

การใช้ตัวแปรสภาพแวดล้อม

ตัวเลือกที่สองของคุณคือการสร้างตัวแปรสภาพแวดล้อมที่สคริปต์ของคุณสามารถเข้าถึงได้

"properties": {
   "arguments": "-Name Learner",
   "environmentVariables:": [
       {
         "name": "Subject",
         "value": "Deployment Scripts"
       }
   ],
   "azPowerShellVersion": "3.0",
   "scriptContent": "
       param ([string]$Name)
       $output = \"Hello $Name!\"
       $output += \"Learning about $env:Subject can be very helpful in your deployments.\"
       Write-Output $output
       $DeploymentScriptOutputs = @{}
       $DeploymentScriptOutputs['text'] = $output
   ",
   "retentionInterval":"P1D"
}
properties: {
  arguments: '-Name Learner'
  environmentVariables: [
    {
      name: 'Subject'
      value: 'Deployment Scripts'
    }
  ]
  azPowerShellVersion: '3.0'
  scriptContent: '''
    param ([string]$Name)
    $output = "Hello $Name!"
    $output += "Learning about $env:Subject can be very helpful in your deployments."
    Write-Output $output
    $DeploymentScriptOutputs = @{}
    $DeploymentScriptOutputs['text'] = $output
  '''
  retentionInterval: 'P1D'
}

ประโยชน์หนึ่งของการใช้ตัวแปรสภาพแวดล้อมคือ คุณสามารถใช้ ตัวเลือก secureValue สําหรับข้อมูลลับที่อาจจําเป็นต้องส่งผ่านไปยังสคริปต์การปรับใช้

"properties": {
   "arguments": "-Name Learner",
   "environmentVariables:": [
       {
         "name": "Subject",
         "value": "Deployment Scripts"
       },
       {
         "name": "MySecretValue",
         "secureValue": "PleaseDoNotPrintMeToTheConsole!"
       }
   ],
   "azPowerShellVersion": "3.0",
   "scriptContent": "
       param ([string]$Name)
       $output = \"Hello $Name!\"
       $output += \"Learning about $env:Subject can be very helpful in your deployments.\"
       $output += \"Secure environment variables (like $env:MySecretValue) are only secure if you keep them that way.\"
       Write-Output $output
       $DeploymentScriptOutputs = @{}
       $DeploymentScriptOutputs['text'] = $output
   ",
   "retentionInterval":"P1D"
}
properties: {
  arguments: '-Name Learner'
  environmentVariables: [
    {
      name: 'Subject'
      value: 'Deployment Scripts'
    }
    {
      name: 'MySecretValue'
      secureValue: 'PleaseDoNotPrintMeToTheConsole!'
    }
  ]
  azPowerShellVersion: '3.0'
  scriptContent: '''
    param ([string]$Name)
    $output = "Hello $Name!"
    $output += "Learning about $env:Subject can be very helpful in your deployments."
    $output += "Secure environment variables (like $env:MySecretValue) are only secure if you keep them that way."
    Write-Output $output
    $DeploymentScriptOutputs = @{}
    $DeploymentScriptOutputs['text'] = $output
  '''
  retentionInterval: 'P1D'
}

การส่งผ่านพารามิเตอร์

ตามที่คุณได้เรียนรู้ คุณสามารถตั้งค่าพารามิเตอร์ได้โดยตรงในคุณสมบัติของสคริปต์การปรับใช้ มีตัวเลือกอื่นๆ อีกมากมายสําหรับค่าที่สามารถส่งผ่านได้ คุณสามารถใช้ค่าไดนามิกที่มาจากทรัพยากรที่สร้างขึ้นก่อนหน้านี้ ตัวแปรที่ประกาศในเทมเพลต หรือพารามิเตอร์ที่ส่งผ่านโดยตรงไปยังเทมเพลตในเวลาการปรับใช้

สถานการณ์เหล่านี้จะพร้อมใช้งานผ่านฟังก์ชันเทมเพลตในคุณสมบัติ arguments หรือ environmentVariables คุณสามารถใช้ฟังก์ชันเทมเพลต ARM เพื่อเข้าถึงค่าต่าง ๆ และส่งผ่านไปยังเทมเพลตได้ ฟังก์ชันเหล่านี้รวมถึง reference, parameters, หรือ variables

สถานการณ์เหล่านี้จะพร้อมใช้งานผ่านฟังก์ชันเทมเพลตในคุณสมบัติ arguments หรือ environmentVariables คุณสามารถใช้คุณลักษณะ Bicep ใด ๆ เพื่อเข้าถึงค่าและส่งผ่านไปยังเทมเพลตได้ ตัวอย่างเช่น คุณสามารถอ้างอิงถึงคุณสมบัติจากแหล่งข้อมูลอื่นโดยใช้ชื่อสัญลักษณ์ และอ้างอิงถึงพารามิเตอร์และตัวแปร