[Ansible] Playbook 으로 하나의 VM 에 Wordpress 구성

728x90

Wordpress 구성하는 데 필요한 Ansible Module

module index 공식 문서 : https://docs.ansible.com/ansible/2.9/modules/modules_by_category.html

패키지 설치(yum): yum

서비스 제어(systemctl): service

텍스트 수정(sed): lineinfile, blockinfile, replace

호스트 네임 설정(hostnamectl): hostname

압축 해제(tar): archive, unarchive

방화벽(firewall-cmd): firewalld(레드햇 계열), ufw(데비안 계열), iptables

파일 복사(cp): copy, fetch(관리 노드에 있는 파일을 제어 노드로 복사해서 가져올 때)

디렉토리 생성, 파일 권한 변경 등..(mkdir, chmod): file

파일 다운로드(wget): get_url

네트워크 설정(nmcli): nmcli

데이터베이스(mysql): mysql_db, mysql_user, meysql_info, mysql_replica...

SSL 인증서(mod_ssl): openssl_certificate, openssl_csr, openssl_*...

yum 에서 다운로드 받을 수 있는 php 버전이 5버전으로 너무 낮아서 remi repo 로 설치를 해야 한다. 아마존의 경우는 아마존 엑스트라로 레포를 확장시킬 수 있다.

Playbook

  • playbook: YAML 파일
    • .yaml, .yml
  • play: 관리 노드를 설정하는 부분
  • task: 모듈을 사용하여 작업 하는 부분, 옵션들은 들여쓰기 이후에 설정한다.
# test.yml
# play
- hosts: host1
  tasks:
    # task
    - yum:
      name: httpd
      state: installed
    # task
    - service:
      name: httpd
      state: started
      enabled: yes

ansible host1 -m yum -a 'name=httpd state=installed' ansible host1 -m service -a 'name=httpd state=started enabled=yes'

$ ansible-playbook test.yml

vim enhanced

sudo yum install -y vim-enhanced 로 vim 플러그인 다운로드

vim .vimrc 에서 다음과 같이 작성한다. yml 파일 만들 때 적용할 스타일을 만드는 작업이다.

[vagrant@controller ~]$ cat .vimrc
syntax on
autocmd FileType yaml setlocal ts=2 sts=2 sw=2 et ai
set cursorline

Playbook 을 이용하여 워드프레스 구성

자세한 설명은 여기를 참조

- hosts: wordpress
  tasks:
  - yum:
      name: https://rpms.remirepo.net/enterprise/remi-release-7.rpm
      state: present
      validate_certs: no
  - yum_repository:
      name: remi-safe
      file: remi-safe
      mirrorlist: http://cdn.remirepo.net/enterprise/7/safe/mirror
      description: remi-safe
      enabled: no
  - yum_repository:
      name: remi-php74
      file: remi-php74
      mirrorlist: http://cdn.remirepo.net/enterprise/7/php74/mirror
      description: remi-php74
      enabled: yes
  - yum:
      name: httpd,php,php-mysqlnd,mariadb,mariadb-server,python2-PyMySQL
      state: installed
  - service:
      name: httpd
      state: started
      enabled: yes
  - service:
      name: mariadb
      state: started
      enabled: yes
  - get_url:
      url: https://wordpress.org/wordpress-5.9.3.tar.gz
      dest: /home/vagrant
  - unarchive:
      src: /home/vagrant/wordpress-5.9.3.tar.gz
      remote_src: yes
      dest: /var/www/html
      owner: apache
      group: apache
  - mysql_db:
      name: wordpress
      state: present
      login_user: root
  - mysql_user:
      name: wpadm
      password: 1234
      state: present
      login_user: root
      priv: wordpress.*:ALL
  - copy:
      src: /var/www/html/wordpress/wp-config-sample.php
      remote_src: yes
      dest: /var/www/html/wordpress/wp-config.php
      owner: apache
      group: apache
  - replace:
      path: /var/www/html/wordpress/wp-config.php
      regexp: username_here
      replace: wpadm
  - replace:
      path: /var/www/html/wordpress/wp-config.php
      regexp: database_name_here
      replace: wordpress
  - replace:
      path: /var/www/html/wordpress/wp-config.php
      regexp: password_here
      replace: 1234

wordpress 를 설치하기 위한 플레이북 작성이 끝났다면 ansible-playbook test.yml -b -vvv 명령어를 실행하여 디버그 모드로 자세하게 살펴보자. -b 는 관리자 모드로 실행하겠다는 뜻

[vagrant@controller ~]$ ansible-playbook test.yml -b -vvv
ansible-playbook 2.9.27
  config file = /home/vagrant/.ansible.cfg
  configured module search path = [u'/home/vagrant/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible-playbook
  python version = 2.7.5 (default, Apr  2 2020, 13:16:51) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
Using /home/vagrant/.ansible.cfg as config file
host_list declined parsing /home/vagrant/inventory.ini as it did not pass its verify_file() method
script declined parsing /home/vagrant/inventory.ini as it did not pass its verify_file() method
auto declined parsing /home/vagrant/inventory.ini as it did not pass its verify_file() method
yaml declined parsing /home/vagrant/inventory.ini as it did not pass its verify_file() method
Parsed /home/vagrant/inventory.ini inventory source with ini plugin
Skipping callback 'actionable', as we already have a stdout callback.
Skipping callback 'counter_enabled', as we already have a stdout callback.
Skipping callback 'debug', as we already have a stdout callback.
Skipping callback 'dense', as we already have a stdout callback.
Skipping callback 'dense', as we already have a stdout callback.
Skipping callback 'full_skip', as we already have a stdout callback.
Skipping callback 'json', as we already have a stdout callback.
Skipping callback 'minimal', as we already have a stdout callback.
Skipping callback 'null', as we already have a stdout callback.
Skipping callback 'oneline', as we already have a stdout callback.
Skipping callback 'selective', as we already have a stdout callback.
Skipping callback 'skippy', as we already have a stdout callback.
Skipping callback 'stderr', as we already have a stdout callback.
Skipping callback 'unixy', as we already have a stdout callback.
Skipping callback 'yaml', as we already have a stdout callback.
.
.
.
PLAYBOOK: test.yml *************************************************************
1 plays in test.yml
PLAY RECAP *********************************************************************
192.168.100.12             : ok=16   changed=16    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
728x90