天天看點

在 Cloud Shell 中使用 Ansible 編排配置阿裡雲資源

簡介

Ansible

是一個開源的用于自動執行資源的配置管理和應用程式部署産品。

雲指令行 Cloud Shell

已經為我們安裝、配置完成 Ansible v2.8.5,我們可以直接使用。同時阿裡雲提供了 Ansible 的

阿裡雲子產品

利用該該子產品我們可以通過 Ansible 非常友善管理我們的阿裡雲資源,同時,我們還可以使用 Ansible 在環境中自動配置資源和部署應用。

本文就介紹了如何使用 Ansible 來管理阿裡雲資源以及部署應用。

你可以在 tutorial-cli-ansible

中檢視本文中對應的 Ansible 配置代碼,并通過 git clone 下載下傳到本地執行。

推薦你直接去往 Cloud Shell 體驗更直覺的互動式教程:

使用 Ansible 管理雲資源 。通過 Cloud Shell 編輯器來檢視代碼,并在 Cloud Shell 直接執行,而不需要繁瑣的本地配置過程。

配置 Ansible

Cloud Shell

供我們免費使用,在其中,我們可以直接使用 Ansible 來管理雲資源。

内置了授權,并且預設配置好了阿裡雲子產品,我們無需進行額外配置。預設 Cloud Shell 會使用臨時 AK 幫我們配置好 AK 資訊,同時預設 region 為

cn-hangzhou

我們也可以如下方式自行配置阿裡雲子產品。

export ALICLOUD_ACCESS_KEY="your_accesskey"

export ALICLOUD_SECRET_KEY="your_accesskey_secret"

export ALICLOUD_REGION="your_region"           

如果需要在

以外的環境配置 Ansible,可以參考

安裝和配置Ansible

建立資源

在本文中,我們會通過 Ansible 編排建立 VPC、VSwitch、安全組和 ECS。其中

create.yml

playbook 聲明了編排配置資訊。

- name: Create Resources
  hosts: localhost
  remote_user: root

  roles:
    - vpc
    - vswitch
    - security_group
    - ecs           

