Ansible은 잘만 사용하면 굉장히 편한 opensource tool입니다.
배포하고자 하는 대상 서버에 별 다른 agent 설치가 필요없습니다.
이번 글에서는 Mysql DB설치 배포를 하고, 다음엔 MHA 구성하는 playbook을 짜보겠습니다.
테스트 환경
hostname
|
IP
|
private IP
|
version
|
db version
|
node1
|
115.85.182.24
|
192.168.100.51
|
centos7.8
|
mysql5.7
|
node2
|
49.50.164.159
|
192.168.100.52
|
centos7.8
|
mysql5.7
|
ansible
|
118.67.132.167
|
192.168.100.53
|
centos7.8
|
|
사전 작업
■ host 등록 (사전에 host를 등록해야 해당 IP 인식 가능)
[root@ansible ~]# vi /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.100.53 ansible
192.168.100.51 node1
192.168.100.52 node2
테스트 시작
■ ansible 설치(ansible 서버)
[root@ansible ~]# yum -y install ansible
[root@ansible ~]# vi /etc/ansible/hosts
## [dbservers]
##
## db01.intranet.mydomain.net
## db02.intranet.mydomain.net
## 10.25.1.56
## 10.25.1.57
ansible
node1
node2
■ Inventory -> server 연결 테스트
## ansible 전용 유저 생성(전 서버)
[root@ansible ~]# useradd kim
[root@ansible ~]# passwd kim
<패스워드 이용 시>
[root@ansible ~]# ansible node1 -m ping -u kim -k //hostname , module과 user 기입
SSH password:
node1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
[root@ansible ~]# ansible all -m ping -u kim -k
SSH password:
node1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
ansible | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
node2 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
<공개 키 이용시>
## 공개키 생성(전 서버)
[root@ansible ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:Gsm4LZH+iLMsAd1sHBzUIE1hOv6S2IxvwjxrfB1PHAo root@ansible
The key's randomart image is:
+---[RSA 2048]----+
| .=B= |
| ++ . |
| .o+ . |
|...E=+ o |
|. ..= * S |
|.= + * = |
|*.= * * |
|oOo+ = . |
|.=Oo. . |
+----[SHA256]-----+
[root@ansible ~]# ssh-copy-id kim@ansible
[root@ansible ~]# ssh-copy-id kim@node1
[root@ansible ~]# ssh-copy-id kim@node2
■ Test playbook 테스트
[root@ansible ~]# vi test.yml
---
- name: Test connectiong
hosts: all
remote_user: kim
tasks:
- name: test connection
ping:
[root@ansible ~]# ansible-playbook test.yml
PLAY [Test connectiong] *********************************************************************
TASK [Gathering Facts] **********************************************************************
ok: [ansible]
ok: [node2]
ok: [node1]
TASK [test connection] **********************************************************************
ok: [node2]
ok: [node1]
ok: [ansible]
PLAY RECAP **********************************************************************************
ansible : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node1 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node2 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
■ Mariadb 설치 yml파일
[root@node2 ~]# mkdir /etc/Downloads
## DB install playbook 작성
[root@ansible ansible]# vi dbinstall.yml
---
- name: mariadb_install
hosts: ansible,node1,node2
remote_user: root
tasks:
- name: Connection Test
ping:
- name: Install wget
yum:
name: wget
state: present
- name: Download mariadb_repo_setup File
get_url:
url: https://downloads.mariadb.com/MariaDB/mariadb_repo_setup
dest: /root/Downloads
- name: Change mode for mariadb_repo_setup
file:
dest: /root/Downloads/mariadb_repo_setup
mode: a+x
- name: excute mariadb_repo_setup
shell: /root/Downloads/mariadb_repo_setup
- name: Delete mariadb_repo_setup
file:
state: absent
path: /root/Downloads/mariadb_repo_setup
- name: Install MariaDB-server
yum:
name: MariaDB-server
state: present
- name: Config 수정
lineinfile:
path: /etc/my.cnf
regexp: '{{item.From}}'
line: '{{item.To}}'
state: present
with_items:
- { From: 'datadir=/var/lib/mysql', To: 'datadir=/data'}
- { From: 'socket=/var/lib/mysql/mysql.sock', To: 'socket=/tmp/mysql.sock'}
- { From: 'log-error=/var/log/mariadb/mariadb.log', To: 'log-error=/log/mariadb.log'}
- { From: 'pid-file=/var/run/mariadb/mariadb.pid', To: 'pid-file=/log/mariadb.pid'}
- name: Mariadb Start
systemd:
state: started
daemon_reload: yes
name: mariadb
## 실행
[root@ansible ansible]# ansible-playbook dbinstall.yml
PLAY [mariadb_install] ***********************************************************************
TASK [Gathering Facts] ***********************************************************************
ok: [node2]
ok: [node1]
ok: [ansible]
TASK [Test Connection] ********************************************************************
ok: [node1]
ok: [node2]
ok: [ansible]
TASK [Install wget] **************************************************************************
ok: [node2]
ok: [ansible]
ok: [node1]
TASK [Download mariadb_repo_setup File] ***************************************************
changed: [ansible]
changed: [node1]
changed: [node2]
TASK [Change mode for mariadb_repo_setup] ****************************************************
changed: [node2]
changed: [node1]
changed: [ansible]
TASK [excute mariadb_repo_setup] *************************************************************
changed: [node2]
changed: [node1]
changed: [ansible]
TASK [Delete mariadb_repo_setup] *************************************************************
changed: [ansible]
changed: [node2]
changed: [node1]
TASK [Install MariaDB-server] ****************************************************************
ok: [ansible]
changed: [node1]
changed: [node2]
TASK [Config 수정] ****************************************************************************************************************************************************************************
changed: [node2] => (item={u'To': u'datadir=/data', u'From': u'datadir=/var/lib/mysql'})
changed: [node1] => (item={u'To': u'datadir=/data', u'From': u'datadir=/var/lib/mysql'})
changed: [ansible] => (item={u'To': u'datadir=/data', u'From': u'datadir=/var/lib/mysql'})
ok: [node1] => (item={u'To': u'socket=/tmp/mysql.sock', u'From': u'socket=/var/lib/mysql/mysql.sock'})
changed: [node2] => (item={u'To': u'socket=/tmp/mysql.sock', u'From': u'socket=/var/lib/mysql/mysql.sock'})
changed: [ansible] => (item={u'To': u'socket=/tmp/mysql.sock', u'From': u'socket=/var/lib/mysql/mysql.sock'})
changed: [node2] => (item={u'To': u'log-error=/log/mariadb.log', u'From': u'log-error=/var/log/mariadb/mariadb.log'})
ok: [node1] => (item={u'To': u'log-error=/log/mariadb.log', u'From': u'log-error=/var/log/mariadb/mariadb.log'})
changed: [ansible] => (item={u'To': u'log-error=/log/mariadb.log', u'From': u'log-error=/var/log/mariadb/mariadb.log'})
changed: [node2] => (item={u'To': u'pid-file=/log/mariadb.pid', u'From': u'pid-file=/var/run/mariadb/mariadb.pid'})
ok: [node1] => (item={u'To': u'pid-file=/log/mariadb.pid', u'From': u'pid-file=/var/run/mariadb/mariadb.pid'})
changed: [ansible] => (item={u'To': u'pid-file=/log/mariadb.pid', u'From': u'pid-file=/var/run/mariadb/mariadb.pid'})
TASK [Start Mysql] **************************************************************************************************************************************************************************
ok: [ansible]
ok: [node2]
ok: [node1]
PLAY RECAP **********************************************************************************************************************************************************************************
ansible : ok=10 changed=5 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node1 : ok=10 changed=7 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node2 : ok=10 changed=7 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
playbook은 띄어쓰기도 굉장히 잘 맞춰줘야 오류가 안납니다. 하나하나 신경써야하지만 잘만 쓰면 굉장히 편하므로 좀 더 공부를 해봐야겠습니다~
참고
'Others > Opensource Tool' 카테고리의 다른 글
[Ansible - AWX설치] (0) | 2022.04.24 |
---|---|
[pt-query-digest - Mysql slowquery 분석 ] (0) | 2022.03.12 |
[Pacemaker & corosync 구성 Vip failover] (0) | 2022.01.20 |
[Redis - Sentinel 설정 & Failover test] part 2 (0) | 2022.01.20 |
[Redis - install & replication] part 1 (0) | 2022.01.20 |