Skip to content

Commit

Permalink
use default settings if user doesn't exist in hash
Browse files Browse the repository at this point in the history
for Vagrant and Ansible. Rename vars to simplify understanding
  • Loading branch information
nqb committed Nov 24, 2021
1 parent 3cb8cfa commit 24da6c3
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 109 deletions.
54 changes: 30 additions & 24 deletions addons/vagrant/cumulus/Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,24 @@ SCRIPT

# Read YAML file with box and network details
inventory = YAML.load_file('inventory/hosts')
current_user = ENV['USER']
networks = inventory['all']['vars']['networks'][current_user]
switch01_ip = networks['vms']['switch01']['ip']
switch01_netmask = networks['vms']['switch01']['netmask']
inline_ip = networks['vms']['switch01']['inline_ip']
inline_netmask = networks['vms']['switch01']['inline_netmask']
inline_l3_ip = networks['vms']['switch01']['inline_l3_ip']
inline_l3_netmask = networks['vms']['switch01']['inline_l3_netmask']
node01_ip = networks['vms']['node01']['ip']
node01_netmask = networks['vms']['node01']['netmask']
node02_ip = networks['vms']['node02']['ip']
node02_netmask = networks['vms']['node02']['netmask']
user = ENV['USER']
users_vars = inventory['all']['vars']['users_vars']
if users_vars[user]
dict_name = user
else
dict_name = 'gitlab-runner'
end
user_vars = inventory['all']['vars']['users_vars'][dict_name]
switch01_ip = user_vars['vms']['switch01']['ip']
switch01_netmask = user_vars['vms']['switch01']['netmask']
inline_ip = user_vars['vms']['switch01']['inline_ip']
inline_netmask = user_vars['vms']['switch01']['inline_netmask']
inline_l3_ip = user_vars['vms']['switch01']['inline_l3_ip']
inline_l3_netmask = user_vars['vms']['switch01']['inline_l3_netmask']
node01_ip = user_vars['vms']['node01']['ip']
node01_netmask = user_vars['vms']['node01']['netmask']
node02_ip = user_vars['vms']['node02']['ip']
node02_netmask = user_vars['vms']['node02']['netmask']

Vagrant.configure("2") do |config|

Expand All @@ -124,34 +130,34 @@ Vagrant.configure("2") do |config|
# link for swp1 --> mgmt_network (vlan 17)
device.vm.network "private_network",
:mac => "a0:00:00:00:00:01",
:libvirt__network_name => networks['networks'][0]['name'],
:ip => networks['networks'][0]['subnet'],
:libvirt__network_name => user_vars['networks'][0]['name'],
:ip => user_vars['networks'][0]['subnet'],
:libvirt__dhcp_enabled => false,
:libvirt__forward_mode => networks['networks'][0]['forward_mode'],
:libvirt__forward_mode => user_vars['networks'][0]['forward_mode'],
auto_config: false
# link for swp2 --> reg_network (vlan 2)
device.vm.network "private_network",
:mac => "44:38:39:00:00:02",
:libvirt__network_name => networks['networks'][1]['name'],
:ip => networks['networks'][1]['subnet'],
:libvirt__network_name => user_vars['networks'][1]['name'],
:ip => user_vars['networks'][1]['subnet'],
:libvirt__dhcp_enabled => false,
:libvirt__forward_mode => networks['networks'][1]['forward_mode'],
:libvirt__forward_mode => user_vars['networks'][1]['forward_mode'],
auto_config: false
# link for swp3 --> iso_network (vlan 3)
device.vm.network "private_network",
:mac => "44:38:39:00:00:03",
:libvirt__network_name => networks['networks'][2]['name'],
:ip => networks['networks'][2]['subnet'],
:libvirt__network_name => user_vars['networks'][2]['name'],
:ip => user_vars['networks'][2]['subnet'],
:libvirt__dhcp_enabled => false,
:libvirt__forward_mode => networks['networks'][2]['forward_mode'],
:libvirt__forward_mode => user_vars['networks'][2]['forward_mode'],
auto_config: false
# link for swp6 --> inline_network (vlan 6)
device.vm.network "private_network",
:mac => "44:38:39:00:00:06",
:libvirt__network_name => networks['networks'][3]['name'],
:ip => networks['networks'][3]['subnet'],
:libvirt__network_name => user_vars['networks'][3]['name'],
:ip => user_vars['networks'][3]['subnet'],
:libvirt__dhcp_enabled => false,
:libvirt__forward_mode => networks['networks'][3]['forward_mode'],
:libvirt__forward_mode => user_vars['networks'][3]['forward_mode'],
auto_config: false
# link for swp11 --> node01:ens6
device.vm.network "private_network",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ freeradius__configuration:
- name: 'packetfence_servers'
raw: |
client pf {
ipaddr = {{ networks[0]['subnet'] }}
ipaddr = {{ users_vars[dict_name]['networks'][1]['subnet'] }}
secret = testing123
}
state: 'present'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
---
tinyproxy__allow:
- "{{ networks[0]['subnet'] }}"
- "{{ users_vars[dict_name]['networks'][1]['subnet'] }}"
22 changes: 11 additions & 11 deletions addons/vagrant/inventory/group_vars/pfservers/venom_local_vars.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ venom_local_vars:

