관리하는 시스템이 많아지고 반복적인 작업이 많을 때 ansible을 사용하면 편할 것 같아
ansible을 사용하여 mariadb 를 관리하는 방법을 준비중인데 이번 글에서는 제가 사용하고 있는 ansible 관련 설정들을 공유하겠습니다.
-1. ansible 설치
[root@test_ansible]# git clone https://github.com/ansible/ansible.git [root@test_ansible]# make
* make error 발생 시
Traceback (most recent call last): File "packaging/release/versionhelper/version_helper.py", line 9, in <module> from packaging.version import Version, VERSION_PATTERN ImportError: No module named packaging.version Makefile:40: *** "version_helper failed". Stop.
[root@test_ansible]# pip install --upgrade setuptools [root@test_ansible]# pip install packaging
=> 설치 후 make 재시도
-2. ansible 기본설정 파일
1) ansible.cfg
ansible 사용 계정 profile에 ANSIBBLE_HOME 설정하면
export ANSIBLE_HOME=/engn001/ansible
ansible command 사용 시 아래의 ansible.cfg 설정파일을 자동으로 load 함
[defaults] # some basic default values... inventory = /engn001/ansible/inventory/hosts_test host_key_checking = False ansible_ssh_user = masvc01
2) inventory
[dbservers_test_users01] 11.11.111.11 22.222.222.22 333.333.333.33 444.444.444.555 [dbservers_test_users01:vars] ansible_connection=ssh ansible_ssh_user=test_users01
[dbservers_test_users02] 11.11.111.111 22.222.222.222 333.333.333.333 444.444.444.5555 [dbservers_test_users02:vars] ansible_connection=ssh ansible_ssh_user=test_users02
=> ansible 로 접속할 서버들 리스트이며 all 로 모든 대상서버에 command를 수행하거나
[ ~ ] 로 그룹핑한 서버들에만 선별적으로 command 수행 가능.
/etc/hosts 에 대상 서버 ip에 alias를 주면 inventory 파일에서 alias 로 지정 가능
* 대상서버들은 ansible 서버와 ssh keygen 을 통해 key 작업을 해주어야함
3) playbook
--- - hosts: dbservers_test_users01 become_user: test_user01 tasks: - name: source profile shell: source .bash_profile && echo $MARIA_HOME args: chdir: /home/test_user01/ executable: /bin/bash register: maria_home - name: mysql command command: mysql -utestuser -ptest123 -S "{{ item }}"/mysql/mysql.sock -e "show processlist\G" with_items: - "{{ maria_home.stdout }}" register: result - debug: msg="{{ result.results | json_query('[].stdout_lines[]') }}"
=> 위와 같이 playbook 이라는 기능을 사용하여 대상 서버들에서 수행할 task들을 설정할 수 있음
위의 playbook 을 살펴보면 inventory 파일의 대상서버들 중 [dbservers_test_users01] 그룹을 대상으로 하고 test_user01 계정으로 task 들을 수행함
source profile :
ansible이 대상 서버에 ssh 수행하는 방식은 interactive 하기 때문에 login shell 이 수행 되지 않아 대상 서버 계정의 profile 을 사용하려면 위와 같이 설정이 필요함
mysql command:
profile 을 읽어 maria db의 home 을 변수로 저장 후 mysql 커맨드를 수행하는데 활용함
-3. 수행 화면
[root@test_ansible]# ansible-playbook this.yml PLAY [dbservers_test_user01] ******************************************************* TASK [Gathering Facts] ********************************************************* ok: [11.111.111.111] ok: [22.222.222.222] TASK [source profile] ********************************************************** changed: [22.222.222.222] changed: [11.111.111.111] TASK [mysql command] *********************************************************** changed: [22.222.222.222] => (item=/engn001/test_user01/TEST_10.2.4) changed: [11.111.111.111] => (item=/engn001/test_user01/TEST_10.2.4) TASK [debug] ******************************************************************* ok: [11.111.111.111] => { "msg": [ "*************************** 1. row ***************************", " Id: 1", " User: system user", " Host: ", " db: NULL", " Command: Daemon", " Time: NULL", " State: InnoDB background thread", " Info: NULL", "Progress: 0.000", "*************************** 2. row ***************************", " Id: 2", " User: system user", " Host: ", " db: NULL", " Command: Daemon", " Time: NULL", " State: InnoDB background thread", " Info: NULL", "Progress: 0.000", ] } ok: [22.222.222.222] => { "msg": [ "*************************** 1. row ***************************", " Id: 1", " User: system user", " Host: ", " db: NULL", " Command: Daemon", " Time: NULL", " State: InnoDB background thread", " Info: NULL", "Progress: 0.000", "*************************** 2. row ***************************", " Id: 2", " User: system user", " Host: ", " db: NULL", " Command: Daemon", " Time: NULL", " State: InnoDB background thread", " Info: NULL", "Progress: 0.000", ] } PLAY RECAP ********************************************************************* 11.111.111.111 : ok=4 changed=2 unreachable=0 failed=0 skipped=0 22.222.222.222 : ok=4 changed=2 unreachable=0 failed=0 skipped=0