소개
이 랩에서는 원격 호스트의 파일 및 디렉토리에 대한 정보를 수집할 수 있는 Ansible Stat 모듈을 살펴봅니다. Stat 모듈은 파일 크기, 소유권, 권한, 수정 타임스탬프 등 다양한 속성 및 정보를 제공합니다.
이 랩에서는 원격 호스트의 파일 및 디렉토리에 대한 정보를 수집할 수 있는 Ansible Stat 모듈을 살펴봅니다. Stat 모듈은 파일 크기, 소유권, 권한, 수정 타임스탬프 등 다양한 속성 및 정보를 제공합니다.
이 단계에서는 Ansible Stat 모듈을 사용하여 원격 호스트의 파일에 대한 정보를 수집합니다.
먼저, /home/labex/project/stat-module-playbook.yaml이라는 새 Ansible 플레이북 파일을 생성하고 텍스트 편집기에서 엽니다.
다음 내용을 플레이북 파일에 추가합니다.
- hosts: localhost
tasks:
- name: Get file information
stat:
path: /home/labex/project/file.txt
register: file_info
- name: Print file information
debug:
msg: |
File size: {{ file_info.stat.size }}
Ownership: {{ file_info.stat.uid }}:{{ file_info.stat.gid }}
Permissions: {{ file_info.stat.mode }}
hosts: 플레이북을 실행할 대상 호스트를 지정합니다. 이 경우 대상 호스트가 localhost이므로 플레이북은 로컬 호스트에서 실행됩니다.tasks: 실행할 작업 목록입니다.name: 작업의 목적을 식별하는 작업에 대한 설명적인 이름입니다.stat: 이 Ansible 모듈은 path 매개변수로 지정된 원격 호스트의 파일에 대한 정보를 수집합니다.register: stat 모듈의 출력을 나중에 사용할 file_info라는 변수에 저장합니다.debug: 플레이북 실행 중에 디버깅 정보를 출력합니다.msg: 파일 크기, 소유권 (UID 및 GID), 권한을 포함하여 stat 모듈을 사용하여 검색된 파일 정보가 있는 메시지를 출력합니다.요약하면, 이 플레이북은 로컬 호스트의 /home/labex/project/file.txt에 있는 특정 파일에 대한 정보를 검색하고 크기, 소유권 및 권한과 같은 다양한 세부 정보를 출력하도록 설계되었습니다.
그런 다음 /home/labex/project 디렉토리에 file.txt라는 파일을 생성합니다.
echo "This is the content of the file." > /home/labex/project/file.txt
마지막으로, 다음 명령으로 플레이북을 실행합니다.
ansible-playbook stat-module-playbook.yaml
출력을 관찰하여 지정된 파일의 파일 크기, 소유권 및 권한을 확인합니다.
예시 출력:
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'
PLAY [localhost] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [localhost]
TASK [Get file information] ****************************************************
ok: [localhost]
TASK [Print file information] **************************************************
ok: [localhost] => {
"msg": "File size: 33\nOwnership: 5000:5000\nPermissions: 0664\n"
}
PLAY RECAP *********************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
이 단계에서는 Ansible Stat 모듈을 사용하여 원격 호스트에서 파일의 존재 여부를 확인합니다.
먼저, 기존 플레이북 파일의 모든 내용을 제거하고 다음 내용을 플레이북 파일에 추가하여 수정합니다.
- hosts: localhost
tasks:
- name: Check file existence on remote host
stat:
path: /home/labex/project/file.txt
register: file_info
- name: Print file existence
debug:
msg: "File exists: {{ file_info.stat.exists }}"
hosts: 플레이북을 실행할 대상 호스트를 지정합니다. 이 경우 대상 호스트가 localhost이므로 플레이북은 로컬 호스트에서 실행됩니다.tasks: 실행할 작업 목록입니다.name: 작업의 목적을 식별하는 작업에 대한 설명적인 이름입니다.stat: 이 Ansible 모듈은 path 매개변수로 지정된 원격 호스트의 파일에 대한 정보를 수집합니다.register: stat 모듈의 출력을 나중에 사용할 file_info라는 변수에 저장합니다.debug: 플레이북 실행 중에 디버깅 정보를 출력합니다.msg: stat 모듈을 사용하여 검색된 정보를 기반으로 파일이 존재하는지 여부를 나타내는 메시지를 출력합니다.요약하면, 이 플레이북은 로컬 호스트의 /path/on/remote/host/file.txt에 있는 특정 파일의 존재 여부를 확인하고 파일이 존재하는지 여부를 나타내는 메시지를 출력하도록 설계되었습니다.
그런 다음, 다음 명령으로 플레이북을 실행합니다.
ansible-playbook stat-module-playbook.yaml
출력을 관찰하여 file.txt 파일이 원격 호스트에 존재하는지 확인합니다.
예시 출력:
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'
PLAY [localhost] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [localhost]
TASK [Check file existence on remote host] *************************************
ok: [localhost]
TASK [Print file existence] ****************************************************
ok: [localhost] => {
"msg": "File exists: True"
}
PLAY RECAP *********************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
이 단계에서는 Ansible Stat 모듈을 사용하여 원격 호스트의 파일 수정 타임스탬프를 검색합니다.
먼저, 기존 플레이북 파일의 모든 내용을 제거하고 다음 내용을 플레이북 파일에 추가하여 수정합니다.
- hosts: localhost
tasks:
- name: Get file modification timestamp
stat:
path: /home/labex/project/file.txt
register: file_info
- name: Print file modification timestamp
debug:
msg: "File modification timestamp: {{ file_info.stat.mtime }}"
hosts: 플레이북을 실행할 대상 호스트를 지정합니다. 이 경우 대상 호스트가 localhost이므로 플레이북은 로컬 호스트에서 실행됩니다.tasks: 실행할 작업 목록입니다.name: 작업의 목적을 식별하는 작업에 대한 설명적인 이름입니다.stat: 이 Ansible 모듈은 path 매개변수로 지정된 원격 호스트의 파일에 대한 정보를 수집합니다.register: stat 모듈의 출력을 나중에 사용할 file_info라는 변수에 저장합니다.debug: 플레이북 실행 중에 디버깅 정보를 출력합니다.msg: stat 모듈을 사용하여 검색된 파일의 수정 타임스탬프를 포함하는 메시지를 출력합니다.요약하면, 이 플레이북은 로컬 호스트의 /home/labex/project/file.txt에 있는 특정 파일의 수정 타임스탬프를 검색하고 타임스탬프를 출력하도록 설계되었습니다.
그런 다음, 다음 명령으로 플레이북을 실행합니다.
ansible-playbook stat-module-playbook.yaml
출력을 관찰하여 file.txt 파일의 수정 타임스탬프를 사람이 읽을 수 있는 형식으로 확인합니다.
예시 출력:
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'
PLAY [localhost] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [localhost]
TASK [Get file modification timestamp] *****************************************
ok: [localhost]
TASK [Print file modification timestamp] ***************************************
ok: [localhost] => {
"msg": "File modification timestamp: 1710555624.2304714"
}
PLAY RECAP *********************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
축하합니다! Ansible Stat 모듈 랩을 성공적으로 완료했습니다. Ansible 에서 Stat 모듈을 사용하여 파일 정보를 수집하고, 파일 존재 여부를 확인하고, 수정 타임스탬프를 검색하는 방법을 배웠습니다.
Stat 모듈은 원격 호스트의 파일 및 디렉토리에 대한 다양한 속성 및 정보를 수집할 수 있는 강력한 도구입니다. 이제 이 모듈을 Ansible 플레이북에서 활용하여 고급 파일 관련 작업을 수행하고 수집된 정보를 기반으로 정보에 입각한 결정을 내릴 수 있습니다.
Ansible 문서를 계속 탐색하고 다양한 모듈을 실험하여 자동화 기술을 향상시키십시오. 즐거운 Ansible-ing!