0

Conditions

  • Vagrant 2.0.1
  • Vagrant Box: ubuntu/xenial64
  • Provisioning: ansible

Vagrantfile

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

    config.vm.box = "ubuntu/xenial64"
    config.vm.box_download_insecure = true

    config.vm.define "foobar"
    config.vm.provider "virtualbox" do |provider|
        provider.name = "foobar"
        provider.customize [ "modifyvm", :id, "--uartmode1", "disconnected" ]
    end

    config.vm.network "private_network", ip: "192.168.5.4"
    config.vm.hostname = "foobar.dev"
    config.hostsupdater.aliases = ["pma.foobar.dev","readis.foobar.dev"]

    config.vm.synced_folder ".", "/vagrant", type: "nfs"

    config.vm.provision "Installing 'vagrant'", type: "ansible_local" do |provisioner|
        provisioner.playbook = "/vagrant/env/ansible/install-vagrant.yml"
        provisioner.inventory_path = "/vagrant/env/ansible/inv/integration/hosts"
        provisioner.limit = "localhost"
    end

end

Problem

As one can see I have 3 nginx hosts running on my guest. I symlinked these 3 separate configs (enabled-sites) from the guest system into my host system with ansible which will be equal to

sudo ln -s /vagrant/path/to/dist.conf /etc/nginx/sites-enabled/dist.conf

When I first vagrant up my Box the sites are reachable, because at the end of the provisioning I restart the nginx service. But as soon I halt the box and up it again, the sites are not reachable, but the nginx service is running. I can then restart the nginx service on the shell and everything works fine then.

Reason

I found out that at the time the nginx service starts the synced /vagrant folder has not been mounted. The nginx service still starts properly regardless if the symlinked configs can be resolved or not.

Question

I could setup a provision shell script running always which restarts the nginx service on every vagrant up. But this is not the solution I'm looking for.

Is there a way to handle a trigger in the guest system as soon the /vagrant folder has been mounted? I can imagine there's some sort of xyz.d folder I can put scripts inside which will be executed with all necessary arguments to identify the vagrant mount.


Edit (2018-04-25)

I don't want to rely on Vagrant's mechanisms like plugins or further provisioning scripts. I recently changed from provisioning shell scripts to Ansible while I want to use the same provisioning for Docker or other deployment mechanisms. And I could imagine the same problem with the mount in Docker.


Edit (2018-04-27)

Launching services after Vagrant mount

I found this article of @razius having exactly the same problem. The solution is not up to date and I got the hint to look for a systemd equivalent of his solution. But I'm not familiar with it. So may one can help me with.

codekandis
  • 712
  • 1
  • 11
  • 22

1 Answers1

0

https://stackoverflow.com/a/38559856/2323764

I found a solution linked above but I had to make modifications due systemd standards and recommendations.

  • systemd unit changes does not belong into /lib/systemd while these changes can be overwritten by package and / or distribution updates at any time. Changes of units has to be made in copies of the units in /etc/systemd.
  • copy
    /lib/systemd/system/nginx.service
    to
    /etc/systemd/system/nginx.service
  • change the setting WantedBy in the section [Install] to vagrant.mount
  • Be aware that the name vagrant.mount depends on the path mounted by vagrant into the box. If you mount vagrant/somepath the mount is named vagrant-somepath.mount.
  • Run the commands as follows to rewrite the unit's targets files and other necessary dependencies:
    systemctl daemon-reload
    systemctl disable nginx.service
    systemctl enable nginx.service

This leads to my ansible handler as follows:

- name: Stopping the service 'nginx.service'
  listen: "restart service 'nginx.service'"
  systemd:
    name: "nginx.service"
    state: stopped

- name: Copying the service unit 'nginx.service'
  listen: "restart service 'nginx.service'"
  copy:
    src: "/lib/systemd/system/nginx.service"
    dest: "/etc/systemd/system/nginx.service"
    force: yes

- name: Configuring the service unit 'nginx.service' dependency to 'vagrant.mount'
  listen: "restart service 'nginx.service'"
  replace:
    path: "/etc/systemd/system/nginx.service"
    regexp: "^WantedBy=(.*)$"
    replace: "WantedBy=vagrant.mount"

- name: Running 'daemon-reload'
  listen: "restart service 'nginx.service'"
  systemd:
    daemon-reload: yes

- name: Disabling service 'nginx.service'
  listen: "restart service 'nginx.service'"
  systemd:
    name: "nginx.service"
    enabled: no

- name: Enabling service 'nginx.service'
  listen: "restart service 'nginx.service'"
  systemd:
    name: "nginx.service"
    enabled: yes

- name: Starting service 'nginx.service'
  listen: "restart service 'nginx.service'"
  systemd:
    name: "nginx.service"
    state: started
codekandis
  • 712
  • 1
  • 11
  • 22