Skip to content

Instantly share code, notes, and snippets.

@maxivak
Last active May 11, 2016 09:53
Show Gist options
  • Select an option

  • Save maxivak/4b16ce62a33c95b65de5255fcfae180d to your computer and use it in GitHub Desktop.

Select an option

Save maxivak/4b16ce62a33c95b65de5255fcfae180d to your computer and use it in GitHub Desktop.
Network in Vagrant

Vagrant Networking

Set static IP using Ansible provision

  • Vagrant file
HOSTNAME = 'debugserver'
IP = "192.168.0.100"
NETWORK_MASK = "255.0.0.0"
NETWORK_GATEWAY = "192.168.1.1"



Vagrant.configure(2) do |config|
  
  #config.vm.box = "ubuntu/xenial64"
  config.vm.box = "geerlingguy/ubuntu1604"
  
   IP
  config.vm.network "public_network", auto_config: false, :bridge => "eth0", ip: "#{IP}"


  config.vm.provision "ansible" do |p|
    p.playbook = "playbook.yml"
    p.verbose        = true
    p.extra_vars =
        {
            machine: "main",
            public_ip: IP,
            public_mask: NETWORK_MASK,
            public_gateway: NETWORK_GATEWAY,
        }
  end

  
end


  • playbook.yml
---
- hosts: all
  sudo: true

  tasks:
# base
#    - hostname: name={{hostname}}
    - lineinfile: dest=/etc/hosts regexp='ubuntu-xenial' line='127.0.0.1 ubuntu-xenial'       state=present
    - lineinfile: dest=/etc/hosts regexp='{{hostname}}' line='127.0.0.1 {{hostname}}' state=present
    - shell: sudo bash -c 'hostnamectl set-hostname {{hostname}}'


# eth
    - name: stop eth1
      shell: ifdown eth1
      ignore_errors: yes

    - name: set networking
      template: src="files/interfaces/eth1.cfg.j2" dest="/etc/network/interfaces.d/eth1.cfg"

    - name: start interface
      #shell: ifup eth1 && ifdown eth1 && ifup eth1
      shell: ifup eth1
      ignore_errors: yes

    - name: restart interface
      shell: sudo ifconfig eth1 up && sudo ifconfig eth1 down && sudo ifconfig eth1 up


  • eth1.cfg
# files/interfaces/eth1.cfg.j2

# The primary network interface
auto eth1
iface eth1 inet static
      address {{public_ip}} 
      netmask {{public_mask}}
      {% if public_gateway is defined %}gateway {{public_gateway}} {% endif %}



Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment