특정 파일의 마지막 변경 일자를 확인하는 방법을 공유합니다.
ad-hoc
과playbook
각각 방법을 기록하였습니다. 유튜브 바로가기테스트 환경
- Master 1대(Ubuntu 18.04), Child 2대(CentOS7, Debian8)
- Ansible 2.9.2
$ cat /etc/ansible/hosts
[child]
182.252.133.71
182.252.133.72
[child:vars]
ansible_user=root
ansible_password=Password123!
child01, child02 /etc/passwd
파일의 마지막 변경 일자를 출력합니다.
/etc/passwd
은 child01, child02에 모두 존재하는 파일 입니다.
실행명령
$ ansible child -m shell -a 'stat -c "%y" /etc/passwd |cut -f1 -d" "'
결과화면
root@master:/workspace/ansible# ansible child -m shell -a 'stat -c "%y" /etc/passwd |cut -f1 -d" "'
[WARNING]: Platform linux on host 182.252.133.72 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python
interpreter could change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
182.252.133.72 | CHANGED | rc=0 >>
2017-08-04
182.252.133.71 | CHANGED | rc=0 >>
2020-02-03
mtime_file.yml
과 my_filter.py
이 필요합니다.
filter_plugins
디렉토리 하위에 my_filter.py
위치합니다.
├── filter_plugins
│ ├── my_filter.py
│ └── my_filter.pyc
└── mtime_file.yml
$ cat mtime_file.yml
---
- hosts : child
tasks :
- name: passwd 파일 변경일 확인하기
stat: path=/etc/passwd
register : status_file
- debug :
var : status_file.stat.mtime | my_filter
when : status_file.stat.exists
$ cat ./filter_plugins/my_filter.py
import datetime
def my_filter(arg):
value = datetime.datetime.utcfromtimestamp(arg).strftime('%Y-%m-%d')
return value
class FilterModule(object):
def filters(self):
return { 'my_filter': my_filter }
실행명령
$ ansible-playbook ./mtime_file.yml
결과화면
root@master:/workspace/ansible# ansible-playbook ./mtime_file.yml
PLAY [child] *****************************************************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************************
ok: [182.252.133.71]
ok: [182.252.133.72]
TASK [passwd 파일 변경일 확인하기] ****************************************************************************************************************************************
ok: [182.252.133.72]
ok: [182.252.133.71]
TASK [debug] *****************************************************************************************************************************************************
ok: [182.252.133.72] => {
"status_file.stat.mtime | my_filter": "2017-08-04"
}
ok: [182.252.133.71] => {
"status_file.stat.mtime | my_filter": "2020-02-03"
}
PLAY RECAP *******************************************************************************************************************************************************
182.252.133.71 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
182.252.133.72 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
child01, child02 /etc/passwd.bak
파일의 마지막 변경 일자를 출력합니다.
/etc/passwd.bak
는 child01에만 존재하는 파일 입니다.
실행명령
$ ansible child -m shell -a 'stat -c "%y" /etc/passwd.bak |cut -f1 -d" "'
결과화면
root@master:/workspace/ansible# ansible child -m shell -a 'stat -c "%y" /etc/passwd.bak |cut -f1 -d" "'
[WARNING]: Platform linux on host 182.252.133.72 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python
interpreter could change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
182.252.133.72 | CHANGED | rc=0 >>
stat: cannot stat ‘/etc/passwd.bak’: No such file or directory
182.252.133.71 | CHANGED | rc=0 >>
2019-02-12
mtime_file.yml
과 my_filter.py
이 필요합니다.
filter_plugins
디렉토리 하위에 my_filter.py
위치합니다.
├── filter_plugins
│ ├── my_filter.py
│ └── my_filter.pyc
└── mtime_file.yml
$ cat mtime_file.yml
---
- hosts : child
tasks :
- name: passwd.bak 파일 변경일 확인하기
stat: path=/etc/passwd.bak
register : status_file
- debug :
var : status_file.stat.mtime | my_filter
when : status_file.stat.exists
$ cat ./filter_plugins/my_filter.py
import datetime
def my_filter(arg):
value = datetime.datetime.utcfromtimestamp(arg).strftime('%Y-%m-%d')
return value
class FilterModule(object):
def filters(self):
return { 'my_filter': my_filter }
실행명령
$ ansible-playbook ./mtime_file.yml
결과화면
root@master:/workspace/ansible# ansible-playbook ./mtime_file.yml
PLAY [child] *****************************************************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************************
ok: [182.252.133.72]
ok: [182.252.133.71]
TASK [passwd.bak 파일 변경일 확인하기] ****************************************************************************************************************************************
ok: [182.252.133.72]
ok: [182.252.133.71]
TASK [debug] *****************************************************************************************************************************************************
ok: [182.252.133.71] => {
"status_file.stat.mtime | my_filter": "2019-02-12"
}
skipping: [182.252.133.72]
PLAY RECAP *******************************************************************************************************************************************************
182.252.133.71 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
182.252.133.72 : ok=2 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
Ansible을 서버 정보 관련 취합 작업에 대부분 활용하고 있습니다.
ad-hoc
이던 playbook
이던 취합을 위해서 결과 화면의 파싱이 필요했습니다.
ad-hoc
의 경우, 결과 화면에서rc=0
와rc=1
구분자로 정상동작을 체크했습니다.
시나리오2 처럼 결과 화면이rc=0
포함하여도 정상동작이 아닐 수 있음을 확인했습니다.
playbook
의 경우, 결과 화면에서ok:
와skipping:
구분자로 정상동작을 체크할 수 있네요.
앞으로 되도록이면 Ansible 스크립트는 playbook
으로 작성해야 할 것 같습니다.