what's ansible & use case (by 이세웅)
TRANSCRIPT
Ansible
April 22, 2017
LEE SAE WOONG
Motive
MEB 3.10 MEB 3.12
UPGRADE
UPGRADE issue MEB 3.10 to MEB 3.12
[localhost]# mkdir –p /mysql/backup [localhost]# cd /mysql/backup [localhost]# wget http://mysql/download/meb_upgrade.sh [localhost]# sh meb_upgrade.sh
Upgrade Script
MEB 3.10 MEB 3.12
UPGRADE
1000 over
How to deploy 1000 over? How next MEB 3.14?
[localhost]# mkdir –p /mysql/backup [localhost]# cd /mysql/backup [localhost]# wget http://mysql/download/meb_upgrade.sh [localhost]# sh meb_upgrade.sh
Deploy with bash shell
For $hostlist loop
mkdir –p /mysql/backup cd /mysql/backup
wget http://mysql/download/meb_upgrade.sh sh meb_upgrade.sh
done
[localhost]# mkdir –p /mysql/backup [localhost]# cd /mysql/backup [localhost]# wget http://mysql/download/meb_upgrade.sh
[localhost]# sh meb_upgrade.sh
Deploy with bash shell
For $hostlist loop
mkdir –p /mysql/backup cd /mysql/backup wget
wget http://mysql/download/meb_upgrade.sh sh meb_upgrade.sh
Done
More `Easy`, `comfortable`, `Safety` deploy?
Completed Basic TEST
Using Ansible
Define Deploy method
Dev Ops
TEAM
MEB 3.12
• Ansible in a Nutshell • Ansible Installation • How to use ansible? • Ansible Use Case • Demo • Ansible UI tool
Ansible in a Nutshell
Ansible in a Nutshell Launched in early 2013
October 16 2015, Redhat acquire Ansible
Simple, Agentless
Easy to manage
Idempotent (ansible module)
100% Python
Configuration management
Uses ssh
Don’t need to learn a programming language(i.e. Ruby)
http://docs.ansible.com/intro_installation.html
Ansible in a Nutshell Launched in early 2013
October 16 2015, Redhat acquire Ansible
Simple, Agentless
Easy to manage
Idempotent (ansible module)
100% Python
Configuration management
Uses ssh
Don’t need to learn a programming language(i.e. Ruby)
http://docs.ansible.com/intro_installation.html
Ansible in a Nutshell cont. Ansible is modular. 1024+ • MySQL Modules
– mysql_db Add remove databases to remote hosts
– mysql_replication Manage MySQL replication
– mysql_user MySQL user management
– mysql_variables Manage global variables
• Others Modules – Mongodb, postgresql
– Package managers (yum, apt)
– Service
– File
– Template
Ansible in a Nutshell cont.
Ansible is a very popular open source project.
- 22,519 stars and 7,374 forks on GitHub on ‘17.04.11
- Puppet : 4,414 stars, 1,825 forks
- Chef : 4,773 stars, 1,992 forks
- Fluentd : 5,127 stars, 617 forks
Ansible in a Nutshell cont.
Google Trends (i.e.https://www.google.co.kr/trends/explore#q=ansible)
> Interest over time > Regional interest
Installation
# install the epel-release RPM if needed on CentOS, RHEL, or Scientific
Linux
– Ansible Manual
$ wget --no-check-certificate https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm
$ rpm -Uvh epel-release-latest-6.noarch.rpm
$ yum –y install ansible
$ ansible --version
ansible 2.2.1.0
config file = /etc/ansible/ansible.cfg
configured module search path = Default w/o overrides
http://docs.ansible.com/intro_installation.html
# Test installation case1
• Set hosts to test
$ cat /etc/ansible/ansible.cfg | grep
inventory = /etc/ansible/hosts
$ echo "127.0.0.1" > /etc/ansible/hosts
• Test your installation:
$ ansible all -m ping --connection=local
127.0.0.1 | success >> {
"changed": false,
"ping": "pong"
}
http://docs.ansible.com/ansible/playbooks_delegation.html
# Test installation case2
• Set hosts to test
$ echo "127.0.0.1" > ~/ansible_hosts
$ export ANSIBLE_HOSTS=~/ansible_hosts
• Test your installation:
$ ansible all -m ping -i ~/ansible_hosts --connection=local
127.0.0.1 | success >> {
"changed": false,
"ping": "pong"
}
How to use ansible?
Ansible Vocabulary
Playbooks Tags
Inventory Groups
Templates Variables
Facts Roles
• Ad-Hoc $ ansible mysql -m yum –a "name=python state=installed"
• Playbooks $ ansible-playbook pb_mysql.yml
Playbooks
---
- name: Install python
yum:
name: python
state: installed
• YAML – Read as ‘yameul[야믈]’or ’[얌]’
– Data serialized form that people can easily read
– Origin: Yet Another Markup Language
– Now: Derived from YAML ain’t Markup Language
Playbooks cont.
[ json “rdbms”, { “mysql”:[“oracle”,”enterprise”,5.6.17] } ]
- rdbms YAML - mysql
- oracle
- enterprise
- 5.6.17
---
- name: Upgrade meb 3.12
hosts: mysql
tasks:
.......
- name: Check MEB versions
command: bash -c 'rpm -qa | grep meb'
register: message
- debug: var=message.stdout_lines
.......
Playbooks cont.
$ cat tags_test.yml --- - name: Upgrade meb 3.12 hosts: mysql gather_facts: false tasks: - name: Check rpm mysql library command: bash -c 'rpm -qa | grep mysql' tags: always - name: Check rpm meb version command: bash -c 'rpm -qa | grep meb' tags: rpm_meb - name: Check rpm zlib version command: bash -c 'rpm -qa | grep zlib' tags: rpm_zlib
Tags
$ cat /etc/ansible/hosts
[mysql] MYSQLDB0001 MYSQLDB0002 MYSQLDB0003 MYSQLDB0004 MYSQLDB0005
[mongo]
MONGODB1001 MONGODB1002 MONGODB1003 MONGODB1004 MONGODB1005
Inventory & Groups
• Jinja2 is templating language for Python – my.cnf.j2
– redis.conf.j2
– mongod.conf.j2
– index.php.j2
Templates
$ vi.my.cnf.j2 [mysqld] read-only = {{ mysql_read_only|default(0) }} datadir = {{ mysql_data_dir }} tmpdir = {{ mysql_tmp_dir }} pid-file = {{ mysql_logs_dir }}/mysqld.pid socket = /tmp/mysql.sock skip-name-resolve character-set-server = utf8 collation-server = utf8_general_ci
# cat mysql/vars.yml
…
mysql_daemon: mysql
mysql_conf_path: /dbpart/rexhome/my.cnf
mysql_homedir: /dbpart/rexhome
mysql_datadir: /dbpart/rexhome/data
mysql_socket:/dbpart/rexhome/tmp/mysqld.sock
mysql_log: /dbpart/rexhome/log
mysql_pid: /dbpart/rexhome/log/mysqld.pid
…
Variables
# In playbook:
- set_fact:
buffer_pool_size: "{{ (ansible_memtotal_mb * 0.6) | int }}"
# In my.cnf.j2 (template):
innodb-buffer-pool-size = {{ buffer_pool_size | int }}
Facts
mysql/tasks mysql/tasks/monitoring.yml mysql/tasks/backups.yml mysql/tasks/server.yml mysql/tasks/main.yml mysql/tasks/base.yml mysql/templates mysql/templates/5.7 mysql/templates/5.7/my.cnf.j2 mysql/templates/5.6 mysql/templates/5.6/my.cnf.j2 mysql/vars mysql/vars/main.yml mysql/files mysql/files/etc_init.d_mysql
Roles
Ansible Use Case
What can I use Ansible to Do?
Configure managemnet
mysql variables, user, schema …
OS crontab, /etc/init.d, variables …
Install server
mysql, redis, mongodb server …
Install AWS EC2 Instance
Application Deployment
source, file …
LINE Ansible Use Case MEB upgrade
: 1000+ 2016.05.17
MEM 2.3 fadeout : 1000+
2017.03.16
DB ACL : 1000+
2017.02.24
MySQL install & Deploy tool
DB DDL : 4096 database
2017.03.10
2017.01.16
Demo
• Install Yum file
• MySQL create database
• MySQL add user
• MySQL variables
• MySQL root user password
https://github.com/saewoong/ansible_sample/blob/master/pb_mysql.yml
$ cat pb_mysql.yml --- - name: Set MySQL with modules hosts: mysql gather_facts: no vars: MySQL_root_user: root MySQL_root_pass: test MySQL_port: 3306 tasks: - name: install MySQL-python and python-simplejson yum: name: "{{ item }}" state: installed with_items: - MySQL-python - python-simplejson tags: yum - name: MySQL create database mysql_db: name=ansible_rex state=present login_user="{{ MySQL_root_user }}" login_password="{{ MySQL_root_pass }}" login_host="{{ inventory_hostname }}" login_port="{{ MySQL_port }}" tags: database
- name: MySQL user add_drop mysql_user: name=federer password=test state=present priv=*.*:ALL login_user="{{ MySQL_root_user }}" login_password="{{ MySQL_root_pass }}" login_host="{{ inventory_hostname }}" login_port="{{ MySQL_port }}" tags: add_user - name: MySQL variables mysql_variables: variable=max_connections value=3000 login_user=root login_password="{{ MySQL_root_pass }}" login_host="{{ inventory_hostname }}" login_port="{{ MySQL_port }}" tags: variables - name: Change MySQL root user password mysql_user: name="{{ MySQL_root_user }}" state=present password=rex host="{{ item }}" login_user=root login_password="{{ MySQL_root_pass }}" login_host="{{ inventory_hostname }}" login_port="{{ MySQL_port }}" with_items: - localhost tags: change_root
DEMO https://github.com/saewoong/ansible_sample/blob/master/pb_mysql.yml
Ansible UI Tool
Ansible Tower (Automation Framework)
Ansible Tower (Automation Framework)
Open Source alternative to Ansible Tower in ‘GitHub’
DB
Deploy Server
⑤ deploy
① pb_script_file.sh pb_script_query.sql
② pb.yml
③ git push
④ semaphore ‘run’
LINE deploy tool
Question?
( We are hiring )