Linux Server Management Assignment 4 – SaltStack Pillar Data

H4 SaltStack Pillar Data

Assignment given by Tero Karvinen

a) Opiskele yllä aikataulussa olevat artikkelit. Noissa artikkeleissa opetetaan ne asiat, joilla läksyt saa tehtyä. Tätä lukutehtävää ei tarvitse raportoida. Luettava materiaali on kunkin tapaamiskerran kohdalla.

b) Tee kahdella orjalla esimerkki, jossa orjat saavat eri muuttujan pilarista. Tarkista ‘pillars.items’, että kummalekin orjalle mene eri tieto. Tee jokin muu kuin tunnilla tehty sshd-esimerkki.

c) Tee kahdella orjalla esimerkki, jossa toinen orja saa muuttujan pilarista ja toinen käyttää oletusarvoa (pillar.get). Tee jokin muu kuin tunnilla tehty sshd-esimerkki.


Testing Environment

For this assignment I used Laptop Acer 5439 and using Ubuntu 17.10


a) Reading stuff: Karvinen 2018: Secrets in Salt Pillars. Official Salt Get Started: SaltStack Configuration Management: Pillar. Official Salt Documentation: Pillar Walkthrough


Setting up minions using Vagrant

We went through on how to use and set up vagrant virtual computers so for this assignments I am going to use vagrant minions.

$ sudo apt-get update

I haven’t opened or used this computer so I had so much stuff to upgrade.

$ sudo apt-get upgrade

$ sudo apt-get -y install vagrant virtualbox

Creating many machines

My teacher, Tero Karvinen made articles on how to create Vagrant machines easily and also how to provision multiple minions easily.

Multiple Virtual Computers in Minutes – Vagrant Multimachine

And link for provision multiple minions easily.

Provision Multiple Virtual Puppet Slaves with Vagrant

Also found an article on my teacher’s site that could also help me on this assignments.

Faster ‘vagrant up’ with apt/deb Package Proxy

I made a folder to keep everything clean and organized.

$ mkdir virtual_machines
$ cd virtual_machines/

So I started by creating an Vagrantfile that would create two minions for me and set the master to be my server (as it has salt-master installed there).

So this is what my Vagrantfile looked:

$tscript = <<TSCRIPT
apt-get update
apt-get install -y salt-minion
echo ‘master: ip-address’|sudo tee /etc/salt/minion
systemctl restart salt-minion.service
TSCRIPT

Vagrant.configure(2) do |config|
config.vm.box = “bento/ubuntu-16.04”
config.vm.provision “shell”, inline: $tscript

config.vm.define “minion1” do |minion1|
minion1.vm.hostname = “minion1”
end

config.vm.define “minion2” do |minion2|
minion2.vm.hostname = “minion2″ls
end
end

Then I had to create this minions and it is as simple as

$ vagrant up

After a while it will create me two new virtual machines that will be my minions.

master$ sudo salt-key
Accepted Keys:
512mb
Denied Keys:
vagrant.vm
Unaccepted Keys:
vagrant.vm
Rejected Keys:

Now it created my minions but it gave them the same id (vagrant.vm) so I need to change them so this would work.

$ vagrant ssh minion1

$ sudoedit /etc/salt/minion
$ cat /etc/salt/minion
master: ip-address
id: vagrantminion1
6 sudo systemctl restart salt-minion.service
7 exit

Then the second minion

$ vagrant ssh minion2

$ sudoedit /etc/salt/minion
$ cat /etc/salt/minion
master: ip-address
id: vagrantminion2
6 sudo systemctl restart salt-minion.service
7 exit

And now on the master I could see both of them, so now I had to accept them to be my minions.

master$ sudo salt-key
Accepted Keys:
512mb
Denied Keys:
Unaccepted Keys:
vagrantminion1
vagrantminion2
Rejected Keys:

master$ sudo salt-key -A
The following keys are going to be accepted:
Unaccepted Keys:
vagrantminion1
vagrantminion2
Proceed? [n/Y] y
Key for minion vagrantminion1 accepted.
Key for minion vagrantminion2 accepted.

