workspace.v1.1.zip调整内容:

  1. selinux/config文件替换步骤,使用lineinfile模块代替shell模块;
  2. 引入handlernotify关键词及触发器概念,将多次重启Apache步骤改为handler操作;
  3. 引入变量概念,将多次使用的数据库相关参数在变量中提前声明;
  4. 引用template概念,使用预定义的模板文件生成wp-config.php配置文件;
  5. 新增使用uri模块,用于代替手工Web端安装操作,实现wordpress安装完全自动化;

具体代码:

---
- hosts: 192.168.26.21
  gather_facts: True

  vars:
    wp_db_hostname: localhost
    wp_db_name: wordpressdb
    wp_db_user: wordpress
    wp_db_password: wordpress

  tasks:
    # common tasks
    - name: Install epel-release
      yum: 
        name: epel-release
        state: present 

    - name: Temporary Disable SELinux
      command: setenforce 0

    - name: Ensure SELinux is set to disabled mode
      lineinfile:
        path: /etc/selinux/config
        regexp: '^SELINUX='
        line: SELINUX=disabled

    # Install apache server
    - name: Install httpd
      yum:
        name: httpd
        state: present
      
    - name: Start httpd
      service:
        name: httpd
        state: started
        enabled: True

    - name: Permit 80/tcp port
      firewalld:
        service: http
        permanent: True
        immediate: True
        state: enabled

    #Install php82
    - name: Install epel-release-latest-7
      yum:
        name: https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
        state: present
    
    - name: Install remirepo
      yum:
        name: https://rpms.remirepo.net/enterprise/remi-release-7.rpm
        state: present
      
    - name: Enable remi-php82 repo
      shell: yum-config-manager --enable remi-php82

    - name: Install php82
      yum:
        name:
          - php82-php
          - php82-php-mbstring
          - php82-php-mysqlnd
          - php82-php-gd
          - php82-php-xml
        state: present
      notify:
        - Restart httpd

    - name: Install mariadb-server
      yum:
        name: mariadb-server
        state: present
      
    - name: Start mariadb-server
      service:
        name: mariadb
        state: started
        enabled: True
    
    - name: Install pip
      yum:
        name: python2-pip
        state: present

    - name: Install Python Library
      pip:
        name: PyMySQL==0.10.0
        state: present

    - name: Create Database
      mysql_db:
        name: "{{ wp_db_name }}"
        state: present

    - name: Create database user
      mysql_user:
        name: "{{ wp_db_user }}"
        password: "{{ wp_db_password }}"
        priv: '{{ wp_db_name }}.*:ALL'
        state: present

    # Install wordpress
    - name: Install wordpress
      get_url:
        url: https://wordpress.org/wordpress-6.2.tar.gz
        dest: /tmp/wordpress-6.2.tar.gz
        checksum: md5:34f279efe623025641bc11a69e3c02fa

    - name: Extract wordpress-6.X.tar.gz into /var/www/wordpress
      unarchive:
        src: /tmp/wordpress-6.2.tar.gz
        dest: /var/www/
        remote_src: True

    - name: Recursive Modify file permission
      file:
        path: /var/www/wordpress
        owner: apache
        group: apache
        mode: '0766'
        recurse: True
        state: directory
      
    - name: Add apache virtual host config
      copy:
        src: wordpress.conf
        dest: /etc/httpd/conf.d/wordpress.conf
      notify:
        - Restart httpd

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

    - name: Fetch random salts for WordPress config
      local_action: command curl https://api.wordpress.org/secret-key/1.1/salt/
      register: "wp_salt"
      become: no

    - name: Add wordpress config
      template:
        src: templates/wp-config.php.j2
        dest: /var/www/wordpress/wp-config.php
        owner: apache
        group: apache
        mode: '0744'

    - name: Install WordPress
      uri:
        url: "http://192.168.26.21/wp-admin/install.php?step=2"
        method: POST
        body_format: form-urlencoded
        body: 
          weblog_title: HelloWorld
          user_name: tony
          admin_password: e!sGma&jmC#ernVBQV
          admin_password2: e!sGma&jmC#ernVBQV
          admin_email: lixm@lixm.cc
          Submit: Install WordPress
        status_code: 200
      register: result

    - name: Print result
      debug:
        msg: "{{ result }}"

  handlers:
    - name: Restart httpd
      service:
        name: httpd
        state: restarted

模板文件:/templates/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:
 *
 * * Database settings
 * * Secret keys
 * * Database table prefix
 * * ABSPATH
 *
 * @link https://wordpress.org/documentation/article/editing-wp-config-php/
 *
 * @package WordPress
 */

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

/** Database username */
define( 'DB_USER', '{{ wp_db_user }}' );

/** Database password */
define( 'DB_PASSWORD', '{{ wp_db_password }}' );

/** Database hostname */
define( 'DB_HOST', '{{ wp_db_hostname }}' );

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

/** 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
 */
{{ wp_salt.stdout }}

/**#@-*/

/**
 * 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/documentation/article/debugging-in-wordpress/
 */
define( 'WP_DEBUG', false );

/* Add any custom values between this line and the "stop editing" line. */



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

Apache配置文件:

<VirtualHost _default_:80>
  DocumentRoot /var/www/wordpress/
  ServerName www.example.com
  
  <Directory /var/www/wordpress/>
    Require all granted
    AllowOverride All
    Options FollowSymLinks MultiViews

    <IfModule mod_dav.c>
      Dav off
    </IfModule>
  </Directory>
  
</VirtualHost>

标签: none

评论已关闭