其中,各資源的詳細的配置如下:

  • 變量定義: group_vars/all ,定義了 Ansible playbook 部署資源所需要的變量資訊,包括 VPC 名稱、ECS 鏡像等等。
    # common parameters
    alicloud_region: "cn-hangzhou"
    alicloud_zone: "cn-hangzhou-i"
    
    # create vpc parameters
    vpc_cidr: "172.16.0.0/12"
    vpc_name: "Cloudshell_Tutorial_Cli_Ansible"
    vpc_description: "Create a new VPC resource via Ansible example."
    
    # create vswitch parameters
    vswitch_cidr: "172.16.1.0/24"
    vswitch_name: "Cloudshell_Tutorial_Cli_Ansible"
    vswitch_description: "Create a new VSwitch resource via Ansible example."
    
    # create security parameters
    group_name: "Cloudshell_Tutorial_Cli_Ansible"
    group_description: "Create a new security group resource via Ansible example."
    group_inboundRules:
      - ip_protocol: tcp
        port_range: 22/22
        source_cidr_ip: 0.0.0.0/0
        priority: 1
    
      - ip_protocol: tcp
        port_range: 80/80
        source_cidr_ip: 0.0.0.0/0
        priority: 1
    
    # create ECS instance parameters
    image_id: "centos_7_06_64_20G_alibase_20190711.vhd"
    instance_type: "ecs.t5-lc2m1.nano"
    instance_name: "Cloudshell_Tutorial_Cli_Ansible"
    instance_description: "Create a new ECS instance resource via Ansible example."
    host_name: "my-instance-from-ansible"
    password: "Test12345"
    
    allocate_public_ip: True
    internet_charge_type: "PayByTraffic"
    max_bandwidth_in: 200
    max_bandwidth_out: 50
    instance_tags: {created_from: cloudshell-tutorial-cli-ansible}
    
    system_disk_category: "cloud_ssd"
    system_disk_size: 20
    
    number_of_instances: 2           
  • VPC: roles/vpc/tasks/main.yml ,這裡會先查詢是否已經存在同名的 VPC,如果已經存在,則不會建立。
    - name: Get the existing vpc
      ali_vpc_facts:
        region: '{{alicloud_region}}'
        vpc_name: '{{vpc_name}}'
      register: vpcs
    
    - name: Create a new alicloud VPC resource
      ali_vpc:
        alicloud_region: '{{ alicloud_region }}'
        state: 'present'
        cidr_block: '{{ vpc_cidr }}'
        vpc_name: '{{ vpc_name }}'
        description: '{{ vpc_description }}'
      when: not vpcs.vpcs
      register: vpc           
  • VSwitch: roles/vswitch/tasks/main.yml 。VSwitch 建立依賴 VPC,這裡會先根據定義的

    vpc_name

    擷取對應 VPC。
    - name: Get the existing vpc
      ali_vpc_facts:
        region: '{{alicloud_region}}'
        vpc_name: '{{vpc_name}}'
      register: vpcs
    
    - name: Create a new alicloud VSwitch resource
      ali_vswitch:
        alicloud_region: '{{ alicloud_region }}'
        alicloud_zone: '{{ alicloud_zone }}'
        state: 'present'
        cidr_block: '{{ vswitch_cidr }}'
        vswitch_name: '{{ vswitch_name }}'
        description: '{{ vswitch_description }}'
        vpc_id: '{{vpcs.vpcs.0.id}}'
      register: vswitch           
  • 安全組: roles/security_group/tasks/main.yml
    - name: Get the existing vpc
      ali_vpc_facts:
        region: '{{alicloud_region}}'
        vpc_name: '{{vpc_name}}'
      register: vpcs
    
    - name: Get the existing groups
      ali_security_group_facts:
        region: '{{alicloud_region}}'
        group_name: '{{ group_name }}'
        filters:
          vpc_id: '{{vpcs.vpcs.0.id}}'
      register: sgs
    
    - name: Creating security group
      ali_security_group:
        alicloud_region: '{{ alicloud_region }}'
        state: 'present'
        name: '{{ group_name }}'
        description: '{{ group_description }}'
        vpc_id: '{{vpcs.vpcs.0.id}}'
        rules: '{{ group_inboundRules }}'
      when: not sgs.groups
      register: group           
  • ECS: roles/ecs/tasks/main.yml
    - name: Get the existing groups
      ali_security_group_facts:
        region: '{{alicloud_region}}'
        group_name: '{{ group_name }}'
        filters:
          vpc_id: '{{vpcs.vpcs.0.id}}'
      register: sgs
    
    - name: Creating an ECS instance
      ali_instance:
        alicloud_region: '{{ alicloud_region }}'
        alicloud_zone: '{{ alicloud_zone }}'
        image: '{{ image_id }}'
        type: '{{ instance_type }}'
        instance_name: '{{ instance_name }}'
        description: '{{ instance_description }}'
        host_name: '{{ host_name }}'
        password: '{{ password }}'
    
        allocate_public_ip: '{{ allocate_public_ip }}'
        internet_charge_type: '{{ internet_charge_type }}'
        max_bandwidth_in: '{{ max_bandwidth_in }}'
        max_bandwidth_out: '{{ max_bandwidth_out }}'
        instance_tags: '{{ instance_tags }}'
    
        security_groups: ['{{ sgs.groups.0.id }}']
        vswitch_id: '{{ vswitch.vswitch.id }}'
    
        system_disk_category: '{{ system_disk_category }}'
        system_disk_size: '{{ system_disk_size }}'
    
        state: 'present'
        count: '{{ number_of_instances }}'
      when: sgs.groups
      register: instance           

更多 Ansible Playbook 的配置,可以參考

Ansible 的 Working With Playbooks

然後,我們就可以執行

create.yml

一鍵配置好我們編排的資源。

ansible-playbook create.yml           

執行完後,可以在對應産品的控制台看到編排建立的資源。比如該示例中預設為我們建立了兩台 ECS:

在 Cloud Shell 中使用 Ansible 編排配置阿裡雲資源

如果出現錯誤,請檢查你的所需服務是否開通,以及你的賬戶是否實名認證同時賬戶餘額大于 100 元。

動态 Inventory

當我們需要配置資源、部署應用的時候,就需要配置 Ansible Inventory。Ansible 可同時操作屬于一個組的多台主機。組和主機之間的關系通過 Inventory 檔案配置。Ansible Inventory 分為靜态 Inventory 和動态 Inventory。當被管理主機比較少的情況下,直接在靜态 Inventory 的 host 檔案中管理即可;當主機越來越多,不斷變化時,可以通過動态 Inventory 來管理。

阿裡雲提供了動态 Inventory,我們可以直接使用,通過阿裡雲動态 Inventory 動态擷取指定過濾條件的主機資訊。

環境準備

目前阿裡雲的動态 Inventory 還在被官方內建的過程中,我們需要手動安裝依賴。

pip install ansible_alicloud_module_utils           

下載下傳與驗證

首先我們需要下載下傳阿裡雲動态 Inventory 檔案,并賦予其可執行權限

wget https://raw.githubusercontent.com/alibaba/ansible-provider/master/contrib/inventory/alicloud.py;

chmod +x alicloud.py           

同時,下載下傳配套的配置檔案

wget https://raw.githubusercontent.com/alibaba/ansible-provider/master/contrib/inventory/alicloud.ini           

我們可以執行 Inventory 檔案來驗證配置。

./alicloud.py --list           

執行完後,會傳回所有地域的 Inventory 資訊。其中對應的配置檔案為 alicloud.ini,我們可以編輯 alicloud.ini 檔案擷取指定 Region 的資訊。除此之外,還可通過 alicloud.ini 檔案中的 instance_filters 過濾所有的主機資訊。

