SALT: SIMPLE LAMP-SERVER INSTALLATION

A state to install LAMP-server is done as follows:
Capture2

This is a very basic installation, nothing fancy, nothing extra. Just installation of the packages. The location of the state should be /srv/salt

I ran the state:

Capture

Works.

  • Copying, modifying and redistributing this and all the other documents in this blog is allowed according to the GNU General Public License (versio 2 or newer).http://www.gnu.org/licenses/gpl.html
  • Based on the Linux course by Tero Karvinen 2015: terokarvinen.com

 

SALT – CLONE YOUR GIT REPO IN A STATE — PYTHON DEV SETUP

GOAL

  • Create a state to be utilised in your working environment
  • Clone git repo!

PERQUISITES:

SaltStack installed on the machine.
https://juhosyrjanen.com/2015/11/05/salty-assignment-i-saltstack/

ENVIRONMENT:

VM1: Running Mint 17.2, Salt master ||  specs

DEV SETUP

I wanted to create a highstate that first installs Git, clones my repository and installs some Python libraries.

GIT

I created this ‘not-so-well-executed’ state to clone my repository for my current project.
State located in /srv/salt/git

git

Running commands in Salt state is not recommended, but this does do the job. My repository is open, therefore there’s no need for passwords in this execution.

I tested this state, it ran fine the first time, but ran into problems using cmd.run.

repo

Instead of using cmd.run, I used cmd.wait, which will only modify directories/scripts if changes are made.

gitfix

Running the state a few times, and all is good.

state

I also added a few line to the git-state to automate the login, so that there’s no need to always enter password on git push.

confgit

 

PYTHON

Another module for the Python installation, done in a similar way to the git-module.
Capture2

Before proceeding, both modules were tested to be working correctly.

Now in the top.sls -file, I added the modules to be set to one of the slaves I have running.
Capture4

Capture3

And then I ran the highstate.
sudo salt slaveVM01 state.highstate

Capture5

It worked.

I created this highstate in order to more easily manage my laptop and other computers I use for development purposes. It will be a lot more easier to run a few states than always manually install python libraries and clone a git repo.

 

  • Copying, modifying and redistributing this and all the other documents in this blog is allowed according to the GNU General Public License (versio 2 or newer).http://www.gnu.org/licenses/gpl.html
  • Based on the Linux course by Tero Karvinen 2015: terokarvinen.com

 

SALTSTACK: CUSTOMISE WORKSTATIONS

GOAL

  • With Salt:
    • change motd (message of the day)
    • change desktop background
    • create state installing essential software

PERQUISITES:

SaltStack installed on the machines.
https://juhosyrjanen.com/2015/11/05/salty-assignment-i-saltstack/

ENVIRONMENT:

Three virtual machines.
VM1: Running Mint 17.2, Salt master || specs
VM2: Running Mint 17.2, Salt minion
VM3: Running Mint 17.2, Salt minion

MOTD

Message of the day can be easily changed by editing the /etc/motd -file.

To do this, I created a new module for Salt, init.sls -file for the module and also a new file to be used as the new motd.
I decided to use a template engine to do this, hence the naming of the file.
Capture1

The init.sls file is build as follows:

#!pyobjects

File.managed("/etc/motd",source="salt://motd/motd.jinja",template="jinja")

I tested that the state runs normally.

Capture2

No errors. Next I modified the motd.jinja -file to create a customised message with the IP of the host using Salt grains.

Motd.jinja is built as follows:
Capture3

I ran the state again and then used ssh to connect to my local machine.

Capture4

Motd has been successfully changed.

BACKGROUND

Changing desktop wallpaper on is done rather simply by overriding the default background binary file.

I created a state for this before and it can be found in a previous article. 

ESSENTIAL SOFTWARE

Installing essential software is done easily with a state using Pkg.installed.  You can just create a new state and list wanted software with Pkg.installed(“nameofsoftware”). For example, I created this state with a list of some software I would use.

Capture5

If you install software that requires configuration prior to use, you can do that with File.managed – and edit the conf files. In the way shown above with motd.

I ran the state locally. It can as well be deployed to slaves using salt ‘*’.

Capture6

It took a while to process..

Capture7 Capture8

Done, and my software is here too.

+ FIREFOX SETTINGS

– – Work in progress

 

 

  • Copying, modifying and redistributing this and all the other documents in this blog is allowed according to the GNU General Public License (versio 2 or newer).http://www.gnu.org/licenses/gpl.html
  • Based on the Linux course by Tero Karvinen 2015: terokarvinen.com

SALT: CHANGE XUBUNTU DEFAULT BACKGROUND WITH SALTSTACK

In Xubuntu and Ubuntu changing the default background is relatively simple. All you need to do is locate the default background file (a binary file) and replace it with the wanted background.

In Xubuntu 14.04, the file is located in
/usr/share/xfce4/backdrops/
And goes by the name of xubuntu-wallpaper.png

In Salt, the easiest way to replace the default background is simply to override it with a state.

First, create a sls. -file for the state. I called mine background/ init.sls. Next you will want to dump your new background into the module folder.

Selection_003

