Vagrant OEL 8 DevOps · VirtualBox · Provisioning

VagrantShell Provisioning for OEL 8

Use shell provisioning to automatically configure OEL 8 VMs. Install MySQL 8, configure master/replica settings and run scripts during vagrant up.

Shell provisioning automatically runs shell scripts inside your OEL 8 VM during vagrant up. It is the simplest and most portable provisioning method — no extra tools required. You can use inline scripts for simple tasks or external script files for complex database setups.

Ruby — Inline Shell
Vagrant.configure("2") do |config|
  config.vm.box = "generic/oracle8"

  # Simple inline script
  config.vm.provision "shell", inline: <<-SHELL
    echo "=== Starting OEL 8 Setup ==="
    dnf update -y
    dnf install -y vim wget curl net-tools
    echo "=== Setup Complete ==="
  SHELL

end
Ruby — External Scripts
Vagrant.configure("2") do |config|
  config.vm.box = "generic/oracle8"

  # Run external script
  config.vm.provision "shell", path: "scripts/setup_oel8.sh"

  # Script with arguments
  config.vm.provision "shell",
    path: "scripts/install_mysql.sh",
    args: ["8.0", "root", "Password123!"]

  # Run as specific user (default is root)
  config.vm.provision "shell",
    path: "scripts/configure_app.sh",
    privileged: false   # runs as vagrant user

end
BASH — MySQL Install Script
#!/bin/bash
# scripts/install_mysql.sh
# Usage: ./install_mysql.sh [master|replica] [ip_address]

set -e
ROLE=${1:-master}
IP=${2:-192.168.56.11}
MYSQL_ROOT_PASSWORD="Root@123!"

echo "=== Installing MySQL 8.0 on OEL 8 ==="

# Add MySQL repository
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
dnf install -y https://dev.mysql.com/get/mysql80-community-release-el8-9.noarch.rpm
dnf module disable -y mysql
dnf install -y mysql-community-server

# Start MySQL
systemctl enable --now mysqld

# Get temporary root password
TEMP_PASS=$(grep "temporary password" /var/log/mysqld.log | awk "{print $NF}")

# Set new root password and configure
mysql --connect-expired-password -u root -p"$TEMP_PASS" << EOF
ALTER USER 'root'@'localhost' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}';
CREATE USER 'root'@'%' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
CREATE USER 'monitor'@'%' IDENTIFIED BY 'Monitor@123!';
GRANT SELECT, REPLICATION CLIENT ON *.* TO 'monitor'@'%';
FLUSH PRIVILEGES;
EOF

echo "=== MySQL 8.0 Installed Successfully ==="
echo "Root Password: ${MYSQL_ROOT_PASSWORD}"
echo "Role: ${ROLE}"
BASH — Master Config Script
#!/bin/bash
# scripts/configure_master.sh

SERVER_ID=${1:-1}
MYSQL_ROOT_PASSWORD="Root@123!"

# Configure Master my.cnf
cat > /etc/my.cnf.d/master.cnf << EOF
[mysqld]
server-id              = ${SERVER_ID}
log_bin                = /var/log/mysql/mysql-bin.log
binlog_format          = ROW
binlog_expire_logs_seconds = 604800
gtid_mode              = ON
enforce_gtid_consistency = ON
log_slave_updates      = ON
read_only              = OFF
innodb_buffer_pool_size = 1G
max_connections        = 500
EOF

systemctl restart mysqld

# Create replication user
mysql -u root -p"$MYSQL_ROOT_PASSWORD" << EOF
CREATE USER IF NOT EXISTS 'replicator'@'%' IDENTIFIED BY 'Repl@123!';
GRANT REPLICATION SLAVE ON *.* TO 'replicator'@'%';
FLUSH PRIVILEGES;
EOF

echo "=== Master Configured. Server ID: ${SERVER_ID} ==="
BASH — Replica Config Script
#!/bin/bash
# scripts/configure_replica.sh

SERVER_ID=${1:-2}
MASTER_IP=${2:-192.168.56.11}
MYSQL_ROOT_PASSWORD="Root@123!"

# Configure Replica my.cnf
cat > /etc/my.cnf.d/replica.cnf << EOF
[mysqld]
server-id              = ${SERVER_ID}
relay_log              = /var/log/mysql/relay-bin.log
log_bin                = /var/log/mysql/mysql-bin.log
binlog_format          = ROW
gtid_mode              = ON
enforce_gtid_consistency = ON
log_slave_updates      = ON
read_only              = ON
super_read_only        = ON
EOF

systemctl restart mysqld

# Configure replication
mysql -u root -p"$MYSQL_ROOT_PASSWORD" << EOF
CHANGE MASTER TO
  MASTER_HOST='${MASTER_IP}',
  MASTER_USER='replicator',
  MASTER_PASSWORD='Repl@123!',
  MASTER_AUTO_POSITION=1;
START SLAVE;
SHOW SLAVE STATUS\G
EOF

echo "=== Replica Configured. Server ID: ${SERVER_ID}, Master: ${MASTER_IP} ==="
Ruby — Run Modes
# Run only on first vagrant up (default)
config.vm.provision "shell", inline: "echo 'first boot only'", run: "once"

# Run every time vagrant up or vagrant reload
config.vm.provision "shell", inline: "echo 'always runs'", run: "always"

# Run only when explicitly called with vagrant provision
config.vm.provision "shell", inline: "echo 'manual only'", run: "never"
💡 Note: Scripts run as root by default (privileged: true). Set privileged: false to run as the vagrant user. Use environment variables with env: option to pass config into scripts.