• Contact
  • Contact Us
  • Disclamer
  • Home 1
  • Home 2
  • Home 3
  • Privacy Policy
Friday, May 9, 2025
How VPS - How to use/setup VPS
  • Login
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon
No Result
View All Result
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon
No Result
View All Result
How VPS - How to use/setup VPS
No Result
View All Result
Home Operating System Linux

Using SaltStack With Pillars on Ubuntu 17.04

How VPS by How VPS
January 1, 2020
in Linux
0
0
SHARES
21
VIEWS
Share on FacebookShare on Twitter

Contents

  1. Requirements
  2. Setting up master
  3. Formula
  4. Pillar
  5. Deploying
  6. Confirming
  7. Conclusion
  8. Want to contribute?


While SaltStack is a great tool for running operations on many servers simultaneously, it also supports per-host defined configuration presets stored in an unique file which are further called “pillar”. In this guide, we will write a small SaltStack formula for installing Nginx and automating the deployment of virtual hosts using pillars.

Requirements

  • Vultr instance running Ubuntu 17.04 ( master )
  • Two Vultr instances running Ubuntu 17.04 ( minions )

Setting up master

If you’re just getting started and did not set up your SaltStack master yet, check out this article which explains how to get started with SaltStack.

Formula

We start with creating the folder for our formula which further holds the files required for SaltStack’s operations on our minions.

mkdir -p /srv/salt/nginx

After creating the folder, we can continue with writing up the init.sls file which is an essential part of every formula:

nginx:
  pkg:
    - installed

/etc/nginx/sites-enabled/virtualhosts.conf:
    file.managed:
        - source: salt://nginx/files/virtualhosts.jinja
        - template: jinja
        - user: root
        - group: root
        - mode: 655

  cmd:
    - run
    - names:
        - service nginx restart

As you have probably noticed, SaltStack uses the Jinja templating engine for generating dynamic configuration files. This provides a broad range of possibilities within our configuration template files, such as accessing native Python code or setting variables and similar. The next step consists of creating the template for our virtualhosts.conf file, whereas we need to create the folder first:

mkdir -p /srv/salt/nginx/files

Once the folder is created, we can continue with writing up the virtualhosts.jinja file:

{% for vhost in pillar['vhosts'] %}
server {
    listen {{ vhost['listenPort'] }};
    server_name {{ vhost['serverName'] }};
    root {{ vhost['documentRoot'] }};
    index {%- for index in vhost['indexFiles'] -%}{{ index }}{%- endfor -%};
    location / {
        try_files $uri $uri/ =404;
    }
}
{% endfor %}

Pillar

After creating the formula, we can go ahead and setup our Pillar base:

mkdir -p /srv/pillar/

Any references for hosts matching a particular pillar file are written into the top.sls file within the directory we just created. In our example, we assume that two minions exist with the hostname nginxwww1 and nginxwww2 whereas we will assign them both unique pillar files:

base:
  'id:nginxwww1':
    - match: grain
    - nginxwww1
  'id:nginxwww2':
    - match: grain
    - nginxwww2

Next, we will continue with creating a pillar file for nginxwww1 whereas we will create a vhost listening on port 81 for the domain example.com, the document root being /var/www/example_com/ and the index files being index.shtml:

vhosts:
    -
      listenPort: '81'
      serverName: 'example.com'
      documentRoot: '/var/www/example_com'
      indexFiles:
        - index.shtml

We will continue with creating a pillar file for nginxwww2 whereas we will create a vhost listening on port 82 for the domain example2.com, the document root being /var/www/example2_com/ and the index files being index.js and index.css:

vhosts:
    -
      listenPort: '82'
      serverName: 'example2.com'
      documentRoot: '/var/www/example2_com'
      indexFiles:
        - index.js
        - index.css

Note: Pillar files are stored within /srv/pillar/

Deploying

We can now complete our first-try with pillars by updating them on our minions:

salt '*' saltutil.refresh_pillar

Afterwards, we can run the SaltStack formula:

salt '*' state.sls nginx

Confirming

Once the SaltStack formula has been run and completed without any errors, we should be able to see nginx listening on port 81 on nginxwww1 and on port 82 on nginxwww2 as confirmation.

Conclusion

This rather lightweight but already powerful SaltStack formula can be extended further to add support for HTTPS, possibly with an integration of Let’s Encrypt, support for CGI or PHP backends and everything you can manually do as well. This guide should have given you a basic idea of SaltStack formulas and pillars used for the deployment of configuration files. Happy automating!

Want to contribute?

You could earn up to $300 by adding new articles

Submit your article
Suggest an update
Request an article
Previous Post

Stop DHCP From Changing resolv.conf

Next Post

How To Install and Configure Solr on Ubuntu 14.04

Next Post

How To Install and Configure Solr on Ubuntu 14.04

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

No Result
View All Result

Recent Post

Install Imagemagick on CentOS
CentOS

Install Imagemagick on CentOS

by How VPS
June 28, 2023
0

This is how I installed Imagemagick on a vanilla CentOS server Start off by installing the prerequisites yum install php-pear...

Read more
how to Check phpinfo

How to Check phpinfo of Hosting or VPS?

June 28, 2023
Failed to download metadata for repo 'appstream' on Centos 8

How to fix error: Failed to download metadata for repo ‘appstream’ on Centos 8

February 25, 2022
How to Fix MySQL Error "Plugin 'InnoDB' registration as a STORAGE ENGINE failed"?

How to Fix MySQL Error “Plugin ‘InnoDB’ registration as a STORAGE ENGINE failed”?

November 17, 2020
How to optimize Mysql or MariaDB

How to optimize Mysql or MariaDB

November 3, 2020

Recent News

  • Install Imagemagick on CentOS
  • How to Check phpinfo of Hosting or VPS?
  • How to fix error: Failed to download metadata for repo ‘appstream’ on Centos 8

Category

  • Arch
  • Authentication
  • Backups
  • BSD
  • Centmin Mod
  • CentOS
  • Control Panels
  • CoreOS
  • CWP
  • Debian
  • Directadmin
  • Encryption
  • Fedora
  • Firewalls
  • Hocvps Script
  • Hosting providers
  • Kloxo-MR
  • Linux
  • Mitigations
  • Operating System
  • Plesk
  • Reviews
  • Securing VPS/Servers
  • Security Patches
  • SSL Certificates
  • Uncategorized
  • Upgrading
  • VPS/Servers management guides
  • Vulnerability Detection
  • Web servers software
  • Webhosting Control Panel
  • About
  • Advertise
  • Careers
  • Contact

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

No Result
View All Result
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
Thabet