# get second IP (.2) usable without /CIDR
- name: 'configurator.interfaces.reg.ip'
value: "{{ networks[current_user]['networks'][1]['subnet'] | ansible.netcommon.next_nth_usable(2) | ansible.netcommon.ipaddr('address') }}"
value: "{{ users_vars[dict_name]['networks'][1]['subnet'] | ansible.netcommon.next_nth_usable(2) | ansible.netcommon.ipaddr('address') }}"

# get netmask based on CIDR
- name: 'configurator.interfaces.reg.netmask'
value: "{{ networks[current_user]['networks'][1]['subnet'] | ansible.netcommon.ipaddr('netmask') }}"
value: "{{ users_vars[dict_name]['networks'][1]['subnet'] | ansible.netcommon.ipaddr('netmask') }}"

# get second IP (.2) usable without /CIDR
- name: 'configurator.interfaces.iso.ip'
value: "{{ networks[current_user]['networks'][2]['subnet'] | ansible.netcommon.next_nth_usable(2) | ansible.netcommon.ipaddr('address') }}"
value: "{{ users_vars[dict_name]['networks'][2]['subnet'] | ansible.netcommon.next_nth_usable(2) | ansible.netcommon.ipaddr('address') }}"

# get netmask based on CIDR
- name: 'configurator.interfaces.iso.netmask'
value: "{{ networks[current_user]['networks'][2]['subnet'] | ansible.netcommon.ipaddr('netmask') }}"
value: "{{ users_vars[dict_name]['networks'][2]['subnet'] | ansible.netcommon.ipaddr('netmask') }}"

- name: 'smtp_server'
value: "{{ packetfence_install__mgmt_interface['ip'] }}"
Expand All @@ -45,22 +45,22 @@ venom_local_vars:
value: "{{ mariadb_socket }}"

- name: 'ad_mgmt_ip'
value: "{{ networks[current_user]['vms']['ad']['ip'] }}"
value: "{{ users_vars[dict_name]['vms']['ad']['ip'] }}"

- name: 'switch01_mgmt_ip'
value: "{{ networks[current_user]['vms']['switch01']['ip'] }}"
value: "{{ users_vars[dict_name]['vms']['switch01']['ip'] }}"

- name: 'node01_mgmt_ip'
value: "{{ networks[current_user]['vms']['node01']['ip'] }}"
value: "{{ users_vars[dict_name]['vms']['node01']['ip'] }}"

- name: 'node02_mgmt_ip'
value: "{{ networks[current_user]['vms']['node02']['ip'] }}"
value: "{{ users_vars[dict_name]['vms']['node02']['ip'] }}"

- name: 'wireless01_mgmt_ip'
value: "{{ networks[current_user]['vms']['wireless01']['ip'] }}"
value: "{{ users_vars[dict_name]['vms']['wireless01']['ip'] }}"

- name: 'linux01_mgmt_ip'
value: "{{ networks[current_user]['vms']['linux01']['ip'] }}"
value: "{{ users_vars[dict_name]['vms']['linux01']['ip'] }}"

