Requirements
기본 설정을 하기 앞서 [Ansible] 시작하기 문서를 참고하여 호스트(Host)에 공개 키 등록하는 작업을 먼저 진행해주세요.
Hosts
키(Key) 확인
처음 Host와 연결하는 경우, 키(Key) 확인을 사용자에게 요청하는 것이 일반적입니다.
ANSIBLE_HOST_KEY_CHECKING=false ansible [your-hosts-group] -m ping
Configure
OS 작업은 총 12단계로 나눠 순서대로 진행합니다.
OS 패키지 업그레이드
OS 패키지 업데이트
HOSTNAME 변경
/etc/host에 HOSTNAME 등록
Profile 설정 추가
시간대 변경
rsyslog 서비스 재시작
sulog 활성화
rsyslog 설정 변경
crontab 설정
Swap memory 설정
재부팅
Playbook 작성
OS 작업을 수행하는 Playbook을 작성합니다.
vi ./site.yaml
# ./site.yaml
---
- name: Update packages & Modify some configurations
hosts: test
become: yes
vars_files:
- "./VARS/vars.yaml"
tasks:
# Server Updates
- name: Upgrade the OS (apt-get dist-upgrade)
apt:
upgrade: full
- name: Run the equivalent of "apt-get update" as a separate step
apt:
update_cache: yes
# This module does NOT modify /etc/hosts
- name: Set a hostname
ansible.builtin.hostname:
name: "{{ hostname }}{{groups['test'].index(inventory_hostname) + 1}}"
- name: Insert "Hostname" in /etc/hosts
ansible.builtin.lineinfile:
path: /etc/hosts
insertafter: '^127.0.0.1 '
line: "{{ hostvars[inventory_hostname]['ansible_default_ipv4']['address'] }} {{ hostname }}{{groups['test'].index(inventory_hostname) + 1}}"
- name: Insert/Update "profile" configuration block in /etc/profile
blockinfile:
path: /etc/profile
block: |
HISTTIMEFORMAT="%Y-%m-%d [%H:%M:%S] " # history 포맷 설정
export HISTTIMEFORMAT
HISTFILESIZE=10000 # history 기록 사이즈 및 수 설정
HISTSIZE=10000
TMOUT=600 # 터미널에서 아무런 작업이 없을 시 세션 종료 설정(초)
export TMOUT
- name: Set timezone to Asia/Seoul
community.general.timezone:
name: Asia/Seoul
- name: Insert/Update "profile" configuration block in /etc/profile
blockinfile:
path: /etc/rsyslog.d/50-default.conf
block: |
*.=info;*.=notice;*.=warn;\
auth,authpriv.none;\
cron,daemon.none;\
mail,news.none -/var/log/messages
- name: Restart service rsyslog, in all cases, also issue daemon-reload to pick up config changes
ansible.builtin.systemd:
state: restarted
daemon_reload: yes
name: rsyslog
- name: Ensure system records all attempts by users on the system to execute the su command in /var/log/sulog
ansible.builtin.lineinfile:
path: /etc/login.defs
insertafter: '^#SULOG_FILE'
line: SULOG_FILE /var/log/sulog
- name: Creating a file with content
copy:
dest: "/etc/logrotate.d/rsyslog"
content: "{{rsyslog}}"
- name: Excecute daily at 00:00 AM
ansible.builtin.lineinfile:
path: /etc/crontab
line: 0 0 * * * root logrotate -f /etc/logrotate.d/rsyslog
# Swap Memory
- name: Set swap_file variable
set_fact:
swap_file: "{{swap_file_path}}"
tags:
- swap.set.file.path
- name: Check if swap file exists
stat:
path: "{{swap_file}}"
register: swap_file_check
tags:
- swap.file.check
- name: Create swap file
command: fallocate -l {{swap_file_size}} {{swap_file}}
when: not swap_file_check.stat.exists
tags:
- swap.file.create
- name: Change swap file permissions
file: path="{{swap_file}}"
owner=root
group=root
mode=0600
tags:
- swap.file.permissions
- name: Format swap file
command: "mkswap {{swap_file}}"
when: not swap_file_check.stat.exists
tags:
- swap.file.mkswap
- name: Write swap entry in fstab
mount: name=none
src={{swap_file}}
fstype=swap
opts=sw
passno=0
dump=0
state=present
tags:
- swap.fstab
- name: Turn on swap
command: swapon -a
when: not swap_file_check.stat.exists
tags:
- swap.turn.on
- name: Set swappiness
sysctl:
name: vm.swappiness
value: "{{swappiness}}"
tags:
- swap.set.swappiness
- name: Reboot a slow machine that might have lots of updates to apply
reboot:
reboot_timeout: 60
변수를 저장하는 파일을 작성합니다.
vi ./VARS/vars.yaml
여러 대의 호스트를 대상으로 hostname을
# ./VARS/vars.yaml
---
# Device Details
hostname: web0
# Swap Memory
swap_file_path: /swapfile
# Use any of the following suffixes
# c=1
# w=2
# b=512
# kB=1000
# K=1024
# MB=1000*1000
# M=1024*1024
# xM=M
# GB=1000*1000*1000
# G=1024*1024*1024
swap_file_size: 2G
swappiness: 1
# rsyslog
rsyslog: |
/var/log/syslog
{
su root root
rotate 31
daily
missingok
notifempty
nocompress
delaycompress
dateext
dateyesterday
postrotate
/usr/lib/rsyslog/rsyslog-rotate
endscript
}
/var/log/mail.info
/var/log/mail.warn
/var/log/mail.err
/var/log/mail.log
/var/log/daemon.log
/var/log/kern.log
/var/log/auth.log
/var/log/user.log
/var/log/lpr.log
/var/log/cron.log
/var/log/debug
/var/log/messages
{
su root root
rotate 31
daily
missingok
notifempty
nocompress
dateext
dateyesterday
delaycompress
sharedscripts
postrotate
/usr/lib/rsyslog/rsyslog-rotate
endscript
}
Playbook 수행
작성한 Playbook을 수행합니다.
ansible-playbook site.yaml
성공적으로 수행을 끝냈다면, 각 서버로 들어가 변경한 Profile 파일을 읽습니다.
ssh ubuntu@10.10.10.10
source /etc/profile
각 서버에 들어가서 source 명령어를 사용하는 이유를 참고하세요.
[Ansible] Ubuntu 20.04 OS 기본 설정