I’m trying to setup a mock environment of Vault and Consul clusters with Vagrant, and using Ansible to provision the environment. However, I’m unable only make the provision happen only once. I tried using flags but it seems the order of evaluation makes this not working as intended. Can someone explain? Thanks in advance.
Vagrantfile:
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
vault_up = false
consul_up = false
VUALT_NODE_COUNT = 3
CONSUL_NODE_COUNT = 5
# Define Vault Node
(1..VUALT_NODE_COUNT).each do |machine_id|
config.vm.define "vault_#{machine_id}" do |machine|
machine.vm.box = "generic/debian12"
machine.vm.hostname = "vault#{machine_id}"
machine.vm.provider :libvirt do |virt|
virt.memory = 1024
end
if machine_id == VUALT_NODE_COUNT
vault_up = true
end
end
end
# Define Consul Node
(1..CONSUL_NODE_COUNT).each do |machine_id|
config.vm.define "consul_#{machine_id}" do |machine|
machine.vm.box = "generic/debian12"
machine.vm.hostname = "consul#{machine_id}"
machine.vm.provider :libvirt do |virt|
virt.memory = 1024
end
if machine_id == CONSUL_NODE_COUNT
consul_up = true
end
end
end
puts "vault_up: #{vault_up}"
puts "consul_up: #{consul_up}"
if vault_up && consul_up
config.vm.provision :ansible do |ansible|
ansible.compatibility_mode = "2.0"
ansible.limit = "all"
ansible.playbook = "playbook.yml"
ansible.groups = {
"vault" => (1..VUALT_NODE_COUNT).map { |i| "vault_#{i}"},
"consul" => (1..CONSUL_NODE_COUNT).map { |i| "consul_#{i}"}
}
end
end
end
playbook.yml
---
- name: Common
hosts: all
tasks:
- name: Add Hashicorp Repo
block:
- name: download key
ansible.builtin.get_url:
url: https://apt.releases.hashicorp.com/gpg
dest: /usr/share/keyrings/hashicorp-archive.asc
- name: add repo
ansible.builtin.apt_repository:
repo: "deb [signed-by=/usr/share/keyrings/hashicorp-archive.asc] https://apt.releases.hashicorp.com {{ ansible_distribution_release }} main"
state: present
update_cache: no
- name: Update cache
become: yes
ansible.builtin.apt:
update_cache: true
cache_valid_time: 3600
- name: Install libnss-mdns and avahi-deamon
become: yes
ansible.builtin.apt:
pkg:
- libnss-mdns
- avahi-daemon
state: present
- name: Vault
hosts: vault
tasks:
- name: Install vault
become: yes
ansible.builtin.apt:
name: vault
- name: Consul
hosts: consul
tasks:
- name: Install consul
become: yes
ansible.builtin.apt:
name: consul