master$ sudo salt-key
Accepted Keys:
512mb
vagrantminion1
vagrantminion2
Denied Keys:
Unaccepted Keys:
Rejected Keys:

Then I checked that were they following my commands with a simple command.

master$ sudo salt ‘*’ test.ping
512mb:
True
vagrantminion1:
True
vagrantminion2:
True

They answered me so now I could continue to do the assignments.

When I am finish I can destroy my vagrant and vagrant minions.

$ vagrant destroy


b) Simple Pillar 

Because this is pretty new to me so I am going to do for this assignment a very simple file that will contain pillar data and give it to one of my minion. I want to understand what I am going!

First I had to create pillar folder in the /srv/  directory because it stores all the pillar data there.

master$ sudo mkdir pillar

$ cd /srv/pillar

So now I created a simple file that will give different word to one of the minion and others will get the default.

$ sudoedit top.sls

$ cat top.sls
base:
‘vagrantminion1’:
– default

$ sudoedit default.sls

$ cat default.sls
editor: vim

Then I had to refresh to data to be applied.

$ sudo salt ‘*’ saltutil.refresh_pillar

$ sudo salt ‘*’ saltutil.refresh_pillar
512mb:
True
vagrantminion1:
True
vagrantminion2:
True

Then after this I could check that it would applied to my minions.

$ sudo salt ‘*’ pillar.items

$ sudo salt ‘*’ pillar.items
512mb:
———-
vagrantminion1:
———-
editor:
vim
vagrantminion2:
———-

It applied to my minions so off to next assignment!


c) Salt State and Pillar data

Was thinking long what would I do for this assignment but as I don’t understand this fully so I am going to do just a simple file like that other minion will get other value and other minion will get the default value. The simple thing is like a hello world kinda thing.

Started by making a two file. File that has a value and an ini.sls file that will run this file.

master$ cd /srv/salt/

$sudo mkdir pillardata && cd pillardata

$ head -100 /srv/salt/pillardata/*
==> /srv/salt/pillardata/defaultvalue.txt <==
You are my {{ minion }}

==> /srv/salt/pillardata/init.sls <==
/tmp/defaultvalue.txt:
file.managed:
– source: salt://pillardata/defaultvalue.txt
– template: jinja
– context:
minion: {{ pillar.get(‘minion’,’minions and you have to do what I command’) }}

Then I had to make and modify a file in the pillar directory.

$ cd /srv/pillar

$ head -100 /srv/pillar/*
==> /srv/pillar/minion1.sls <==
minion: my minion number 1 and you dont have to do anything

==> /srv/pillar/top.sls <==
base:
‘vagrantminion1’:
– minion1

In the /srv/pillar/top.sls file I told to what minion will get the default value with pillar.get. So now I had to check that everything works.

master$ sudo salt ‘*’ state.highstate –state-output terse512mb:
Name: states – Function: no.None – Result: Failed

Summary for vagrantminion2
————
Succeeded: 1 (changed=1)
Failed: 0
————
Total states run: 1
vagrantminion1:
Name: /tmp/defaultvalue.txt – Function: file.managed – Result: Changed

Summary for vagrantminion1
————
Succeeded: 1 (changed=1)
Failed: 0
————
Total states run: 1

So it change something. Lastly I had to check that those files were created on my minions. I could go and check one at a time but there is a faster way. On my master I could run cmd command to check that the command was successful.

First I checked with the pillad.items.

$ sudo salt ‘*’ pillar.items
vagrantminion1:
———-
minion:
my minion number 1 and you dont have to do anything
vagrantminion2:
———-

So it gave to vagranmininion1 some text. Then I ran the cmd command.

$ sudo salt ‘*’ cmd.run ‘cat /tmp/defaultvalue.txt’
vagrantminion2:
You are my minions and you have to do what I command
vagrantminion1:
You are my my minion number 1 and you dont have to do anything

I was successful and now I think that I know how salt state pillar data works! I am going to play with this more and when I think of something complex I will update here!


 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s