Working with Ansible {Part-13}

Working with Ansible {Part-13}

Project:

Installing WordPress a 2-tier application through Ansible

WordPress is a 2-tier application where we need to install a DB.

Here we will Take the same approach as we took in the last blog i.e. by using roles we will install the DB and WordPress application.

  • Create the directory as follows

  • Now, under main.yaml. Write the YAML for creating a DB

      ---
      - name: Installing mariadb
        yum: 
          name: "{{ item }}"
          state: latest
        with_items: ["MySQL-python","mariadb-server"]
    
      - name: enable mariadb service
        systemd:
          name: mariadb
          enabled: true
    
      - name: Start mariadb service
        systemd:
          name: mariadb
          state: started
    
      - name: Create a New Database with Name given by user
        mysql_db:
          name: "{{ DBNAME }}"
          state: present
    
      - name: Create Database user with name fiven by user and password given by user with all db privileges
        mysql_user:
          name: "{{ DBUSER }}"
          password: "{{ DBPASS }}"
          priv: '*.*:ALL'
          state: present
          host: '%'
    

  • Now, create a directory app1 and app1.yaml file in it

      ---
      - name: Installing Database
        hosts: Node-1
        vars:
          DBNAME: "devopswala"
          DBUSER: "admin"
          DBPASS: "admin123"
        roles:
          - db
    

  • Now run the playbook for app1.

  • Now, Go to Node-1 and verify that we can see the Maria DB installed along with DB name and username-password

  • Now under roles create a new directory named WordPress and under that create subdirectories named handlers, tasks and templates.

Now, under the templates directory create a file named wp-config.php and copy the following content

Copy Below Template
<?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 website, 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', 'database_name_here' ); / Database username / define( 'DB_USER', 'username_here' ); / Database password / define( 'DB_PASSWORD', 'password_here' ); / Database hostname / define( 'DB_HOST', 'localhost' ); / 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! / $tableprefix = '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';
//Copy this Template
<?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', 'database_name_here' );

/** Database username */
define( 'DB_USER', 'username_here' );

/** Database password */
define( 'DB_PASSWORD', 'password_here' );

/** Database hostname */
define( 'DB_HOST', 'localhost' );

/** 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/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';
  • Under the wp-config.php change the following values

  • Now, navigate to tasks create a file main.yaml and write the code as follows

      ---
      - name: Installing PHP
        command: amazon-linux-extras install -y lamp-mariadb10.2-php7.2 php7.2
    
      - name: Install Apache
        yum:
          name: httpd
          state: latest
      - name: enable httpd
        systemd:
          name: httpd
          enabled: true
      - name: download and untar wordpress
        unarchive:
          src: https://wordpress.org/latest.tar.gz
          dest: /var/www/html/
          remote_src: yes
          owner: apache
          group: apache
      - name: placing wp-config.php file
        template:
          src: wp-config.php
          dest: /var/www/html/wp-config.php
          owner: apache
          group: apache
        notify: restart apache
    

  • Now, under Handlers create another main.yaml as follows

      ---
      - name: restart apache
        systemd:
          name: httpd
          state: restarted
    

  • Let's see how the WordPress app files look like

  • Now, Let's come to our app1.yaml and call all these roles from there

      ---
      - name: Installing Database
        hosts: Node-1
        vars:
          DBNAME: "devopswala"
          DBUSER: "admin"
          DBPASS: "admin123"
        roles:
          - db
    
      - name: Installing Wordpress
        hosts: Node-2
        vars:
          DBNAME: "devopswala"
          DBUSER: "admin"
          DBPASS: "admin123"
          DBSERVER: "3.95.226.114"
        roles:
          - wordpress
    
  • Let's execute the playbook.

    Hence, We saw we could deploy a 2-tier application with the help of the Above YAML roles.

Did you find this article valuable?

Support DevOps and Cloud Computing by becoming a sponsor. Any amount is appreciated!