需要注意的是,預設的 alicloud.ini 配置中,是使用 ECS 的 instance_id 作為 hostname 的,在 Ansible 使用時,會出現這個 hostname unreachable 的問題,是以我們可以手動将 alicloud.ini 中

hostname_variable = instance_id

這一行注釋掉,注釋掉後,會使用 ECS ip 作為 hostname

我們可以通過 Ansible ping 子產品來驗證和之前建立的兩台 ECS 執行個體的連通性

ansible -i ./alicloud.py tag_created_from_cloudshell_tutorial_cli_ansible -m ping -u root -k           

其中

tag_created_from_cloudshell_tutorial_cli_ansible

表示我們根據

created_from_cloudshell_tutorial_cli_ansible

的 tag 的過濾 ecs。執行指令後提示輸入 SSH 密碼,編排資源時,示例中預設設定的密碼為:

Test12345

。結果如下所示:

在 Cloud Shell 中使用 Ansible 編排配置阿裡雲資源

部署應用

通過 Ansible 我們可以配置資源并部署應用,本文會部署 Nginx 到前文我們建立的 ECS 中。

deploy.yml

playbook 聲明了部署配置資訊。

- name: Deploy
  hosts: tag_created_from_cloudshell_tutorial_cli_ansible
  remote_user: root
  tasks:
      - name: install nginx
        yum: name=nginx state=present
      - name: start nginx
        service: name=nginx state=started           

tasks

中會安裝并啟動 Nginx,同時配置

hosts: tag_created_from_cloudshell_tutorial_cli_ansible

,根據

created_from_cloudshell_tutorial_cli_ansible

的 tag 來過濾 ecs。執行指令後提示輸入 SSH 密碼,編排資源時,示例中預設設定的密碼為:

Test12345

ansible-playbook -i ./alicloud.py deploy.yml -u root -k           

執行結果如下所示:

在 Cloud Shell 中使用 Ansible 編排配置阿裡雲資源

執行完後,Nginx 已經成功在我們的 ECS 執行個體上安裝并啟動,我們可以通路該 ECS 的公網 IP 檢視。

在 Cloud Shell 中使用 Ansible 編排配置阿裡雲資源

銷毀資源

通過 Ansible 也可以銷毀資源。其中

destory.yml

playbook 聲明了銷毀資源的配置資訊。

- name: Destroy Resources
  hosts: localhost
  remote_user: root

  tasks:
    - name: Get the existing instances
      ali_instance_info:
        alicloud_region: '{{ alicloud_region }}'
        alicloud_zone: '{{ alicloud_zone }}'
        instance_names:
          - '{{ instance_name }}'
      register: instances

    - name: Delete the existing instances
      ali_instance:
        alicloud_region: '{{ alicloud_region }}'
        alicloud_zone: '{{ alicloud_zone }}'
        instance_ids: '{{ instances.ids }}'
        force: true
        state: absent
      when: instances.ids

    - name: Get the existing vpc
      ali_vpc_facts:
        region: '{{alicloud_region}}'
        vpc_name: '{{vpc_name}}'
      register: vpcs

    - name: Get the existing groups
      ali_security_group_facts:
        region: '{{alicloud_region}}'
        filters:
          vpc_id: '{{vpcs.vpcs.0.id}}'
      when: vpcs.vpcs
      register: sgs

    - name: Delete the existing security groups
      ali_security_group:
        region: '{{ alicloud_region }}'
        group_name: '{{ group_name }}'
        state: absent
      when: sgs.ids

    - name: Get the existing vswitches
      ali_vswitch_facts:
        region: '{{alicloud_region}}'
        filters:
          vpc_id: '{{vpcs.vpcs.0.id}}'
      when: vpcs.vpcs
      register: vsws

    - name: Delete the existing vswitch
      ali_vswitch:
        alicloud_region: '{{ alicloud_region }}'
        alicloud_zone: '{{ alicloud_zone }}'
        cidr_block: '{{ vswitch_cidr }}'
        vpc_id: '{{vpcs.vpcs.0.id}}'
        state: absent
      when: vsws.vswitches

    - name: Delete the existing vpc
      ali_vpc:
        alicloud_region: '{{ alicloud_region }}'
        vpc_name: '{{ vpc_name }}'
        cidr_block: '{{ vpc_cidr }}'
        state: absent
      when: vpcs.vpcs           

我們可以執行以下指令來銷毀前文建立的資源。

ansible-playbook destory.yml           

總結

利用 Ansible 可以友善的編排和配置我們的阿裡雲資源,更多的互動式教程案例,可以參考

開放平台開發者實驗室

。同時你也可以通路

Cloud Shell 幫助文檔

了解更多 Cloud Shell 的資訊。

繼續閱讀