Getting started with SaltStack

Setup

SaltMaster

Ubuntu 18.04 server on digital ocean
1 core 1gig ram

SaltMinion

Ubuntu 18.04 digital ocean
Linux mint 19 Ryzen 1600, gtx 1060
First update everything runing

sudo apt update && sudo apt ugrade -y

Creating Salt Master

sudo apt-get -y install salt-master
hostname -I

Creating Salt Minion

First install the salt minion and edit the minion file to ad your master ip and slave id

sudo apt-get -y install salt-minion
sudoedit /etc/salt/minion

Add this

master: 46.101.160.215
id: minion1

Somehow my virtual mashine was not connecting to the master even it was done right. So I made my own pc runing linux mint 19 to a minion. I also made a local slave on the same host as master.

Restart your salt-minion service

sudo systemctl restart salt-minion.service

Connecting slave

In master write and accept the new slaves.

sudo salt-key -A

Test your setup with

sudo salt '*' cmd.run 'whoami'


Im not sure what those errors are. But we have a connection.

Collecting Brains…

I mean Grains

The master can brainwas the slaves in releasing their secrets with

sudo salt '*' grains.items


For example it will show us the processor that digital ocean is running my virtual mashine.

Idempotnecy

Want to make more complex things than just single commands? With salt you can declare things like “I want that Firefox is installed”. No matter how many times you run the command it will always check if your wish is granted if it (Firefox is installed) it will not do anything to the minion. This is called Idempotency

First we need to make a new folder on the Saltmaster where to store the rules for the minions.

sudo mkdir -p /srv/salt/

Make a new hello.sls file these are always written in YAML.

sudoedit /srv/salt/hello.sls

Add inside to file this. It will put the file “hello.txt” to all your slaves temporary folder

/tmp/hello.txt:
  file.managed:
    - source: salt://hello.txt

Add a file called hello.txt in to the same folder as hello.sls and write inside what ever you like. I will put “See you at https:akseliratamo.fi”

sudoedit /srv/salt/hellotero.txt

To apply it to your slaves just run.

sudo salt '*' state.highstate

Apply these automatically

If want to run something to all minions by default just make a file named “top.sls”. All minions will periodically ask the master if everthing is as the top.sls says and will change themself accordingly.

Does firewall matter?

It does not. The beauty lies in salt being a “pull” design. Meaning that the minions will contact the master and not the other way around. So it dosent matter what ip or firewall the minion has. Only know domain or ip in the setup needs to be the master IP.

Soruces

http://terokarvinen.com/2018/aikataulu-%e2%80%93-palvelinten-hallinta-ict4tn022-3004-ti-ja-3002-to-%e2%80%93-loppukevat-2018-5p
http://terokarvinen.com/2018/salt-quickstart-salt-stack-master-and-slave-on-ubuntu-linux
http://terokarvinen.com/2018/salt-states-i-want-my-computers-like-this

Copyright GPLv2 2018

Practice exam

Exam practice

This is a practice run for next weeks final exam for the Linux server course.

The idea is to choose one of the old exams and i chose this one.

Lamp stack

We need to create 7 users with their own public_html with an index file. Here are the users. Jorma wants a own domain named sleep.example.com this we will make with an hostfile and sites-available .conf. Maija is the new sudo admin and as the boss she wants to choose her own username what will be maija.
Jorma Mähkylä jmahk gieP1ugh6eu sleep.example.com
Pekka Hurme phurm engooR1ocha
Ronaldo Smith rsmit IeyiexeeP2a
Håkan Petersson hpete iereiteeH7E
Einari Mikkonen emikk queej8EvahY
Einari Vähäkäähkä evaha quab9Xayei3
Eija Vähäkäähkä eijav moJ4zoogiem
Maija Virtanen maija AhB4Oo2nohy sudo

First we will edit the skeleton folder which is the base to all newly created users. We can add the public_html folder with the index.php file already inside in to the skeleton so all users get it automatically. The web page of each user needs to display to current user so we need to make some kind of script for that. Lets try getting it with php so first install LAMP stack. As we did in the last time.
So here are the commands in order with only tiny descriptions what they do.

Update fresh Linux, install and configure Apache

sudo apt update && sudo apt upgrade -y
sudo apt install apache2 -y
echo "Default"|sudo tee /var/www/html/index.html
sudo a2enmod userdir
sudo systemctl restart apache2
cd; mkdir public_html; cd public_html
echo 'User created Apache works!'> index.php

Lets create the first user to test Apache for normal users.

sudo adduser jmahk
cd; cd public_html; echo 'Php works if it says four > <?php print(2+2+"\n\n"); ?>'> index.php


Now we know it works so lets put that inside the skeleton and add in the index.php this part so it will show “Welcome” and the current user name.

<h1>Welcome
<?php
echo   get_current_user();
?>!
</h1>

Now we can create all the other users too with the “sudo adduser” command and remember to give Maija sudo privliges. With typing

sudo adduser maija sudo

Firewall and python script

Now we enable and configure firewall

sudo ufw allow 22/tcp && sudo ufw allow 80/tcp && sudo ufw allow 443/tcp
sudo ufw enable && sudo ufw status

#!/usr/local/bin/python3

print “Welcome back!”

Adding host name for jorma

 cd /etc/apache2/sites-available/
sudoedit jorma.conf
sudo a2ensite jorma.conf

Copy this inside jorma.conf

<VirtualHost *:80>
    ServerName sleep.example.com
    ServerAlias example.com
    DocumentRoot /home/jmahk/public_html/index.php
</VirtualHost>

Then we activate the site and edit the hosts file.

 sudo a2ensite jorma.conf
 systemctl reload apache2
 cd /etc/ ; sudoedit hosts

and add this 2 lines
138.68.70.235 example.com
138.68.70.235 sleep.example.com

Done

Kurssiguru-coding meta packet

I tried to do it with this guide but was unsuccessful it gave me an error
dh_installchangelogs: warning: debian/changelog(l2): found end of file where expected first heading
dh_installchangelogs: error: fatal error occurred while parsing debian/changelog
debian/rules:20: recipe for target ‘binary-indep’ failed
make: *** [binary-indep] Error 255
Error in the build process: exit status 2

Sources:

Course assignment http://terokarvinen.com/2018/aikataulu-%E2%80%93-linux-palvelimet-ict4tn021-3003-to-8-14-alkusyksy-2018p1-%E2%80%93-5-op
Chosen practice examhttp://terokarvinen.com/2017/arvioitava-laboratorioharjoitus-%E2%80%93-linux-palvelimet-ict4tn021-3-uusi-ops-alkukevaalla-2017-p1
My lamp stack blog. https://akseliratamo.fi/2018/09/25/how-to-install-lamp-stack/

PHP JavaScript Python coding first steps

It’s good to know at least the basics of coding in different languages and this week’s assignment we had to write Hello world in three different programming languages.

So, lets get started with JavaScript!

JavaScript is a popular language that runs the web. Now days it runs also android apps and even the web backend.

print("Hello World");

See that’s not that hard you just need to remember JavaScript always ends with Semicolon. (Don’t trust me I don’t know how to code.)

PHP

You may often hear that PHP is a mess because you can write almost anything in many multiple ways, so nothing is consistent. Still php is one of the most important languages it also runs large parts of the web.

<?php
print("Hello World");
?>

Python

Python is a very big language what you can use in almost anything. Web development, science programs, AI, Big data etc. And seems to be the polar opposite of PHP. IF you watch videos on YouTube about python its always stated that Python is a very strict language whit one way to write something. It has its advantages and disadvantages. Because of that its really easy to others to understand your code and vice versa.

print "Hello World"

As you see Python seems to have the most user-friendly syntax it has been designed for good readability but if you write anything longer you need to be careful with the indentations those matter a lot in Python. But you don’t need to play around with parenthesis as much than in other languages.

Sources

This was written with markdown. If you want to know how. https://akseliratamo.fi/2018/09/28/markdown-with-elementor-in-wordpress/
My school assignment. This was part “h6” http://terokarvinen.com/2018/aikataulu-%e2%80%93-linux-palvelimet-ict4tn021-3003-to-8-14-alkusyksy-2018p1-%e2%80%93-5-op#comment-24183