AWS Access Key ID [None]:
AWS Secret Access Key [None]:
Default region name [None]: ap-southeast-1
Default output format [None]:
0.2 jmespath 설치
JSON parsing에 사용할 라이브러리를 설치합니다.
sudo python3 -m pip install jmespath
1. EBS 추가
# 코드 업데이트
사용자가 보기 위한 정보는 shell 모듈을 활용하여 결과를 가져옵니다.
실제 작업은 filesystem, file, mount 모듈을 활용하여 업데이트 전보다 OS에 더 유연합니다.
---
- name: Attatch a volume to new instance
hosts: test
vars:
device_name: nvme1n1
directory_name: /mydir
become: yes
tasks:
- name: Find a device which will be attatched.
shell: lsblk
register: lsblk
- debug: var=lsblk.stdout_lines
- name: Create a file system on the volume.
community.general.filesystem:
fstype: xfs
dev: /dev/{{ device_name }}
state: present
- name: Create a directory for the volume.
ansible.builtin.file:
path: "{{ directory_name }}"
state: directory
- name: Mount a volume to the above directory
ansible.posix.mount:
path: "{{ directory_name }}"
src: /dev/{{ device_name }}
opts: defaults,nofail
fstype: xfs
state: mounted
- name: Get information about filesystem which is mounted on a directory
shell: df -h
register: df
- debug: var=df.stdout_lines
- name: Get information from fstab
shell: cat /etc/fstab
register: fstab
- debug: var=fstab.stdout_lines
# 코드 업데이트 전
AWS Console에서 EBS 추가를 먼저 한 다음 진행해주세요.
EC2 인스턴스에 파일 시스템을 생성하고, 볼륨을 Mount합니다.
작업 할 장치 이름(device_name)과 폴더 이름(directory_name)을 변경하여 상황에 맞게 적용할 수 있습니다.
---
- name: Attatch a volume to new instance
hosts: test
vars:
device_name: nvme1n1
directory_name: mydir
become: yes
tasks:
- name: Find a device which will be attatched.
shell: lsblk
register: lsblk
- debug: var=lsblk.stdout_lines
- name: Determine whether there is a file system on the volume.
shell: file -s /dev/{{ device_name }}
register: filesystem
- debug: var=filesystem.stdout_lines
- name: Create a file system on the volume.
shell: mkfs -t xfs /dev/{{ device_name }}
register: mkfs
- debug: var=mkfs.stdout_lines
- name: Create a directory for the volume.
file:
path: /{{ directory_name }}
state: directory
- name: Mount a volume to the above directory
shell: mount /dev/{{ device_name }} /{{ directory_name }}
- name: Get information about filesystem which is mounted on a directory
shell: df -h
register: df
- debug: var=df.stdout_lines
- name: Get information about UUID of the attatched device
shell: blkid | grep {{ device_name }} | grep -oE "[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}"
register: uuid
- debug: var=uuid.stdout_lines
- name: Attatch the volume automatically
shell: echo UUID={{ uuid.stdout_lines[0] }} /{{ directory_name }} xfs defaults,nofail 0 2 >> /etc/fstab
1.1 설정
Play의 이름과 대상 서버, 사용할 변수를 지정합니다.
Root 권한을 필요로 할 때는 become 키워드를 yes로 설정합니다.
- name: Attatch a volume to new instance
hosts: test
vars:
device_name: nvme1n1
directory_name: mydir
become: yes
1.2 작업
작업은 한 개 이상으로 구성할 수 으며 구분은 Dash( – )로 합니다.
첫번째 작업을 살펴보면, 작업의 이름, 사용하는 모듈, 변수 등록으로 구성하였습니다. 모듈은 대상 서버에서 Shell Command를 실행할 수 있는 빌트인 모듈 Shell을 사용하였습니다. Register 키워드는 작업의 결과를 변수로 등록할 수 있습니다. Shell command의 입력으로 나온 결과를 lsblk 변수로 등록하였습니다.
두번째 작업은 첫번째 작업과 비슷하나, 1.1 설정에서 미리 설정해둔 변수를 사용하였습니다. 변수를 사용할 때에는 중괄호 두개로 변수를 감싸 사용합니다.
tasks:
- name: Find a device which will be attatched.
shell: lsblk
register: lsblk
- debug: var=lsblk.stdout_lines
- name: Determine whether there is a file system on the volume.
shell: file -s /dev/{{ device_name }}
register: filesystem
- debug: var=filesystem.stdout_lines
2. EBS 확장
# 코드 업데이트 후
3. Password에서 단일 암호화 변수를 지정하여, root 패스워드를 관리합니다.
볼륨 확장시 filesystem 모듈을 활용합니다.
---
- name: Describe the EBS volume state after expanding a volume size.
hosts: localhost
vars_files:
- "../VARS/vars.yaml"
vars:
volume_id: vol-036ce259af02fff40
region: ap-southeast-1
ansible_become_password: "{{ localhostPassword }}"
become: yes
tasks:
-
- name: Describe the EBS volume state.
shell: aws ec2 describe-volumes-modifications --volume-ids {{ volume_id }} --region {{ region }}
register: volume_state
environment:
PATH: "{{ lookup('env', 'PATH') }}:/usr/local/bin/aws"
until: volume_state.stdout | from_json | json_query('VolumesModifications[0].ModificationState') == 'completed'
retries: 30
delay: 30
- name: Expand a volume
hosts: test
vars:
device_name: nvme1n1
directory_name: mydir
become: yes
gather_facts: no
tasks:
- name: Expand a volume on the selected directory.
community.general.filesystem:
dev: /dev/{{ device_name }}
fstype: xfs
resizefs: yes
state: present
- name: Get information about filesystem which is mounted on a directory
shell: df -h
register: df
- debug: var=df.stdout_lines
# 코드 업데이트 전
볼륨 확장은 크게 2개의 Play로 구성하였습니다. Play는 순차적으로 진행합니다.
첫번째 Play는 AWS Cli로 API를 호출하여 EBS 변경 상태가 완료되었는지 확인합니다. AWS Cli는 환경에 영향을 받지 않아 localhost에서 진행하였습니다.
두번째 Play는 해당 디렉토리의 볼륨을 확장하고, 확인하는 과정입니다.
---
- name: Describe the EBS volume state after expanding a volume size.
hosts: localhost
vars:
volume_id: vol-0923cd24
become: yes
gather_facts: no
tasks:
- name: Describe the EBS volume state.
shell: aws ec2 describe-volumes-modifications --volume-ids {{ volume_id }} --region ap-southeast-1
register: volume_state
environment:
PATH: "{{ lookup('env', 'PATH') }}:/usr/local/bin/aws"
until: volume_state.stdout | from_json | json_query('VolumesModifications[0].ModificationState') == 'completed'
retries: 30
delay: 30
- name: Expand a volume
hosts: test
vars:
directory_name: mydir
become: yes
tasks:
- name: Expand a volume on the selected directory.
shell: xfs_growfs -d /{{ directory_name }}
register: xfs_growfs
- debug: var=xfs_growfs.stdout_lines
- name: Get information about filesystem which is mounted on a directory
shell: df -h
register: df
- debug: var=df.stdout_lines
until과 retries, delay 키워드로 상태가 완료될 때까지 확인합니다. 지금 구성 정보는 완료 상태가 아니라면, 30초 후에 재시도, 30번 시도합니다.