본문 바로가기

클라우드/앤서블(Ansible)

앤서블 Ansible: Apache + Wordpress + MySQL 배포 3

https://daaa0555.tistory.com/382

 

앤서블 Ansible: Apache + Wordpress + MySQL 배포 2

https://daaa0555.tistory.com/381 앤서블 Ansible: Apache + Wordpress + MySQL 배포 1 https://daaa0555.tistory.com/351 앤서블 Ansible 플레이북 ( playbook ) / wordpress 구성하기 2 wpdeploy.yaml 더보기 -..

daaa0555.tistory.com

 

 

# 역할 (roles)

표준화 된 디렉토리 및 파일 구조를 기반으로 변수, 작업, 핸들러, 템플릿 등을 구조화 하며, Ansible Artifact를 자동으로 가져올 수 있다. 또한 서비스 와 기능별로 역할을 분리하면 쉽게 재사용하고 다른 사용자와 역할을 공유 할 수 있다.

 

tree

 

 


~/wp/wpsite.yaml

더보기

- name: Database
  hosts: dbserver
  roles:
    - database

- name: Web
  hosts: webserver
  roles:
    - web

 

~/wp/ansible.cfg

더보기

[defaults]
inventory= ./inventory

 

~/wp/inventory/hosts

더보기

[webserver]
192.168.200.102
[dbserver] 
192.168.200.101

[local]
192.168.200.100

 

~/wp/group_vars/webserver.yaml

더보기

service_port: 8080

 

 


# database

~/wp/roles/database/handlers/main.yaml

더보기

- name: Restart MySQL services
  service:
    name: mysql
    state: restarted


- name: Restart MariaDB service
  service:
    name: mariadb
    state: restarted


 

~/wp/roles/database/vars/main.yaml

더보기

db:
  mysql_sock: "/var/run/mysqld/mysqld.sock"
  mysql_db_name: "wpdb"
  mysql_user_name: "wpadm"
  mysql_user_passwd: "qwer1234"
cat: o: No such file or directory

 

~/wp/roles/database/tasks/main.yaml

더보기

- name: Install Database in Debian
  import_tasks: db_debian.yaml
  when: ansible_facts['os_family'] == "Debian"

- name: Install Database in RedHat
  import_tasks: db_redhat.yaml
  when: ansible_facts['os_family'] == "RedHat"

 

~/wp/roles/database/tasks/db_debian.yaml

더보기

- name: Install MySQL Package
  apt:
    update_cache: true
    name: mysql-server, python3-pymysql
    state: present
- name: Starting MySQL Service
  service:
    name: mysql
    state: started
    enabled: yes
- name: Create Wordpress Database
  mysql_db:
    login_unix_socket: "{{ db['mysql_sock'] }}"
    name: "{{ db['mysql_db_name'] }}"
    state: present
- name: Create Wordpress User
  mysql_user:
    check_implicit_admin: yes
    login_unix_socket: "{{ db['mysql_sock'] }}"
    name: "{{ db['mysql_user_name'] }}"
    password: "{{ db['mysql_user_passwd'] }}"
    host: "{{ groups['webserver'][0] }}"
    priv: wpdb.*:ALL,GRANT
    state: present
- name: External Connection Setting
  lineinfile:
    path: /etc/mysql/mysql.conf.d/mysqld.cnf
    regexp: bind-address
    line: bind-address=0.0.0.0
  notify: Restart MySQL services

 

~/wp/roles/database/tasks/db_redhat.yaml

더보기

- name: Install MariaDB Package
  yum:
    update_cache: true
    name: mariadb-server, python3-pymysql
    state: present
- name: Starting Maria Service
  service:
    name: mariadb
    state: started
- name: Create Wordpress Database
  mysql_db:
    login_unix_socket: "{{ db['mysql_sock'] }}"
    name: "{{ db['mysql_db_name'] }}"
    state: present
    enabled: yes
- name: Create Wordpress User
  mysql_user:
    check_implicit_admin: yes
    login_unix_socket: "{{ db['mysql_sock'] }}"
    name: "{{ db['mysql_user_name'] }}"
    password: "{{ db['mysql_user_passwd'] }}"
    host: localhost              
    priv: wpdb.*:ALL,GRANT
    state: present
- name: External Connection Setting
  lineinfile:
    path: /etc/mysql/mysql.conf.d/mysqld.cnf
    regexp: bind-address
    line: bind-address=0.0.0.0
  notify: Restart MariaDB services


# web

 

~/wp/roles/web/handlers/main.yaml

더보기

- name: Restart Apache2 services
  service:
    name: apache2 
    state: restarted



- name: Restart Httpd services
  service:
    name: httpd
    state: restarted

 

~/wp/roles/web/vars/main.yaml

더보기

db:
  mysql_sock: "/var/run/mysqld/mysqld.sock"
  mysql_db_name: "wpdb"
  mysql_user_name: "wpadm"
  mysql_user_passwd: "qwer1234"
web:
  wp_url: "https://wordpress.org/wordpress-{{ wp_version }}.tar.gz"
  wp_checksum: "https://wordpress.org/wordpress-{{ wp_version }}.tar.gz.sha1"
  wp_tar: "/tmp/wordpress-{{ wp_version }}.tar.gz"
  wp_config: "/var/www/html/wordpress/wp-config.php"