The init.sls -file’s contents are the following:
(I used #!pyobjects to override the file)

#!pyobjects

File.managed("/usr/share/xfce4/backdrops/xubuntu-wallpaper.png", source="salt://background/img.png")

This piece of code will override the default wallpaper. After relogging the background should be updated to your chosen wallpaper.

Running the state:

Selection_004

  • Copying, modifying and redistributing this and all the other documents in this blog is allowed according to the GNU General Public License (versio 2 or newer).http://www.gnu.org/licenses/gpl.html
  • Based on the Linux course by Tero Karvinen 2015: terokarvinen.com

SALTSTACK: MASTER & SLAVES

GOAL:

Setup Salt master and minion -architecture.
Create a module for the minions.

PERQUISITES:

SaltStack installed on the machines.
https://juhosyrjanen.com/2015/11/05/salty-assignment-i-saltstack/

ENVIRONMENT:

Three virtual machines.
VM1: Running Mint 17.2, Salt master || specs
VM2: Running Mint 17.2, Salt minion
VM3: Running Mint 17.2, Salt minion

SETTING UP SALT MASTER AND MINIONS

I started setting up my master and minions following the walk-through in doc.saltstack. 

First I started the salt master -service.

sudo service salt-master start

Capture6

For the minion – master needs to be defined. So I added my master VMs IP to the minions /etc/salt/minion -file.
Capture7

I started the minion with

sudo salt-minion -d

This will generate a minion id, which will be stored in /etc/salt/ as shown in the pictures bellow

Capture8 Capture9

Now when I ran sudo salt-key -L on the master VM:
Capture10

I accepted the pending key with

sudo salt-key -A

And ran the sudo salt-key -L again:
Capture11

Capture12

I verified that the keys do match by running salt-key -F master on the master VM and salt-call key.finger --local on the minion VM.
Capture13Capture14

I verified that the master can ping the minion by running the test.ping command:
Capture15

The master can now start sending command to the minion! Installation successful!

I repeated the steps for the other VM and ran the test command again:
Capture1

MODULE

I created a top.sls file, in which I set states to be installed to my slaves. The file looks a little something like this:

top2

 

In the top.sls -file, I set Apache -state to be installed on all the slaves and specified the other packages to certain individual slaves.

I tested that all my states work on my master VM:

MySQL:
mysql

Apache:
apa

SSH:
ssh

Running the top file

All the individual states worked fine, so I ran the top file!
sudo salt '*' state.highstate

Salt took a while to run the file, but it seemed to have worked without any errors.
result1

result2

result3

Success

I ran the highstate again to confirm the results, and everything seemed to have worked fine.

  • Copying, modifying and redistributing this and all the other documents in this blog is allowed according to the GNU General Public License (version 2 or newer).http://www.gnu.org/licenses/gpl.html
  • Based on the Linux course by Tero Karvinen 2015: terokarvinen.com

SALT: PILLARS & GRAINS

GOAL:

Use Salt pillars & grains.
Setup Salt master – minion

Time elapsed: approx 3h.

SETUP:

Three virtual machines.
VM1: Running Mint 17.2, Salt master || specs
VM2: Running Mint 17.2, Salt minion
VM3: Running Mint 17.2, Salt minion

All VMs are set to bridged connections.

INSTALLING SALT

VM1

This is the same machine I used in the previous article. Salt is already installed. Here’s how the machine is configured: last article

I only uninstalled the Salt minion from this machine using the command:

sudo apt-get remove salt-minion

 

VM1 & VM2

For the slaves, I installed Salt using the same steps as used in the last article:

First I imported the SaltStack repository key

wget https://repo.saltstack.com/apt/ubuntu/ubuntu14/latest/SALTSTACK-GPG-KEY.pub | sudoapt-key add SALTSTACK-GPG-KEY.pub

Opened the sources list

sudoedit /etc/apt/sources.list

I Added the following line to the list:

deb http://repo.saltstack.com/apt/ubuntu/ubuntu14/latest trusty main

Next I ran apt-get update

sudo apt-get update

Installed only minion and SSH -components for the minions.

sudo apt-get install salt-minion salt-ssh

And there, installation completed!

Before proceeding I checked the installation status by running

$ sudo salt-call --version

Capture5

Capture4

SETTING UP SALT MASTER AND MINIONS

I started setting up my master and minions following the walk-through in doc.saltstack. 

First I started the salt master -service.

sudo service salt-master start

Capture6

For the minion – master needs to be defined. So I added my master VMs IP to the minions /etc/salt/minion -file.
Capture7

I started the minion with

sudo salt-minion -d

This will generate a minion id, which will be stored in /etc/salt/ as shown in the pictures bellow

Capture8 Capture9

Now when I ran sudo salt-key -L on the master VM:
Capture10

I accepted the pending key with

sudo salt-key -A

And ran the sudo salt-key -L again:
Capture11

Capture12

I verified that the keys do match by running salt-key -F master on the master VM and salt-call key.finger --local on the minion VM.
Capture13Capture14

I verified that the master can ping the minion by running the test.ping command:
Capture15

Yay! The master can now start sending command to the minion! Installation successful!

Grains

I want to make my slaves run a website that states their basic info, such as IP, hostname, role.. etc. To test this out I will do this on the VM1. I will create a new state, that will install Apache and define a template engine for the website. In the template, I will create a simple webpage, that will fetch the grains.

First I begin with checking what kind on grains I can actually utilize on my minion machines.

$ sudo salt-call --local grains.items

This command returns a huge list of grains that can be utilized. I’m interested in the “host” and “ip4_interfaces” -grains.
Capture1

To make the state, I first created a new folder for it and called it “status”

$ sudo mkdir /srv/salt/status

Inside the folder I created a new sls. -file and called it “init.sls”.

$ sudoedit init.sls

First I used #!pyobjects to install Apache and define the template engine:

#!pyobjects

with Pkg.installed("apache2")
File.managed("/var/www/html/index.html",
source="salt://status/index.html.jinja", template="jinja")

I tested if my state works:

$ sudo salt-call --local state.sls status

Capture2

Next I wrote the template file.

 

Hostname: {{ grains['host']}}
IP: {{ grains['ip4_interfaces']}
MAC: {{ grains['hwaddr_interfaces']}}

Then, I ran the state again and opened http://localhost in Firefox.

Capture3

Worked as expected, with minor formatting flaws.

 

PILLARS

I created a basic pillar following the doc.saltstack walk-through.

So first I created a directory for the pillars in /srv/

sudo mkdir /srv/pillar

And in the pillar-directory I created the top.sls -file.

sudoedit /srv/pillar/top.sls

And in the top.sls file, I just created a very simple file to test the pillar.

base:
'*':
- info

After this I created the info.sls -file and added some data into it.

info: testing pillar

Now that the test pillar files are created, I tested them out using these commands:

sudo salt '*' saltutils.refresh_pillar

sudo salt '*' pillar.items

Getting these results:
Capture16

Basic pillar test completed, worked without problems.

  • Copying, modifying and redistributing this and all the other documents in this blog is allowed according to the GNU General Public License (versio 2 or newer).http://www.gnu.org/licenses/gpl.html
  • Based on the Linux course by Tero Karvinen 2015: terokarvinen.com

SALTY ASSIGNMENT I: SALTSTACK

ASSIGNMENT:

a) Install SaltStack
b) Create a Package-File-Service state for Salt Stack

