TheKoguryo's 기술 블로그

Version 2023.04.29

14.2.6 예시, list, count를 이용한 유저 만들기

자원을 만들다 보면 Array를 통해 동일한 자원을 여러개 만드는 경우가 있습니다. 실습환경을 구성하기위해 여러 유저를 만든다는 상황을 고려해 list, count를 통해 여러 유저를 한꺼번에 만드는 방법을 알아 보겠습니다.

User 생성을 위해 필요한 Policy

Allow group <group_name> to manage users in tenancy

User 만들기

OCI 유저는 oci_identity_user 구문을 사용하여 만들 수 있습니다.

  • 예시

    resource "oci_identity_user" "test_user" {
      #Required
      compartment_id = "${var.tenancy_ocid}"
      description    = "${var.user_description}"
      name           = "${var.user_name}"
    
      #Optional
      defined_tags   = {"Operations.CostCenter"= "42"}
      email          = "${var.user_email}"
      freeform_tags  = {"Department"= "Finance"}
    }
    

count를 사용해 여러 유저 만들기

기본적으로 하나의 resource 블럭은 하나의 자원에 해당됩니다. 여기에 count를 추가하게 되면 동일한 유형을 여러개 만들 수 있습니다. 일반적으로 루프 프로그래밍에서 처럼 count로 반복 횟수를 설정하고, count.index로 현재 루프값을 조회하여 사용합니다. 그래서 아래와 같이 count에 만들 자원 수를 지정하고, count.index를 통해 자원이름 등이 충돌하지 않게 사용할 수 있습니다.

  • testuser_01, testuser_02 처럼 원하는 count 수 만큼 유저를 만드는 예제

    resource "oci_identity_user" "test_user" {
      count          = 2  
    
      #Required
      compartment_id = "${var.tenancy_ocid}"
      description    = "testuser_${format("%02d", count.index + 1)}"
      name           = "testuser_${format("%02d", count.index + 1)}"
    }
    
  • users.tf 전체 내용 예시

    • provider.tf, terraform.tfvars는 앞선 실습 파일을 그대로 사용
    ### Create Users
    resource "oci_identity_user" "users_1" {
      count          = 2
    
      compartment_id = "${var.tenancy_ocid}"
      description    = "testuser_${format("%02d", count.index + 1)}"
      name           = "testuser_${format("%02d", count.index + 1)}"
    }
    
    ### Set User Passwords
    ### This is one time password.
    resource "oci_identity_ui_password" "users_1_password" {
      count   = 2
      user_id = "${oci_identity_user.users_1.*.id[count.index]}"
    }
    
    ### Outputs
    output "my_users_1_password" {
      sensitive = false
      value     = "${concat(oci_identity_user.users_1.*.name, oci_identity_ui_password.users_1_password.*.password)}"
    }
    
  • 실행결과 output과 OCI 유저가 지정한 count 갯수 만큼 생성된 것을 볼 수 있습니다.

    [opc@bastion-host example_user_count]$ terraform apply
    
    ...
    
    Apply complete! Resources: 4 added, 0 changed, 0 destroyed.
    
    Outputs:
    
    my_users_1_password = [
      "testuser_01",
      "testuser_02",
      ":<7:qa.Z[XqvKa19uL$u",
      "s.;sl72P_.v]Eu]V{rxu",
    ]
    [opc@bastion-host example_user_count]$
    

    image-20220119143947359

list, count를 사용해 여러 유저 만들기

앞선 예제와 달리 유저의 이름을 각각 전혀 다른 이름으로 만들고자 할때는 list 타입 배열변수와 count를 조합하여 다른 이름의 여러 유저를 만들 수 있습니다.

  • 다른 이름의 여러 유저를 만드는 예제

    list 타입의 변수를 만들고 "${element(LIST, INDEX)}", **"${length(LIST)}"**를 사용하여, 특정 데이터 및 list 길이를 조회할 수 있습니다.

    variable "user_names" {
      type        = "list"
      default     = ["oracle", "neo"]
    }
    
    resource "oci_identity_user" "my_users_2" {
      count          = "${length(var.user_names)}"
    
      compartment_id = "${var.tenancy_ocid}"
      description    = "${element(var.user_names, count.index)}"
      name           = "${element(var.user_names, count.index)}"
    }
    
  • users.tf 전체 내용 예시

    • provider.tf, terraform.tfvars는 앞선 실습 파일을 그대로 사용
    variable "user_names" {
      type        = list
      default     = ["oracle", "neo"]
    }
    
    ### Create Users
    resource "oci_identity_user" "my_users_2" {
      count          = "${length(var.user_names)}"
    
      compartment_id = "${var.tenancy_ocid}"
      description    = "${element(var.user_names, count.index)}"
      name           = "${element(var.user_names, count.index)}"
    }
    
    ### Set User Passwords
    ### This is one time password.
    resource "oci_identity_ui_password" "my_users_2_password" {
      count   = "${length(var.user_names)}"
      user_id = "${oci_identity_user.my_users_2.*.id[count.index]}"
    }
    
    ### Outputs
    output "my_users_2_password" {
      sensitive = false
      value     = "${concat(oci_identity_user.my_users_2.*.name, oci_identity_ui_password.my_users_2_password.*.password)}"
    }
    
  • 실행결과 output과 OCI 유저가 리스트상의 이름대로 생성된 것을 볼 수 있습니다.

    [opc@bastion-host example_user_list]$ terraform apply
    
    Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
      + create
    
    ...
    
    Apply complete! Resources: 4 added, 0 changed, 0 destroyed.
    
    Outputs:
    
    my_users_2_password = [
      "oracle",
      "neo",
      "W$tC!bS6k&h;mH8R#}9u",
      "(I;hZJ-uZq+.LkXGl5#Z",
    ]
    [opc@bastion-host example_user_list]$
    

    image-20220119152115623



이 글은 개인으로서, 개인의 시간을 할애하여 작성된 글입니다. 글의 내용에 오류가 있을 수 있으며, 글 속의 의견은 개인적인 의견입니다.

Last updated on 10 May 2023