- name: 'linux02_mgmt_ip'
value: "{{ networks[current_user]['vms']['linux02']['ip'] }}"
value: "{{ users_vars[dict_name]['vms']['linux02']['ip'] }}"
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---
# allow all machines on management network to send logs to rsyslog
rsyslog__group_allow:
- "{{ networks[current_user]['networks'][0]['subnet'] }}"
- "{{ users_vars[dict_name]['networks'][0]['subnet'] }}"
74 changes: 38 additions & 36 deletions addons/vagrant/inventory/hosts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ all:
cumulus:
hosts:
switch01:
mgmt_ip: "{{ networks[current_user]['vms']['switch01']['ip'] }}"
mgmt_netmask: "{{ networks[current_user]['vms']['switch01']['netmask'] }}"
mgmt_ip: "{{ users_vars[dict_name]['vms']['switch01']['ip'] }}"
mgmt_netmask: "{{ users_vars[dict_name]['vms']['switch01']['netmask'] }}"
box: CumulusCommunity/cumulus-vx
box_version: 3.7.12
ansible_host: "{{ mgmt_ip }}"
Expand All @@ -19,16 +19,16 @@ all:
node01:
box: debian/bullseye64
box_version: 11.20211018.1
mgmt_ip: "{{ networks[current_user]['vms']['node01']['ip'] }}"
mgmt_netmask: "{{ networks[current_user]['vms']['node01']['netmask'] }}"
mgmt_ip: "{{ users_vars[dict_name]['vms']['node01']['ip'] }}"
mgmt_netmask: "{{ users_vars[dict_name]['vms']['node01']['netmask'] }}"
ansible_host: "{{ mgmt_ip }}"
# only used when run outside Vagrant
ansible_python_interpreter: '/usr/bin/python3'
node02:
box: debian/bullseye64
box_version: 11.20211018.1
mgmt_ip: "{{ networks[current_user]['vms']['node02']['ip'] }}"
mgmt_netmask: "{{ networks[current_user]['vms']['node02']['netmask'] }}"
mgmt_ip: "{{ users_vars[dict_name]['vms']['node02']['ip'] }}"
mgmt_netmask: "{{ users_vars[dict_name]['vms']['node02']['netmask'] }}"
ansible_host: "{{ mgmt_ip }}"
# only used when run outside Vagrant
ansible_python_interpreter: '/usr/bin/python3'
Expand All @@ -38,8 +38,8 @@ all:
ad:
box: jborean93/WindowsServer2016
box_version: 0.7.0
mgmt_ip: "{{ networks[current_user]['vms']['ad']['ip'] }}"
mgmt_netmask: "{{ networks[current_user]['vms']['ad']['netmask'] }}"
mgmt_ip: "{{ users_vars[dict_name]['vms']['ad']['ip'] }}"
mgmt_netmask: "{{ users_vars[dict_name]['vms']['ad']['netmask'] }}"
ansible_host: "{{ mgmt_ip }}"
cpus: 2
memory: 2048
Expand All @@ -49,8 +49,8 @@ all:
wireless01:
box: debian/bullseye64
box_version: 11.20211018.1
mgmt_ip: "{{ networks[current_user]['vms']['wireless01']['ip'] }}"
mgmt_netmask: "{{ networks[current_user]['vms']['wireless01']['netmask'] }}"
mgmt_ip: "{{ users_vars[dict_name]['vms']['wireless01']['ip'] }}"
mgmt_netmask: "{{ users_vars[dict_name]['vms']['wireless01']['netmask'] }}"
ansible_host: "{{ mgmt_ip }}"
ansible_python_interpreter: '/usr/bin/python3'
cpus: 1
Expand All @@ -65,17 +65,17 @@ all:
linux01:
box: debian/bullseye64
box_version: 11.20211018.1
mgmt_ip: "{{ networks[current_user]['vms']['linux01']['ip'] }}"
mgmt_netmask: "{{ networks[current_user]['vms']['linux01']['netmask'] }}"
mgmt_ip: "{{ users_vars[dict_name]['vms']['linux01']['ip'] }}"
mgmt_netmask: "{{ users_vars[dict_name]['vms']['linux01']['netmask'] }}"
ansible_host: "{{ mgmt_ip }}"
ansible_python_interpreter: '/usr/bin/python3'
cpus: 1
memory: 512
linux02:
box: debian/bullseye64
box_version: 11.20211018.1
mgmt_ip: "{{ networks[current_user]['vms']['linux02']['ip'] }}"
mgmt_netmask: "{{ networks[current_user]['vms']['linux02']['netmask'] }}"
mgmt_ip: "{{ users_vars[dict_name]['vms']['linux02']['ip'] }}"
mgmt_netmask: "{{ users_vars[dict_name]['vms']['linux02']['netmask'] }}"
ansible_host: "{{ mgmt_ip }}"
ansible_python_interpreter: '/usr/bin/python3'
cpus: 1
Expand Down Expand Up @@ -104,33 +104,33 @@ all:
pfel8dev:
box: generic/rhel8
box_version: '3.4.2'
mgmt_ip: "{{ networks[current_user]['vms']['pfel8dev']['ip'] }}"
mgmt_netmask: "{{ networks[current_user]['vms']['pfel8dev']['netmask'] }}"
mgmt_ip: "{{ users_vars[dict_name]['vms']['pfel8dev']['ip'] }}"
mgmt_netmask: "{{ users_vars[dict_name]['vms']['pfel8dev']['netmask'] }}"
ansible_host: "{{ mgmt_ip }}"
cpus: 2
memory: 6144
pfdeb11dev:
box: debian/bullseye64
box_version: 11.20211018.1
mgmt_ip: "{{ networks[current_user]['vms']['pfdeb11dev']['ip'] }}"
mgmt_netmask: "{{ networks[current_user]['vms']['pfdeb11dev']['netmask'] }}"
mgmt_ip: "{{ users_vars[dict_name]['vms']['pfdeb11dev']['ip'] }}"
mgmt_netmask: "{{ users_vars[dict_name]['vms']['pfdeb11dev']['netmask'] }}"
ansible_host: "{{ mgmt_ip }}"
ansible_python_interpreter: '/usr/bin/python3'
cpus: 2
memory: 6144
el8dev:
box: generic/rhel8
box_version: '3.4.2'
mgmt_ip: "{{ networks[current_user]['vms']['el8dev']['ip'] }}"
mgmt_netmask: "{{ networks[current_user]['vms']['el8dev']['netmask'] }}"
mgmt_ip: "{{ users_vars[dict_name]['vms']['el8dev']['ip'] }}"
mgmt_netmask: "{{ users_vars[dict_name]['vms']['el8dev']['netmask'] }}"
ansible_host: "{{ mgmt_ip }}"
cpus: 2
memory: 6144
deb11dev:
box: debian/bullseye64
box_version: 11.20211018.1
mgmt_ip: "{{ networks[current_user]['vms']['deb11dev']['ip'] }}"
mgmt_netmask: "{{ networks[current_user]['vms']['deb11dev']['netmask'] }}"
mgmt_ip: "{{ users_vars[dict_name]['vms']['deb11dev']['ip'] }}"
mgmt_netmask: "{{ users_vars[dict_name]['vms']['deb11dev']['netmask'] }}"
ansible_host: "{{ mgmt_ip }}"
ansible_python_interpreter: '/usr/bin/python3'
cpus: 2
Expand All @@ -139,22 +139,22 @@ all:
localdev:
hosts:
localhost:
mgmt_ip: "{{ networks[current_user]['vms']['localhost']['ip'] }}"
mgmt_netmask: "{{ networks[current_user]['vms']['localhost']['netmask'] }}"
mgmt_ip: "{{ users_vars[dict_name]['vms']['localhost']['ip'] }}"
mgmt_netmask: "{{ users_vars[dict_name]['vms']['localhost']['netmask'] }}"
ansible_connection: local
pfel8localdev:
box: generic/rhel8
box_version: '3.4.2'
mgmt_ip: "{{ networks[current_user]['vms']['pfel8localdev']['ip'] }}"
mgmt_netmask: "{{ networks[current_user]['vms']['pfel8localdev']['netmask'] }}"
mgmt_ip: "{{ users_vars[dict_name]['vms']['pfel8localdev']['ip'] }}"
mgmt_netmask: "{{ users_vars[dict_name]['vms']['pfel8localdev']['netmask'] }}"
ansible_host: "{{ mgmt_ip }}"
cpus: 2
memory: 6144
pfdeb11localdev:
box: debian/bullseye64
box_version: 11.20211018.1
mgmt_ip: "{{ networks[current_user]['vms']['pfdeb11localdev']['ip'] }}"
mgmt_netmask: "{{ networks[current_user]['vms']['pfdeb11localdev']['netmask'] }}"
mgmt_ip: "{{ users_vars[dict_name]['vms']['pfdeb11localdev']['ip'] }}"
mgmt_netmask: "{{ users_vars[dict_name]['vms']['pfdeb11localdev']['netmask'] }}"
ansible_host: "{{ mgmt_ip }}"
ansible_python_interpreter: '/usr/bin/python3'
cpus: 2
Expand All @@ -165,25 +165,25 @@ all:
pfel8stable:
box: generic/rhel8
box_version: '3.4.2'
mgmt_ip: "{{ networks[current_user]['vms']['pfel8stable']['ip'] }}"
mgmt_netmask: "{{ networks[current_user]['vms']['pfel8stable']['netmask'] }}"
mgmt_ip: "{{ users_vars[dict_name]['vms']['pfel8stable']['ip'] }}"
mgmt_netmask: "{{ users_vars[dict_name]['vms']['pfel8stable']['netmask'] }}"
ansible_host: "{{ mgmt_ip }}"
cpus: 2
memory: 8192
# to test upgrades
pfdeb9stable:
box: inverse-inc/pfdeb9stable
box_version: 10.3.20210414165339
mgmt_ip: "{{ networks[current_user]['vms']['pfdeb9stable']['ip'] }}"
mgmt_netmask: "{{ networks[current_user]['vms']['pfdeb9stable']['netmask'] }}"
mgmt_ip: "{{ users_vars[dict_name]['vms']['pfdeb9stable']['ip'] }}"
mgmt_netmask: "{{ users_vars[dict_name]['vms']['pfdeb9stable']['netmask'] }}"
ansible_host: "{{ mgmt_ip }}"
cpus: 2
memory: 8192
pfdeb11stable:
box: debian/bullseye64
box_version: 11.20211018.1
mgmt_ip: "{{ networks[current_user]['vms']['pfdeb11stable']['ip'] }}"
mgmt_netmask: "{{ networks[current_user]['vms']['pfdeb11stable']['netmask'] }}"
mgmt_ip: "{{ users_vars[dict_name]['vms']['pfdeb11stable']['ip'] }}"
mgmt_netmask: "{{ users_vars[dict_name]['vms']['pfdeb11stable']['netmask'] }}"
ansible_host: "{{ mgmt_ip }}"
ansible_python_interpreter: '/usr/bin/python3'
cpus: 2
Expand All @@ -192,8 +192,10 @@ all:

vars:
tz: UTC
current_user: "{{ lookup('env', 'USER') }}"
networks:
user: "{{ lookup('env', 'USER') }}"
# if variable "user" doesn't exist in users_vars, we fallback to "gitlab-runner" has dict_name
dict_name: "{{ user if users_vars[user]|d() else 'gitlab-runner' }}"
users_vars:
# ci
gitlab-runner:
networks:
Expand Down
20 changes: 13 additions & 7 deletions addons/vagrant/linux_servers/Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@

# Require YAML module
require 'yaml'

# Read YAML file with box and network details
inventory = YAML.load_file('inventory/hosts')
current_user = ENV['USER']
networks = inventory['all']['vars']['networks'][current_user]
user = ENV['USER']
users_vars = inventory['all']['vars']['users_vars']
if users_vars[user]
dict_name = user
else
dict_name = 'gitlab-runner'
end
user_vars = inventory['all']['vars']['users_vars'][dict_name]

Vagrant.configure("2") do |config|
# loop on **all** host(s) in linux_servers group in inventory to create VM(s)
Expand All @@ -23,11 +29,11 @@ Vagrant.configure("2") do |config|
# only from our expected subnet. Allow traffic between guests. Deny
# all other inbound. Deny all other outbound.
srv.vm.network "private_network",
:libvirt__network_name => networks['networks'][0]['name'],
:ip => networks['vms'][server]['ip'],
:netmask => networks['vms'][server]['netmask'],
:libvirt__network_name => user_vars['networks'][0]['name'],
:ip => user_vars['vms'][server]['ip'],
:netmask => user_vars['vms'][server]['netmask'],
:libvirt__dhcp_enabled => false,
:libvirt__forward_mode => networks['networks'][0]['forward_mode']
:libvirt__forward_mode => user_vars['networks'][0]['forward_mode']

srv.vm.provider "libvirt" do |v|
v.cpus = details['cpus']
Expand Down
Loading

0 comments on commit 24da6c3

Please sign in to comment.