Assignment based on the course “Linuxin keskitetty hallinta” by Tero Karvinen

PLATFORM

  • Linux Mint 17.2 running on VirtualBox

GETTING SALTY – INSTALLATION

Firstly after booting the virtual machine, I started by running the basic

sudo apt-get update
sudo apt-get upgrade

To install SaltStack, I followed the instructions on docs.saltstack, found here

SaltStack requires repo.saltstack to be added to the sources.list. I proceeded with the instructions by running these commands:

First I imported the SaltStack repository key

wget -O - https://repo.saltstack.com/apt/ubuntu/ubuntu14/latest/SALTSTACK-GPG-KEY.pub | sudo apt-key add

Capture1

Next I added the repo to sources.list:

sudo nano /etc/apt/sources.list

I Added the following line to the list:

deb http://repo.saltstack.com/apt/ubuntu/ubuntu14/latest trusty main

Next I ran

sudo apt-get update

And then install the SaltStack components wanted! I added the master, minion and ssh components.

sudo apt-get install salt-master salt-minion salt-ssh

And there, installation completed!

Before proceeding I checked the installation status by running

 
$ sudo salt-call --version 

Capture2


PACKAGE-FILE-SERVICE

I created a DHCP-service using Salt and PyObjects:

First I created the .sls -file, that is used to run the state.  I created a directory for all the future Salt stuff in which I also created the sls. -file.

 
sudo mkdir -p /srv/salt
sudoedit /srv/salt/dhcp.sls

Documentation for the use of PyObjects in SaltStack states can be found here – docs.saltstack.
I used the article by Tero Karvinen as refenrence.

To use PyObjects in the sls. -file I first defined the shebang as

 #!pyobjects 

After the  So I added a line to the state-file:

 
with Pkg.installed("bind9"): 
        Service.running("bind9", enable=True)

Since dhcp-service will need bind9 to work properly, it will be installed first. I tested the line above and it worked like a charm.

Capture4

Added dhcp install command:

 
with Pkg.installed("bind9"): 
        Service.running("bind9", enable=True)

with Pkg.installed("isc-dhcp-server"):
        Service.running("isc-dhcp-server", enable=True)

After this, I ran the sls. file again with the command:

 
$ sudo salt-call --state-output=mixed --local state.sls dhcp

Capture5

It worked. This is a very basic installation of dhcp-service. Configuration has not been specified and has to be done by hand when using this implementation.

Later I will post a more detailed guide on how to modify the configuration files in Salt inside the state- file.

  • Copying, modifying and redistributing this and all the other documents in this blog is allowed according to the GNU General Public License (versio 2 or newer).http://www.gnu.org/licenses/gpl.html
  • Based on the Linux course by Tero Karvinen 2015: terokarvinen.com