최근에 회사에서 관리해야 하는 서버 수십대에 동일한 작업을 해야 하는 상황이 생겼다.
이런 상황에 유용한 Ansible을 이용하여 간단하게 자동화 할 수 있는 방법을 소개한다.
> yum install epel-release
> yum install ansible
# SSH 최초 접속시 host key download YES/NO 물어보는 절차 무시하기
host_key_checking = False
> vi /home/kwlee/ansible_works/hosts.ini
[servers]
10.10.10.21
10.10.10.22
10.10.10.23
10.10.10.24
> vi /home/kwlee/ansible_works/kwlee_pass_job.yml
---
- name: playbook
# /home/kwlee/ansible_works/hosts.ini 에 등록된 이름
hosts: servers
# sudo 를 이용함 (su 아니고 sudo)
become: yes
become_user: root
# task 시작
tasks:
# copy task (Controller 서버에 있는 .sh 파일을 리모트 서버에 업로드)
- name: Copy file kwlee_pass_job.sh
copy:
src: /myfile/kwlee_pass_job.sh
dest: /home/kwlee/kwlee_pass_job.sh
owner: root
group: root
mode: '0700'
# cron task (crontab 등록)
- name: add crontab
cron:
name: "kwlee_job"
minute: "35"
hour: "03"
day: "5"
job: "/home/kwlee/kwlee_pass_job.sh"
# copy task (여러 파일)
- name: Copy file expect and tcl
copy:
src: "{{ item }}"
dest: /home/kwlee/
with_fileglob:
- "/myfile/expect/*"
# yum task (tcl 설치)
- name: Install package tcl
yum:
pkg: tcl-8.5.13-8.el7.x86_64.rpm
state: present
# yum task (expect 설치)
- name: Install package expect
yum:
pkg: expect-5.45-14.el7_1.x86_64.rpm
state: present
> ansible-playbook -i hosts.ini -l servers kwlee_pass_job.yml -u kwlee -k -K
SSH password:
BECOME password[defaults to SSH password]:
PLAY [playbook] *******************************************************************************************************
TASK [Gathering Facts] ************************************************************************************************
ok: [10.10.10.24]
ok: [10.10.10.22]
ok: [10.10.10.23]
ok: [10.10.10.21]
TASK [add crontab] ****************************************************************************************************
ok: [10.10.10.23]
ok: [10.10.10.24]
ok: [10.10.10.22]
ok: [10.10.10.21]
TASK [Copy file kwlee_pass_job.sh] ************************************************************************************
ok: [10.10.10.23]
ok: [10.10.10.24]
ok: [10.10.10.21]
ok: [10.10.10.22]
TASK [Copy file expect and tcl] ***************************************************************************************
ok: [10.10.10.23] => (item=/home/kwlee/expect/expect-5.45-14.el7_1.x86_64.rpm)
ok: [10.10.10.24] => (item=/home/kwlee/expect/expect-5.45-14.el7_1.x86_64.rpm)
ok: [10.10.10.22] => (item=/home/kwlee/expect/expect-5.45-14.el7_1.x86_64.rpm)
ok: [10.10.10.21] => (item=/home/kwlee/expect/expect-5.45-14.el7_1.x86_64.rpm)
ok: [10.10.10.23] => (item=/home/kwlee/expect/tcl-8.5.13-8.el7.x86_64.rpm)
ok: [10.10.10.24] => (item=/home/kwlee/expect/tcl-8.5.13-8.el7.x86_64.rpm)
ok: [10.10.10.22] => (item=/home/kwlee/expect/tcl-8.5.13-8.el7.x86_64.rpm)
ok: [10.10.10.21] => (item=/home/kwlee/expect/tcl-8.5.13-8.el7.x86_64.rpm)
TASK [Install package tcl] ********************************************************************************************
ok: [10.10.10.23]
ok: [10.10.10.24]
ok: [10.10.10.21]
ok: [10.10.10.22]
TASK [Install package expect] *****************************************************************************************
ok: [10.10.10.23]
ok: [10.10.10.24]
ok: [10.10.10.21]
ok: [10.10.10.22]
PLAY RECAP ************************************************************************************************************
10.10.10.21 : ok=6 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
10.10.10.22 : ok=6 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
10.10.10.23 : ok=6 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
10.10.10.24 : ok=6 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
> crontab -l
#Ansible: kwlee_job
35 03 5 * * /home/kwlee/kwlee_pass_job.sh
> ls -al /home/kwlee
-rw-r--r-- 1 root root 268656 Apr 21 17:48 expect-5.45-14.el7_1.x86_64.rpm
-rwx------ 1 root root 358 Apr 21 15:09 kwlee_pass_job.sh
-rw-r--r-- 1 root root 1980564 Apr 21 17:48 tcl-8.5.13-8.el7.x86_64.rpm
> rpm -qa | grep tcl
tcl-8.5.13-8.el7.x86_64
> rpm -qa | grep expect
expect-5.45-14.el7_1.x86_64
댓글 영역