[Ansible] Ansible YAML에서 Multiline 처리 방법

Multiline 스타일 종류 #

YAML에서 멀티라인을 표현하는 가장 대표적인 두 가지 스타일은 다음과 같습니다.

  • Literal style (|)
  • Folded style (>)

Literal style (|) #

Literal 스타일은 줄바꿈을 그대로 보존합니다.
즉 입력한 그대로 여러 줄이 결과에 포함됩니다.

예:

1my_pattern: |
2  This is line one
3  This is line two
4  Line three

위와 같이 작성하면 출력 결과는 정확히 입력한 줄바꿈까지 그대로 반영됩니다.

이는 아래와 같은 상황에서 유용합니다.

  • Shell script 삽입
  • 여러 줄 리터럴 텍스트
  • 정형화된 로그 메시지

Folded style (>) #

Folded 스타일은 줄바꿈을 접어서 하나의 공간(space)로 변환합니다. 다만 연속된 줄바꿈이나 빈 줄은 중복 줄바꿈으로 유지됩니다.

예:

1my_pattern: >
2  This is line one
3  This is line two
4  Line three

위 문법은 실제 값으로는 아래처럼 해석됩니다.

This is line one This is line two Line three

즉 줄바꿈이 기본적으로 공백으로 대체됩니다.


Block Chomping #

YAML은 Multiline에서 줄바꿈 처리 방식을 제어하기 위해 Chomping indicator를 제공합니다.

Indicator설명
`-`마지막 줄바꿈 제거
`+`모든 줄바꿈 보존
>공백으로 줄바꿈 접기

예를 들어 |+를 사용하면 입력한 줄바꿈이 모두 유지됩니다.

1my_pattern: |+
2  First line
3  Second line
4  Third line

이 경우 출력은 많은 줄바꿈까지 유지된 형태입니다.


실무 활용 예 #

Ansible에서 multiline을 활용하는 대표적인 예는 다음과 같습니다.

Shell 스크립트 삽입 #

1- name: Run custom script
2  shell: |
3    echo "Start process"
4    mkdir -p /etc/example
5    cp files/app.conf /etc/example/

이처럼 스크립트를 그대로 Playbook에 포함할 수 있습니다.

큰 텍스트 변수 #

1vars:
2  banner: >
3    Welcome to the server!
4    Please follow the instructions below.

문자열 안에서 줄바꿈을 텍스트 흐름으로 처리하고 싶을 때 > 스타일이 적합합니다.


정리 #

Ansible Playbook에서 YAML Multiline은 다음과 같이 이해하면 됩니다.

  • |: 입력 그대로 줄바꿈 유지 (Literal)
  • >: 줄바꿈을 공백으로 합침 (Folded)
  • 추가적인 +, -를 통해 줄바꿈 유지/삭제 옵션 조정 가능

YAML 멀티라인을 잘 활용하면 Playbook을 더 읽기 좋고 유지보수하기 쉬운 코드로 작성할 수 있습니다.


Advertisement