Ansible의 Facts를 이용한 OS 시스템의 정보 수집
시스템 전체현황 및 관리를 위해 OS의 정보를 자동으로 과정을 수집한다.
주요 기능
- Ansible Facts 변수를 사용(명령에 따라 결과가 달라지는 것을 최소화)한다.
- 리눅스 명령을 통해 보기좋거나 정렬하기 쉽게 나열한다.
- 수행 후 OS_VERSION_RESULT/YYYY/MM/DD/os_version_result.txt 파일을 남기도록 한다.
Inventory 설정
# cat hosts
[ALL_HOSTS]
172.16.0.100 managed_ip=172.16.0.100 des="2020-12-03"
172.16.0.200 managed_ip=172.16.0.200 des="2020-12-03"
[ALL_HOSTS_OK]
대상 호스트는 172.16.0.100,200이며, 추가는 2020-12-03에 수행한다. 참고적으로 ALL_HOSTS_OK 호스트 그룹은 작업이 완료된 호스트의 history를 위한 그룹이다.
Host Variables 설정
# cat host_vars/172.16.0.100
ansible_ssh_host: 172.16.0.100
ansible_ssh_port: SSH_포트번호
ansible_ssh_user: 사용자 아이디
ansible_ssh_pass: "사용자 패스워드"
ansible_become: yes
ansible_become_method: su
ansible_become_user: root
ansible_become_pass: "!root_패스워드"
ansible_python_interpreter: python2.7
172.16.0.200 host도 위와 형식이 동일하고 정보만 다르다.
Playbook 설정
---
- name: System Distro,Kernel Version Checking
hosts: ALL_HOSTS
vars:
work_year: "{{lookup('pipe','date \"+%Y\"')}}"
work_month: "{{lookup('pipe','date \"+%m\"')}}"
work_day: "{{lookup('pipe','date \"+%d\"')}}"
time: "{{lookup('pipe','date \"+%Y-%m-%d\"')}}"
tasks:
- include_vars: define_vars_system.yml
- debug: var=system_version
- name: OS_VERSION DIR Local Directory Create
local_action: command mkdir -p ./OS_VERSION/{{ work_year }}/{{ work_month }}/{{ work_day }}/{{ managed_ip }}
- name: SYSTEM Version RESULT, Local File Copy
local_action: copy content={{ system_version }} dest=./OS_VERSION/{{ work_year }}/{{ work_month }}/{{ work_day }}/{{ managed_ip }}/os_version.txt
ignore_errors: True
- name: line insert
lineinfile:
path: ./OS_VERSION/{{ work_year }}/{{ work_month }}/{{ work_day }}/{{ managed_ip }}/os_version.txt
line: ''
insertbefore: EOF
delegate_to: localhost
- name: SYSTEM_VERSION_RESULT DIR Local Directory Create
local_action: command mkdir -p ./OS_VERSION_RESULT/{{ work_year }}/{{ work_month }}/{{ work_day }}
- name: txt File Result
shell: find ./OS_VERSION/{{ work_year }}/{{ work_month }}/{{ work_day }} -type f -name '*.txt' -exec cat {} + > ./OS_VERSION_RESULT/{{ work_year }}
}/{{ work_month }}/{{ work_day }}/os_version_result.txt
delegate_to: localhost
- name: Result txt File Copy
shell: cp -rf ./OS_VERSION_RESULT/{{ work_year }}/{{ work_month }}/{{ work_day }}/os_version_result.txt ./OS_VERSION_RESULT/{{ work_year }}/{{ worr
k_month }}/{{ work_day }}/os_version_result.txt_src
delegate_to: localhost
- name: txt File Blank Line Remove
shell: sed '/^$/d' -i ./OS_VERSION_RESULT/{{ work_year }}/{{ work_month }}/{{ work_day }}/os_version_result.txt
delegate_to: localhost
- name: Local Directory All Removed
shell: rm -rf ./OS_VERSION
delegate_to: localhost
추가적으로 다음과 같이 YAML 형식의 변수 파일을 별도로 생성한다.
# cat define_vars_system.yml
---
system_version: "{{ managed_ip }} {{ ansible_system_vendor }}_{{ ansible_product_name }} {{ ansible_os_family }}_{{ ansible_distribution_version }} {{ ansible_kernel }} {{ ansible_nodename }}"
OS 정보수집
에 대한 Playbook 분석
-
Inventory에 지정된 호스트에 지정된 정보를 수집한다.
- managed_ip => host var
- ansible_system_vendor => Fact Var ( H/W Vendor 수집)
- ansible_product_name => Fact Var ( H/w Model 수집)
- ansible_os_family => Fact Var ( OS Distro Version Major version 수집 )
- ansible_distribution_version => Fact Var ( OS Distro Version Minor version 수집 )
- ansible_kernel => Fact Var ( OS Kernel Version 수집)
- ansible_nodename => Fact Var ( OS Hostname 수집 )
- delegate_to: localhost 는 원격지가 아닌, 현재 수행되는 시스템(PC 또는 서버)에서 수행하는 내역이다.
- 존재하는 사용자만(user_exists 변수) 패스워드 변경을 수행하는데, 매번 수행 시마다(update_password: always) 패스워드를 변경한다.
playbook 실행
# ansible-playbook -i hosts system_env.yml
수행 결과
# cat OS_VERSION_RESULT/2020/12/03/os_version_result.txt
172.16.0.100 Intel Corporation_S1200BTL Debian_9.11 4.9.0-11-amd64 test1
172.16.0.200 Intel Corporation_S2600GZ Debian_9.11 4.9.0-11-amd64 test2