마짱짱의 지식창고

Wordpress 만드는 과정 3 - (변수 및 Jinja2 template) 본문

Automation/Ansible

Wordpress 만드는 과정 3 - (변수 및 Jinja2 template)

마짱짱 2020. 8. 14. 15:12
반응형

변수

  일부시스템에서 다른동작과 약간 다른 동작이나 구성을 설정하기 위해 따로 설정한다.

[student@controller ~]$ tree group_vars/
group_vars/
└── wp
    ├── apache.yaml
    ├── haproxy.yaml
    ├── mariadb.yaml
    ├── nfs.yaml
    └── wordpress.yaml

1 directory, 5 files
 # apache.yaml 
 apache:
  port: 80

php:
  repo:
    pkg: "https://rpms.remirepo.net/enterprise/remi-release-7.rpm "


# haproxy.yaml
haproxy:
  frontend:
    port: 80
  backend:
    name: wordpress
    balance_type: roundrobin
    wordpress1:
      port: 80
    wordpress2:
      port: 80


# mariadb.yaml
mariadb:
  repo:
    baseurl: http://mirror.yongbok.net/mariadb/yum/10.5/centos7-amd64
    gpgkey: http://mirror.yongbok.net/mariadb/yum/RPM-GPG-KEY-MariaDB
  wp:
    name: wordpress_db
    user: admin
    pwd: dkagh1.
    priv: wordpress_db.*:ALL,GRANT
    host: '192.168.123.%'
  port: 3306


# nfs.yaml
nfs:
  exports:
    directory: /wordpress
    subnet: 192.168.123.0/24
    options: rw,sync,no_root_squash
  block:
    device: /dev/vdb
    fs_type: ext4


# wordpress.yaml
wordpress:
  source:
    version: 5.3.4
    language: ko_KR
  db:
    name: wordpress_db
    username: admin
    password: dkagh1.
    host: 192.168.123.54

 

 

Template (템플릿, Jinja2)

  동적 표현식을 활성화하고 변수에 접근할 수 있다.

[student@controller ~]$ tree templates/
templates/
├── apache.conf.j2
├── exports.j2
├── haproxy.cfg.j2
├── my.cnf.j2
└── wp-config.php.j2

0 directories, 5 files
# apache.conf.j2
Listen {{ ansible_eth1.ipv4.address }}:{{ apache['port'] }}

Include conf.modules.d/*.conf

User apache
Group apache

ServerAdmin root@localhost

<Directory />
    AllowOverride none
    Require all denied
</Directory>

DocumentRoot "/var/www/html"

<Directory "/var/www">
    AllowOverride None
    # Allow open access:
    Require all granted
</Directory>

<Directory "/var/www/html">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

<IfModule dir_module>
    DirectoryIndex index.html
</IfModule>

<Files ".ht*">
    Require all denied
</Files>

ErrorLog "logs/error_log"

LogLevel warn

<IfModule log_config_module>
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common

    <IfModule logio_module>
      # You need to enable mod_logio.c to use %I and %O
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>
    CustomLog "logs/access_log" combined
</IfModule>

<IfModule alias_module>
# exports.j2
{{ nfs['exports']['directory'] }} {{ nfs['exports']['subnet'] }}({{ nfs['exports']['options'] }})
# haproxy.cfg.j2
global
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon
    stats socket /var/lib/haproxy/stats
defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000

frontend  main {{ haproxy_public_ip }}:{{ haproxy['frontend']['port'] }}
    acl url_static       path_beg       -i /static /images /javascript /stylesheets
    acl url_static       path_end       -i .jpg .gif .png .css .js

    #use_backend static          if url_static
    default_backend              {{ haproxy['backend']['name'] }}

backend static
    balance     roundrobin
    server      static 127.0.0.1:4331 check

backend {{ haproxy['backend']['name'] }}
    balance     {{ haproxy['backend']['balance_type'] }}
    server  web1 {{ wordpress1_private_ip }}:{{ haproxy['backend']['wordpress1']['port'] }} check
    server  web2 {{ wordpress2_private_ip }}:{{ haproxy['backend']['wordpress2']['port'] }} check
# my.cnf.j2
[mysqld]
bind-address={{ ansible_eth1.ipv4.address }}
port={{ mariadb['port'] }}

[galera]

[embedded]

[mariadb]

[mariadb-10.5]
# wp-config.php.j2
<?php
/**
 * The base configuration for WordPress
 *
 * The wp-config.php creation script uses this file during the
 * installation. You don't have to use the web site, you can
 * copy this file to "wp-config.php" and fill in the values.
 *
 * This file contains the following configurations:
 *
 * * MySQL settings
 * * Secret keys
 * * Database table prefix
 * * ABSPATH
 *
 * @link https://wordpress.org/support/article/editing-wp-config-php/
 *
 * @package WordPress
 */

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

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

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

/** MySQL hostname */
define( 'DB_HOST', '{{ wordpress['db']['host'] }}' );

/** 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', '' );

/**#@+
 * Authentication Unique Keys and Salts.
 *
 * Change these to different unique phrases!
 * You can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}
 * You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.
 *
 * @since 2.6.0
 */
define( 'AUTH_KEY',         'put your unique phrase here' );
define( 'SECURE_AUTH_KEY',  'put your unique phrase here' );
define( 'LOGGED_IN_KEY',    'put your unique phrase here' );
define( 'NONCE_KEY',        'put your unique phrase here' );
define( 'AUTH_SALT',        'put your unique phrase here' );
define( 'SECURE_AUTH_SALT', 'put your unique phrase here' );
define( 'LOGGED_IN_SALT',   'put your unique phrase here' );
define( 'NONCE_SALT',       'put your unique phrase here' );

/**#@-*/

/**
 * WordPress Database Table prefix.
 *
 * You can have multiple installations in one database if you give each
 * a unique prefix. Only numbers, letters, and underscores please!
 */
$table_prefix = 'wp_';

/**
 * For developers: WordPress debugging mode.
 *
 * Change this to true to enable the display of notices during development.
 * It is strongly recommended that plugin and theme developers use WP_DEBUG
 * in their development environments.
 *
 * For information on other constants that can be used for debugging,
 * visit the documentation.
 *
  * @link https://wordpress.org/support/article/debugging-in-wordpress/
 */
define( 'WP_DEBUG', false );

/* That's all, stop editing! Happy publishing. */

/** Absolute path to the WordPress directory. */
if ( ! defined( 'ABSPATH' ) ) {
        define( 'ABSPATH', __DIR__ . '/' );
}

/** Sets up WordPress vars and included files. */
require_once ABSPATH . 'wp-settings.php';


 

 

여기까지 완료 후 Playbook 을 01~05 순서대로 실행시키면 워드프레스 설치 자동화 완료된다.

반응형