Oracle Cloud上使用Terraform安装AlmaLinux

如果手动安装的话,那还比较简单,基本上点点就可以了。但是如果使用Terraform自动化安装AlmaLinux,则比想象的要复杂。这个复杂度对于所以云市场(MarketPlace)的资源都有类似的问题:需要先在Terraform脚本中同意相关协议才能够安装,这是复杂度的主要原因。

这里给出示例,供参考。

获取AlmaLinux的NameOCID

OCID是Oracle Cloud上所有云资源的唯一标识符。每一个OS镜像也都有一个OCID。在官网页面上,也只是列出小部分Oracle镜像,以及对应的OCID:参考All Image Families。但是,对于大量来自其云市场(MarketPlace)的镜像,这里是查不到的,即便在云市场能够查到,也并没有给出OCID

使用oci cli获取nameOCID

最简单的运行 oci cli的方法应该是使用Oracle Cloud提供的Cloud Shell。可以在控制台右上角打开,如下图:

oci cli获取镜像列表

命令:oci compute pic listing list
zzx@cloudshell:~ (ap-tokyo-1)$ oci compute pic listing list
{
  "data": [
    {
      "display-name": "A10 vThunder Application Delivery Controller",
      "listing-id": "ocid1.appcataloglisting.oc1..aaaaaaaato6mehhcyn4ezwcll2bbsupuxlfdtrxbd4t3qjq55o7lgl6q56zq",
      "publisher-name": "A10 Networks Inc",
      "summary": "Deploy high-performance, enterprise-class load balancing with all-inclusive licensing"
    },
    {
      "display-name": "A10 vThunder Application Delivery Controller - BYOL",
      "listing-id": "ocid1.appcataloglisting.oc1..aaaaaaaai5oire4pedtj2w4psjedizcog3gbp7o34oh3fihpdh2lsaazg7ra",
      "publisher-name": "A10 Networks Inc.",
      "summary": "Deploy high-performance, enterprise-class load balancing with all-inclusive licensing"
    },
    {
      "display-name": "AI 'all-in-one' Data Science Image Intel/AMD",
      "listing-id": "ocid1.appcataloglisting.oc1..aaaaaaaatnob23p2w67rjlzz3y3i4isriztyupewmfyghzq6hkxcldbk55uq",
      "publisher-name": "Oracle SA Team",
      "summary": "Oracle Linux 8.x Intel/AMD Image with preconfigured AI/ML/DL packages and Oracle AutoML"
    },
...

使用 jq命令处理结果集

oci compute pic listing list返回的是一个以JSON结构化的os镜像列表。可以使用jq命令处理,jq是一个使用C编写的JSON命令行工具,参考:jq@GitHub

获取AlmaLinuxNameOCID

好了,有了这些就可以获取AlmaLinux镜像的NameOCID了:

zzx@cloudshell:~ (ap-tokyo-1)$ oci compute pic listing list --all | jq -r -c '.data[] | select(."display-name" | test ("AlmaLinux"))'
{"display-name":"AlmaLinux OS 8 (AArch64)","listing-id":"ocid1.appcataloglisting.oc1..aaaaaaaaiayknhrp2gsevmoano5i3253iv65q73pc6qzk4s2lrouoruz6zua","publisher-name":"AlmaLinux Foundation","summary":"AlmaLinux OS 8"}
{"display-name":"AlmaLinux OS 8 (x86_64)","listing-id":"ocid1.appcataloglisting.oc1..aaaaaaaarw4g33g4ne3ocyhgs4asgnxnmvooljke6yhr53oy5oqtnrkbj63a","publisher-name":"AlmaLinux Foundation","summary":"AlmaLinux OS 8"}
{"display-name":"AlmaLinux OS 9 (AArch64)","listing-id":"ocid1.appcataloglisting.oc1..aaaaaaaaftyo754yngogacktfvav4itegkcys2ssw7qhtuapucjq3walodbq","publisher-name":"AlmaLinux Foundation","summary":"AlmaLinux OS 9.0"}
{"display-name":"AlmaLinux OS 9 (x86_64)","listing-id":"ocid1.appcataloglisting.oc1..aaaaaaaae5csgo2yfi4f7wohwo3je5ukeianys2gfxuhhjuciuctfdsch2uq","publisher-name":"AlmaLinux Foundation","summary":"AlmaLinux OS 9.0"}
{"display-name":"KUSANAGI 9 for Oracle Cloud (AlmaLinux OS 8)","listing-id":"ocid1.appcataloglisting.oc1..aaaaaaaampxzbyywj4cfo36t5o6gyigxo3v5eiptndhnfvbazb6kpmrvorba","publisher-name":"Prime Strategy Co., Ltd","summary":"Ultra-fast CMS platform \"KUSANAGI\""}

这里选择了Oracle Cloud上提供的AlmaLinux 8.9版本,该版本由AlmaLinux Foundation于2024年11月发布。最终获得其NameOCID为:

AlmaLinux OS 8 (x86_64)
ocid1.appcataloglisting.oc1..aaaaaaaarw4g33g4ne3ocyhgs4asgnxnmvooljke6yhr53oy5oqtnrkbj63a

Terraform中使用云市场资源

要使用云市场资源,首先需要“同意”一系列相关的协议,在Terraform中也需要获取并同意这些协议。这个过程,非常“拧巴”,对于Oracle Cloud上可以参考如下文章:Using Terraform for Marketplace images@Oracle Cloud Infrastructure Blog。文中给出了如何使用镜像“Microsoft SQL 2016 Standard with Windows Server 2016 Standard”,这里稍作修改,使用镜像“AlmaLinux OS 8 (x86_64)”,具体代码如下:

resource "oci_marketplace_accepted_agreement" "test_accepted_agreement" {
  #Required
  agreement_id    = oci_marketplace_listing_package_agreement.test_listing_package_agreement.agreement_id
  compartment_id  = oci_identity_compartment.oic.id
  listing_id      = data.oci_marketplace_listing.test_listing.id
  package_version = data.oci_marketplace_listing.test_listing.default_package_version
  signature       = oci_marketplace_listing_package_agreement.test_listing_package_agreement.signature
}
resource "oci_marketplace_listing_package_agreement" "test_listing_package_agreement" {
  #Required
  agreement_id    = data.oci_marketplace_listing_package_agreements.test_listing_package_agreements.agreements[0].id
  listing_id      = data.oci_marketplace_listing.test_listing.id
  package_version = data.oci_marketplace_listing.test_listing.default_package_version
}
/*
    Data Elements
*/
data "oci_marketplace_listing_package_agreements" "test_listing_package_agreements" {
  #Required
  listing_id      = data.oci_marketplace_listing.test_listing.id
  package_version = data.oci_marketplace_listing.test_listing.default_package_version

  #Optional
  compartment_id = oci_identity_compartment.oic.id
}
data "oci_marketplace_listing_package" "test_listing_package" {
  #Required
  listing_id      = data.oci_marketplace_listing.test_listing.id
  package_version = data.oci_marketplace_listing.test_listing.default_package_version

}
data "oci_marketplace_listing_packages" "test_listing_packages" {
  #Required
  listing_id = data.oci_marketplace_listing.test_listing.id

}
data "oci_marketplace_listing" "test_listing" {
  listing_id     = data.oci_marketplace_listings.test_listings.listings[0].id
  compartment_id = oci_identity_compartment.oic.id
}
data "oci_marketplace_listings" "test_listings" {
  name = ["AlmaLinux OS 8 (x86_64)"]
  compartment_id = oci_identity_compartment.oic.id
}
data "oci_core_app_catalog_listing_resource_version" "test_catalog_listing" {
  listing_id = data.oci_marketplace_listing_package.test_listing_package.app_catalog_listing_id
  resource_version = data.oci_marketplace_listing_package.test_listing_package.app_catalog_listing_resource_version
}

注意上面的代码通常需要适配的地方包括:

  • 所有的:compartment_id 部分
  • "oci_marketplace_listings" "test_listings"中的name变量需要根据你需要使用的image进行修改

参考:

Leave a Reply

Your email address will not be published. Required fields are marked *