wp_version: "5.8"
port: 
  service_port: "{{ service_port }}"

 

~/wp/roles/web/tasks/main.yaml

더보기

- name: Install Wordpress in Debian
  import_tasks: wp_debian.yaml
  when: ansible_facts['os_family'] == "Debian"

- name: Install Wordpress in RedHat
  import_tasks: wp_redhat.yaml
  when: ansible_facts['os_family'] == "RedHat"

 

~/wp/roles/web/tasks/wp_debian.yaml

더보기

- name: Download Wordpress Source
  get_url:
    url: "{{ web['wp_url'] }}"
 #  checksum: "{{ web['wp_checksum'] }}"
    dest: "{{ web['wp_tar'] }}"
  async: 300
  poll: 0
  register: wp_download_result
- name: Install Apache2 Package
  apt:
    name: apache2, php, php-mysql, php-gd, php-mbstring
    state: present
- name: Change Apache2 Service Ports
  template:
    src: ../templates/ports.conf.j2
    dest: /etc/apache2/ports.conf
  notify:
    - Restart Apache2 services
- name: Starting Apache2 Service
  service:
    name: apache2
    state: started
    enabled: yes
- name: Sync Download Wordpress
  async_status:
    jid: "{{ wp_download_result.ansible_job_id }}"
  register: job_result
  until: job_result.finished
  retries: 100
  delay: 5
- name: Unarchive Wordpress Source
  unarchive:
    src: "{{ web['wp_tar'] }}"
    remote_src: true
    dest: /var/www/html
- name: Setting Wordpress Database Configuration
  template:
    src: ../templates/wp-config.php.j2
    dest: "{{ web['wp_config'] }}"

 

~/wp/roles/web/tasks/wp_redhat.yaml

더보기

- name: Download Wordpress Source
  get_url:
    url: "{{ web['wp_url'] }}"
 #  checksum: "{{ web['wp_checksum'] }}"
    dest: "{{ web['wp_tar'] }}"
  async: 300
  poll: 0
  register: wp_download_result
- name: Install Httpd Package
  apt:
    name: httpd, php, php-mysql, php-gd, php-mbstring
    state: present
- name: Change Httpd Service Ports
  template:
    src: ../templates/httpd.conf.j2
    dest: /etc/httpd/conf/https.conf
  notify:
    - Restart Httpd services
- name: Starting Httpd Service
  service:
    name: httpd
    state: started
    enabled: yes
- name: Sync Download Wordpress
  async_status:
    jid: "{{ wp_download_result.ansible_job_id }}"
  register: job_result
  until: job_result.finished
  retries: 100
  delay: 5
- name: Unarchive Wordpress Source
  unarchive:
    src: "{{ web['wp_tar'] }}"
    remote_src: true
    dest: /var/www/html
- name: Setting Wordpress Database Configuration
  template:
    src: ../templates/wp-config.php.j2
    dest: "{{ web['wp_config'] }}"

 

~/wp/roles/web/templates/ports.conf.j2

더보기

# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default.conf

Listen "{{ port['service_port'] }}"

<IfModule ssl_module>
        Listen 443
</IfModule>

<IfModule mod_gnutls.c>
        Listen 443
</IfModule>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noeti

 

~/wp/roles/web/templates/httpd.conf.j2

더보기

Listen "{{ port['service_port'] }}"

 

~/wp/roles/web/templates/wp-config.php.j2

더보기

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME','{{ db['mysql_db_name'] }}' );

/** MySQL database username */
define( 'DB_USER', '{{ db['mysql_user_name'] }}' );

/** MySQL database password */
define( 'DB_PASSWORD', '{{ db['mysql_user_passwd'] }}' );

/** MySQL hostname */
define( 'DB_HOST', "{{ groups['dbserver'][0] }}" );

/** Database charset to use in creating database tables. */
define( 'DB_CHARSET', 'utf8' );

/** The database collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', '' );

 


# remove

~/wp/roles/remove/main.yaml
~/wp/roles/remove/main.yaml

더보기

- name: Wordpress Remove
  hosts: 192.168.200.102
  tasks:
  - name: Stop Apache2 Service
    service:
      name: apache2
      state: stopped
    ignore_errors: true
  - name: Remove Apache2 Package
    apt:
      name: apache2, php, php-mysql, php-gd, php-mbstring
      state: absent
      purge: yes
  - name: Remove Dep-Apache2 Package
    apt:
      autoremove: yes
      purge: yes
  - name: Remove Apache Configuration
    file:
      path: /etc/apache2
      state: absent
  - name: Delete Wordpress Archive Files
    file:
      path: /tmp/wordpress-5.8.tar.gz
      state: absent
  - name: Delete Wordpress App
    file:
      path: /var/www/html
      state: absent

- name: MySQL Remove
  hosts: 192.168.200.101
  tasks:
  - name: Stop MySQL Service
    service:
      name: mysql
      state: stopped
    ignore_errors: true
  - name: Remove MySQL Package
    apt:
      name: mysql-server, python3-pymysql, mysql-common, mysql-client
      state: absent
      purge: yes
  - name: Remove MySQL Configuration
    file:
      path: /etc/mysql
      state: absent
  - name: Remove Dep-MySQL Package
    apt:
      autoremove: yes
      purge: yes
  - name: Cleanup Databases
    file:
      path: /var/lib/mysql
      state: absent