๊ด€๋ฆฌ ๋ฉ”๋‰ด

data_lab

[ansible] mysql ์„ค์น˜ํ•˜๊ธฐ ๋ณธ๋ฌธ

LINUX

[ansible] mysql ์„ค์น˜ํ•˜๊ธฐ

๐Ÿฐํžˆํžˆ 2021. 6. 14. 21:40

ansible ๋””๋ ‰ํ„ฐ๋ฆฌ ๊ตฌ์กฐ

 

  1.  /etc/ansible  - ansible ํ™ˆ
    1. hosts - ansible inventory ๋กœ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋‚ ๋ฆด ์›๊ฒฉ ์„œ๋ฒ„ ๋ชจ์Œ
    2. ansible.cfg - ansible ํ™˜๊ฒฝ ์„ค์ •์œผ๋กœ sh ์‹คํ–‰ sh,bash ์ผ์ง€ ์„ ํƒํ•˜๊ฑฐ๋‚˜ ansible ๋กœ๊ทธ ๋‚จ๊ธฐ๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ ์„ค์ •
    3. roles - ansible ์‹คํ–‰ํ•  playbook ๋ชจ์Œ (task ๋ชจ์Œ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋จ)
      1. mysql 
        1. tasks - mysql ์„ค์น˜ํ•˜๋Š” task ๋ชจ์Œ
        2. templates - mysql ์„ค์น˜ ํ›„ ํ™˜๊ฒฝ ์„ธํŒ…ํ•˜๋Š” ํŒŒ์ผ ๋ชจ์Œ (ex : my.ini)
        3. handlers - mysql service ์‹คํ–‰ 
    4. example_site.yml - roles์— ์ž‘์„ฑ๋œ tasks ์‹คํ–‰ํ•  ํŒŒ์ผ

 

ํ•ด๋‹น ์ฝ”๋“œ๋Š” github๋ฅผ ์ฐธ์กฐํ•˜์˜€์Œ

https://github.com/ansible/ansible-examples/tree/master/wordpress-nginx

 

ansible/ansible-examples

A few starter examples of ansible playbooks, to show features and how they work together. See http://galaxy.ansible.com for example roles from the Ansible community for deploying many popular appl...

github.com

ansible.cfg ์„ค์ •

 ** remote ์„œ๋ฒ„์— ssh-copy-id๋กœ ์‚ฌ์ „์ž‘์—… ๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.

sudo ๊ถŒํ•œ์„ ์ค„ userid ์ž‘์„ฑ

1
2
3
4
5
6
[defaults]
 
# some basic default values...
inventory      = /etc/ansible/hosts
sudo_user      = userId
ask_pass      = False
cs

 

{ANSIBLE_HOME}/roles/mysql/tasks/main.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
---
- name: Install Mysql package
  yum: name={{ item }} state=present
  with_items:
   - http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
   - mysql-community-server
   - MySQL-python
   - libselinux-python
   - libsemanage-python
 
- name: Configure SELinux to start mysql on any port
  seboolean: name=mysql_connect_any state=true persistent=yes
  when: ansible_selinux.status == "enabled"
 
- name: Create Mysql configuration file
  template: src=my.cnf.j2 dest=/etc/my.cnf
  notify:
  - restart mysql
 
- name: Start Mysql Service
  service: name=mysqld state=started enabled=yes
cs

 

 

{ANSIBLE_HOME}/roles/mysql/templates/my.cnf.j2

1
2
3
4
5
6
7
8
9
10
11
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
port=3306
 
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
cs

 

 

{ANSIBLE_HOME}/roles/mysql/handlers/main.yml

1
2
3
---
- name: restart mysql
  service: name=mysqld state=restarted
cs

 

 

{ANSIBLE_HOME}/example_site.yaml

 

1
2
3
4
5
6
7
8
9
---
- name: Install MySQL
  hosts: all
  remote_user: userId
  become: yes
  become_method: sudo
 
  roles:
    - mysql
cs

 

{ANSIBLE_HOME}/hosts ์— ์„ค์น˜์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋‚ ๋ฆด ์›๊ฒฉ์„œ๋ฒ„์˜ ip๋ฅผ ์ ์–ด ์ค€๋‹ค.

 

ํ•ด๋‹น ์Šคํฌ๋ฆฝํŠธ ์‹คํ–‰ ๋ช…๋ น์–ด

 

1
$ ansible-playnook -i hosts example_site.yaml
cs

 

728x90
๋ฐ˜์‘ํ˜•