Ansible Provisioning with Vagrant
Vagrant integrates with Ansible to provision OEL 8 VMs using playbooks. Instead of shell scripts, you write clean, idempotent Ansible playbooks that install and configure MySQL, Oracle, and ProxySQL on your VMs automatically during vagrant up.
Two Ansible Provisioner Modes
| Mode | How it works | Requirement |
|---|---|---|
| ansible | Ansible runs on HOST machine — connects to VM via SSH | Ansible installed on HOST |
| ansible_local | Ansible runs INSIDE the VM | Ansible installed inside VM (auto-installed) |
Mode 1 — ansible (runs on host)
Ruby — Ansible Host Mode
Vagrant.configure("2") do |config|
config.vm.box = "generic/oracle8"
config.vm.network "private_network", ip: "192.168.56.10"
config.vm.provision "ansible" do |ansible|
ansible.playbook = "ansible/site.yml"
ansible.inventory_path = "ansible/inventory"
ansible.limit = "all"
ansible.verbose = "v"
# Extra variables
ansible.extra_vars = {
mysql_root_password: "Root@123!",
mysql_version: "8.0"
}
end
end
Mode 2 — ansible_local (runs inside VM)
Ruby — Ansible Local Mode
Vagrant.configure("2") do |config|
config.vm.box = "generic/oracle8"
config.vm.network "private_network", ip: "192.168.56.10"
# Share ansible folder into VM
config.vm.synced_folder "./ansible", "/vagrant/ansible"
config.vm.provision "ansible_local" do |ansible|
ansible.playbook = "ansible/site.yml"
ansible.install = true # Auto-install Ansible in VM
ansible.install_mode = "pip3" # Use pip to install Ansible
ansible.version = "latest"
ansible.extra_vars = {
mysql_root_password: "Root@123!"
}
end
end
Ansible Inventory for Multi-Machine
INI — Ansible Inventory
# ansible/inventory
[db_masters]
master ansible_host=192.168.56.11 ansible_user=vagrant ansible_private_key_file=.vagrant/machines/master/virtualbox/private_key
[db_replicas]
replica1 ansible_host=192.168.56.12 ansible_user=vagrant ansible_private_key_file=.vagrant/machines/replica1/virtualbox/private_key
replica2 ansible_host=192.168.56.13 ansible_user=vagrant ansible_private_key_file=.vagrant/machines/replica2/virtualbox/private_key
[proxysql]
proxysql ansible_host=192.168.56.10 ansible_user=vagrant ansible_private_key_file=.vagrant/machines/proxysql/virtualbox/private_key
[all:vars]
ansible_python_interpreter=/usr/bin/python3
MySQL Playbook for OEL 8
YAML — MySQL Playbook
# ansible/site.yml
---
- name: Install MySQL 8 on OEL 8
hosts: all
become: yes
vars:
mysql_root_password: "Root@123!"
mysql_version: "8.0"
tasks:
- name: Import MySQL GPG key
rpm_key:
state: present
key: https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
- name: Install MySQL repository
dnf:
name: https://dev.mysql.com/get/mysql80-community-release-el8-9.noarch.rpm
state: present
disable_gpg_check: yes
- name: Disable default MySQL module
command: dnf module disable -y mysql
changed_when: false
- name: Install MySQL server
dnf:
name: mysql-community-server
state: present
- name: Start and enable MySQL
systemd:
name: mysqld
state: started
enabled: yes
- name: Get temporary MySQL root password
shell: grep "temporary password" /var/log/mysqld.log | awk "{print $NF}"
register: temp_password
changed_when: false
- name: Set MySQL root password
shell: |
mysql --connect-expired-password -u root -p"{{ temp_password.stdout }}" -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '{{ mysql_root_password }}';"
ignore_errors: yes
- name: Configure MySQL root remote access
mysql_user:
name: root
host: "%"
password: "{{ mysql_root_password }}"
priv: "*.*:ALL,GRANT"
state: present
login_user: root
login_password: "{{ mysql_root_password }}"
Multi-Machine Ansible Provisioning
Ruby — Multi-Machine Ansible
Vagrant.configure("2") do |config|
config.vm.box = "generic/oracle8"
config.vm.define "master" do |node|
node.vm.network "private_network", ip: "192.168.56.11"
node.vm.provision "ansible" do |ansible|
ansible.playbook = "ansible/mysql_master.yml"
ansible.extra_vars = { server_id: 1, role: "master" }
end
end
config.vm.define "replica1" do |node|
node.vm.network "private_network", ip: "192.168.56.12"
node.vm.provision "ansible" do |ansible|
ansible.playbook = "ansible/mysql_replica.yml"
ansible.extra_vars = {
server_id: 2,
role: "replica",
master_ip: "192.168.56.11"
}
end
end
end
💡 Note: Use ansible_local when you do not have Ansible installed on your host machine (e.g. Windows) — Vagrant will automatically install Ansible inside the VM and run the playbook from there.