天天看點

運維編排場景系列-----自動一鍵連通跳闆機

跳闆機是雲盾提供的一個核心系統運維和安全審計管控平台。通過跳闆機可以操作跳闆機内連通的執行個體,并在跳闆機内對執行個體進行免密連接配接操作。此文章建構的是一個簡易的跳闆機例文。

前提條件

  1. 執行個體狀态為運作中(

    Running

  2. 控制台 檢視準備的秘鑰對或建立秘鑰對
  3. 儲存秘鑰對中的私鑰部分

模版說明:

父模版主要功能為:

  1. 擷取執行個體
  2. 檢查執行個體是否綁定了指定的公共秘鑰否則綁定秘鑰對
  3. 檢查執行個體運作狀态是否符合預期
  4. 檢測選擇執行公網Ip或者選擇執行彈性公網Ip
  5. 将似鑰存放進跳闆機内并測試連接配接
  6. 檢測選擇是否執行測試連接配接彈性公網Ip
  7. 檢測目前任務
FormatVersion: OOS-2019-06-01
Description:
  en: Connect the instance in the jumper instance.
  zh-cn: 連接配接跳闆機内的執行個體。
Parameters:
  KeyPairName:
    Type: String
    Description:
      en: Key pair name.
      zh-cn: 秘鑰對名稱。
  SecretKeys:
    Type: String
    Description:
      en: Path and file name of the private key stored in the springboard.
      zh-cn: 跳闆機記憶體放私鑰的路徑及檔案名稱。
  JumperInstanceId:
    Type: String
    Description:
      en: Jumper instance ID.
      zh-cn: 跳闆機執行個體ID。
  Targets:
    Type: Json
    AssociationProperty: Targets
    AssociationPropertyMetadata:
      ResourceType: 'ALIYUN::ECS::Instance'
  RateControl:
    Description:
      en: Concurrency ratio of task execution.
      zh-cn: 任務執行的并發比率。
    Type: Json
    AssociationProperty: RateControl
    Default:
      Mode: Concurrency
      MaxErrors: 100
      Concurrency: 10
  OOSAssumeRole:
    Description: The RAM role to be assumed by OOS.
    Type: String
    Default: OOSServiceRole
RamRole: '{{ OOSAssumeRole }}'
Tasks:
  - Name: getInstance
    Description:
      en: Views the ECS instances.
      zh-cn: 擷取ECS執行個體。
    Action: 'ACS::SelectTargets'
    Properties:
      ResourceType: 'ALIYUN::ECS::Instance'
      Filters:
        - '{{ Targets }}'
    Outputs:
      instanceIds:
        Type: List
        ValueSelector: 'Instances.Instance[].InstanceId'
  - Name: checkInstanceHasInstalledKeyPair
    Action: 'ACS::Template'
    Description:
      en: 'Check if the instance has the specified public Secret, if not, install it.'
      zh-cn: 檢查執行個體是否安裝了指定的公共秘要,若無則安裝。
    Properties:
      TemplateName: CheckInstanceWetherInstallKeyPair
      Parameters:
        KeyPairName: '{{ KeyPairName }}'
        InstanceId: '{{ ACS::TaskLoopItem }}'
    Loop:
      RateControl: '{{ RateControl }}'
      Items: '{{ getInstance.instanceIds }}'
  - Name: checkInstanceReady
    Action: 'ACS::CheckFor'
    Description:
      en: Check the ECS instance is not Starting status..
      zh-cn: 檢查執行個體的狀态不是Starting。
    Properties:
      Service: ECS
      API: DescribeInstances
      Parameters:
        InstanceIds:
          - '{{ ACS::TaskLoopItem }}'
      NotDesiredValues:
        - Starting
      PropertySelector: 'Instances.Instance[].Status'
    Outputs:
      publicIpAddress:
        Type: String
        ValueSelector: 'Instances.Instance[].PublicIpAddress.IpAddress[]'
      eipAddress:
        Type: String
        ValueSelector: 'Instances.Instance[].EipAddress.IpAddress'
    Loop:
      RateControl: '{{ RateControl }}'
      Items: '{{ getInstance.instanceIds }}'
      Outputs:
        publicIpAddresses:
          AggregateType: 'Fn::ListJoin'
          AggregateField: publicIpAddress
        eipAddresses:
          AggregateType: 'Fn::ListJoin'
          AggregateField: eipAddress
  - Name: chooseEipOrPublicIp
    Action: 'ACS::Choice'
    Properties:
      DefaultTask: runCommandPublicIp
      Choices:
        - When:
            'Fn::Equals':
              - - Null
              - '{{ checkInstanceReady.publicIpAddresses }}'
          NextTask: runCommandEIp
  - Name: runCommandPublicIp
    Action: 'ACS::ECS::RunCommand'
    Description:
      en: 'Try to connect the ECS instance in the jumper instance.'
      zh-cn: 測試連接配接跳闆機内的執行個體。
    Properties:
      commandContent:
        'Fn::Join':
          - |+

          - - 'echo ''{{ SecretKeys }}'' > ~/jumperInstance.pem'
            - chmod 600 ~/jumperInstance.pem
            - 'ssh root@{{ ACS::TaskLoopItem }} -i ~/jumperInstance.pem'
            - ifconfig -a
      instanceId: '{{ JumperInstanceId }}'
      commandType: RunShellScript
    Outputs:
      commandOutput:
        Type: String
        ValueSelector: invocationOutput
    Loop:
      RateControl: '{{ RateControl }}'
      Items: '{{ checkInstanceReady.publicIpAddresses }}'
      Outputs:
        commandOutputs:
          AggregateType: 'Fn::ListJoin'
          AggregateField: commandOutput
  - Name: chooseEipOrEnd
    Action: 'ACS::Choice'
    Properties:
      DefaultTask: runCommandEIp
      Choices:
        - When:
            'Fn::Equals':
              - - Null
              - '{{ checkInstanceReady.eipAddresses }}'
          NextTask: listExecutions
  - Name: runCommandEIp
    Action: 'ACS::ECS::RunCommand'
    Description:
      en: 'Try to connect the ECS instance in the jumper instance.'
      zh-cn: 測試連接配接跳闆機内的執行個體。
    Properties:
      commandContent:
        'Fn::Join':
          - |+

          - - 'echo ''{{ SecretKeys }}'' > ~/jumperInstance.pem'
            - chmod 600 ~/jumperInstance.pem
            - 'ssh root@{{ ACS::TaskLoopItem }} -i ~/jumperInstance.pem'
            - ifconfig -a
      instanceId: '{{ JumperInstanceId }}'
      commandType: RunShellScript
    Outputs:
      commandOutput:
        Type: String
        ValueSelector: invocationOutput
    Loop:
      RateControl: '{{ RateControl }}'
      Items: '{{ checkInstanceReady.eipAddresses }}'
      Outputs:
        commandOutputs:
          AggregateType: 'Fn::ListJoin'
          AggregateField: commandOutput
  - Name: listExecutions
    Action: 'ACS::ExecuteAPI'
    Description: 
      en: 'The execution springboard.'
      zh-cn: 執行跳闆。
        Properties:
      Service: OOS
      API: ListExecutions
      Parameters:
        ExecutionId: '{{ ACS::ExecutionId }}'
           

子模版

需将子模版命名為:CheckInstanceWetherInstallKeyPair

子模版主要功能:

  • 檢測是否綁定公共秘要
  • 若無則将公共秘要綁定到執行個體上
  • 重新開機執行個體
  • 檢測是否綁定秘要
FormatVersion: OOS-2019-06-01
Description:
  en: Check if the instance bind the specified key pair.
  zh-cn: 檢查執行個體是否綁定了指定的秘鑰對。
Parameters:
  InstanceId:
    Type: String
    Description:
      en: The ECS instance ID in the jumper instance need to binding.
      zh-cn: 跳闆機内需要綁定的ECS執行個體ID。
  KeyPairName:
    Type: String
    Description:
      en: The key pair name.
      zh-cn: 秘鑰對的名稱。
  OOSAssumeRole:
    Description: The RAM role to be assumed by OOS.
    Type: String
    Default: OOSServiceRole
RamRole: '{{ OOSAssumeRole }}'
Tasks:
  - Name: checkInstanceHasKeyPair
    Action: 'ACS::CheckFor'
    Description:
      zh-cn: 查詢執行個體是否綁定了給定的秘鑰對。
      en: Check if the instance bind the specified KeyPair.
    OnSuccess: checkInstanceHasKeyPairIsOnlyOne
    OnError: AttachKeyPair
    Properties:
      Service: ECS
      API: DescribeInstances
      Parameters:
        KeyPairName: '{{ KeyPairName }}'
        InstanceIds:
          - '{{ InstanceId }}'
      NotDesiredValues:
        - 0
      PropertySelector: TotalCount
  - Name: AttachKeyPair
    Action: 'ACS::ExecuteAPI'
    Description:
      en: Bind the key pair to the instance.
      zh-cn: 将秘鑰對綁定到執行個體上。
    Properties:
      Service: ECS
      API: AttachKeyPair
      Parameters:
        InstanceIds:
          - '{{ InstanceId }}'
        KeyPairName: '{{ KeyPairName }}'
  - Name: rebootInstance
    Action: 'ACS::ECS::RebootInstance'
    Description:
      en: Restarts the ECS instances.
      zh-cn: 重新開機執行個體。
    Properties:
      instanceId: '{{ InstanceId }}'
  - Name: checkInstanceHasKeyPairIsOnlyOne
    Action: 'ACS::CheckFor'
    Description:
      zh-cn: 查詢執行個體是否綁定了給定的秘鑰對。
      en: Check if the instance bind the specified KeyPair.
    Properties:
      Service: ECS
      API: DescribeInstances
      Parameters:
        KeyPairName: '{{ KeyPairName }}'
        InstanceIds:
          - '{{ InstanceId }}'
      DesiredValues:
        - 1
      PropertySelector: TotalCount
           

模版參數說明:

參數 說明 示例
KeyPairName 建立的秘鑰對名稱 test-oos-jump
SecretKeys 秘鑰對中的私鑰 usr/test.pem
JumperInstanceId 跳闆機執行個體Id
Targets 根據執行個體标簽(tags)或執行個體ID(InstanceId)標明需要的執行個體。 i-11111111111
RateControl

固定參數。

- 速率控制類型:并發控制

- 并發速率:1

- 最大錯誤次數:0

OOSAssumeRole

可選參數。

- (預設設定)目前賬号的已有權限:執行您使用的賬号的權限動作。請確定您擁有建立自定義鏡像涉及的所有ECS API調用權限。

- 指定RAM角色,使用該角色的權限:如果指定了RAM角色名稱,OOS扮演該RAM角色執行運維任務。

目前賬号的已有權限

操作步驟

  1. 請通路 運維編排OOS的控制
    運維編排場景系列-----自動一鍵連通跳闆機
  2. 單擊我的模版,單擊建立模闆按鈕,選取空白模闆。
    運維編排場景系列-----自動一鍵連通跳闆機
  3. 先将上述提供的子模闆粘貼到yaml欄,輸入名稱CheckInstanceWetherInstallKeyPair,點選建立模闆。然後将父模版粘貼到yaml欄,輸入名稱ConnectJumperInstance,點選建立模版。
    運維編排場景系列-----自動一鍵連通跳闆機
  4. 找到建立的父模版,單擊建立執行。
    運維編排場景系列-----自動一鍵連通跳闆機
  5. 保持基本資訊的預設設定,單擊下一步:設定參數。
  6. 完成參數設定填寫,單擊下一步:确認。
    運維編排場景系列-----自動一鍵連通跳闆機
  7. 檢視參數設定是否無誤,确認無誤後單擊建立執行。
    運維編排場景系列-----自動一鍵連通跳闆機
  8. 在執行管理中檢視建立的運維任務。
    運維編排場景系列-----自動一鍵連通跳闆機
  9. 執行結果

    登陸跳闆機,此時在跳闆機内輸入ssh [email protected] -i ~/xxxx.pem就可以實作免密連接配接其下任意執行個體。

    運維編排場景系列-----自動一鍵連通跳闆機

系列文章

主題文章

阿裡雲重磅釋出雲上自動化利器——運維編排OOS

最佳實踐

玩轉運維編排服務的權限:Assume Role+Pass Role 阿裡雲運維編排新功能:一鍵批量克隆ECS 批量在多台ECS内執行指令

場景系列

運維編排場景系列----更新ECS鏡像 運維編排場景系列----給ECS執行個體自動打TAG 運維編排場景系列----從執行個體中拷貝檔案到OSS 運維編排場景系列----給執行個體加到SLS機器組 運維編排場景系列----檢測MFA功能狀态 運維編排場景系列----每日統計多Region執行個體的運作狀态 運維編排場景系列----如何使用jq 運維編排場景系列----分批到機器上運作指令 運維編排場景系列----更新鏡像後自動更新伸縮配置鏡像 運維編排場景系列----向Linux執行個體上傳檔案 運維編排場景系列----運作遠端shell腳本 運維編排場景系列----在ECS執行個體上運作Ansible-playbook 運維編排場景系列----下載下傳JVM堆棧到OSS 運維編排系列場景----将執行個體的固定公網IP轉換為其它新EIP 運維編排場景系列----自動定時更新臨時帶寬 運維編排場景系列----批量更新雲助手用戶端 運維編排系列場景----批量釋放執行個體 運維編排場景系列----ECS執行個體系統快照下載下傳到本地 運維編排場景系列----批量更換系統盤 運維編排場景系列----自動建立多個區域同類型存儲空間 運維編排場景系列---一鍵批量重置執行個體密碼