Search code examples
nginxvagrantsalt-project

Using Vagrant with SaltStack, how do I start nginx after providing custom configuration


I have a Vagrant boxset up to provision with salt. When I do a fresh vagrant up (after a vagrant destroy), nginx defaults to port 80 and the default welcome page, despite not being configured to. I can fix it by manually running sudo nginx -s reload inside the guest, but I would prefer not to use a manual workaround.

Here's my salt/roots/salt/nginx/init.sls file:

nginx:
  pkg:
    - installed

nginx run:
  service.running:
    - name: nginx
    - enable: True
    - watch:
      - file: /etc/nginx/nginx.conf
      - file: /etc/nginx/sites-available/dotmanca
  require:
    - file: /etc/nginx/sites-enabled/dotmanca
    - file: /etc/nginx/nginx.conf
    - pkg: nginx

/etc/nginx/nginx.conf:
  file:
    - managed
    - source: salt://nginx/nginx.conf
    - user: root
    - group: root
    - mode: 644

/etc/nginx/sites-available/dotmanca:
  file:
    - managed
    - source: salt://nginx/dotmanca.conf
    - user: root
    - group: root
    - mode: 644
  require:
    - pkg: nginx

/etc/nginx/sites-enabled/dotmanca:
  file.symlink:
    - target: /etc/nginx/sites-available/dotmanca
    - user: root
    - group: root
    - mode: 644
  require:
    - file: /etc/nginx/sites-available/dotmanca

/etc/nginx/sites-enabled/default:
  file.absent:
    - name: /etc/nginx/sites-enabled/default
  require:
    - pkg: nginx

The nxginx server is installed and runs properly after provisioning, and the configuration files show up in the correct location.

I need to either reload the config in nginx after my custom files get placed, or somehow hold off running the nginx service until the files are in place.


Solution

  • Apparently, I needed to know more about the require bits. I had them where states should go, and not under the states themselves.

    My file should have looked like this:

    nginx:
      pkg:
        - installed
    
    nginx_run:
      service.running:
        - name: nginx
        - enable: True
        - watch:
          - file: /etc/nginx/nginx.conf
          - file: /etc/nginx/sites-available/dotmanca
        - require:
          - file: /etc/nginx/sites-enabled/dotmanca
          - file: /etc/nginx/nginx.conf
          - pkg: nginx
    
    /etc/nginx/nginx.conf:
      file:
        - managed
        - source: salt://nginx/nginx.conf
        - user: root
        - group: root
        - mode: 644
    
    /etc/nginx/sites-available/dotmanca:
      file:
        - managed
        - source: salt://nginx/dotmanca.conf
        - user: root
        - group: root
        - mode: 644
        - require:
          - pkg: nginx
    
    /etc/nginx/sites-enabled/dotmanca:
      file.symlink:
        - target: /etc/nginx/sites-available/dotmanca
        - user: root
        - group: root
        - mode: 644
        - require:
          - file: /etc/nginx/sites-available/dotmanca
    
    /etc/nginx/sites-enabled/default:
      file.absent:
        - name: /etc/nginx/sites-enabled/default
        - require:
          - pkg: nginx