Comparison between Ansible and Puppet

Key differences between Ansible and Puppet:

  • No single master server. You can simply install ansible on any server and that can then act as the muster. In Puppet, that’s not possible because the puppetmasters url is hardcoded into each puppet agent’s puppet.conf file.
  • Orchestration is done via the “push” system rather than the “pull” system. I.e. you don’t need to log into ansible client and then trigger a ansible run.
  • The code  in a playbook is executed in the order that they are specified. Where that is not the case with puppet, where if you want you can specify ordering using the before/require metaparameters
  • Puppet is based on ruby. However Ansible is based on python, and you can write python code right into your ansible code. Python comes with a huge amount of

Golang – Organise your code using functions

Functions are a block of code that can call in your code. Functions and Packages are the 2 main ways used for organising code in Golang. You can feed parameters to functions and functions can also return values. You can call one function from inside another function.


Setup a Workspace for Go

Before you can start writing Golang code. You first need to set up a Workspace. A workspace is just a folder that will store all your Golang related stuff. In my case I’ll create a workspace called ‘go_project’:

$ mkdir /root/go_project

A workspace needs to have 3 top level folders called pkg, bin, and src:

$ mkdir /root/go_project/{bin,pkg,src}
$ tree /root/go_project
/root/go_project
├── bin
├── pkg
└── src

Next, you need to tell Go to use this workspace. That’s done by setting an environment variable called ‘GOPATH’. Let’s first see what GOPATH is currently set to:

$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/root/go"
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build207657744=/tmp/go-build -gno-record-gcc-switches"

I’m using bash, so I’ll set this using the .bash_profile approach.

You may want to have several workspaces for your various Go projects. If so, then you’ll need to


Installing Golang

Installing Golang is really easy, just following the instructions in the official guide. For example, for RHEL/CentOSm we would do something like:

golang_archive_filename=go1.10.3.linux-amd64.tar.gz
curl -o ${golang_archive_filename} https://dl.google.com/go/${golang_archive_filename}
tar -C /usr/local -xzf ${golang_archive_filename}
cd /usr/local/go/bin 
cp -rfp /usr/local/go/bin/* /usr/bin/

However here are a few other ways to install Golang:

RHEL/CentOS install tips

A quick way to install Golang is via yum:

yum install golang

But note that there’s a good chance the Go version yum installs could be at least several versions behind.

MacOS install tips

You can install golang using brew. With brew it’s as simple as:

$ brew install go

Then when new version of golang comes out, you can just do:

$ brew upgrade golang

Windows Install tips

You can install Golang using chocolatey:

c:\ choco install golang

Then when new version of golang comes out, you can just do:

c:\ choco upgrade															

Golang Tutorial

What is Golang?

Golang (aka G0) is a relatively new programming language Google that has taken the IT world by storm. It was released in 2009 and already some of the biggest applications are written in Go, including Docker, Terraform, and Kubernetes.

This is a series of bitesize tutorials that’ll help you get comfortable with using Golang. This course also requires you to have some experience with Linux, especially using the Bash terminal.

What makes Golang so great?

There’s a lot to like about Golang, here are some of the main reasons why Golang has become so popular:

  1. It’s a general purpose language – meaning that it can can be used for writing low level OS level components, which is usually done in C or C++. It’s also create for developing high level

Golang – A ‘hello world’ example

Once you’ve installed Golang and set up your workspace, you’re now ready to write your hello world go program. Your Go code has to be written in the src folder, and the file needs to have the ‘.go’ extension. So I created the following file:


$ cat hello.go
package main

// this is a comment. 
import "fmt"


func main() {
    fmt.Println("hello world")
} 

There are 2 types of Go projects you can create. One is a shared library project which is code that’s used as part of other Golang projects. The other is where the end product of a Golang project is a standalone executable binary.

The package main tells Golang that this project will be creating a standalone executable binary. Next, we declared a function called ‘main’. You


AWS Cost Explorer CLI demo – filtering costs by cost allocation tags

Here’s a quick example:

$ cat filter.json
{
  "Tags": {
    "Key": "role",
    "Values": ["webserver"]
  }
}

$ aws ce get-cost-and-usage --time-period Start=$(date +"%Y-%m-%d" --date="-24 hours"),End=$(date +"%Y-%m-%d") --granularity=DAILY  --metrics BlendedCost --filter file://filter.json

RESULTSBYTIME	True
TIMEPERIOD	2018-08-09	2018-08-08
BLENDEDCOST	22.30	USD

References:

http://blog.xi-group.com/2015/01/small-tip-how-to-use-aws-cli-filter-parameter/

https://medium.com/@nachomillangarcia/prometheus-alerts-for-aws-daily-costs-3374137d1371


S3 – Use IAM role to grant S3 access to ec2 instances

Not sure if this is article works (at least it works without setting up any bucket policies). Need to investigate further.

I recently discovered that you don’t need to set up S3 bucket policies in order to give an EC2 access to an s3 bucket (or folder). Say you want to copy files to an s3 bucket from an EC2 instance:

$ aws s3 cp /path/to/testfile.txt s3://s3-bucketname/path/to/s3-bucket-folder

Then you need to do the following:

1. attach an IAM role to your ec2 instance
2. create the following policy:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
															

RHCE – rough notes

page 4:

systemctl –type=service

list all running services:

systemctl –list-units –type=service

list all services, running and not running:

systemctl –list-units –type=service

page 6:

lists the units that this unit depends on:

systemctl list-dependencies UNIT

shows what other services depends on the following unit:
systemctl list-dependencies UNIT –reverse

this forcefully disables a unit, so that it doesn’t start up at boot time or manually started. this
is so to prevent accidentally starting a unit. e.g. don’t accidentally start chronyd if already using ntpd
systemctl mask unit

Opposite of umask:

systemctl umask unit

page 26

the following command is best practice because it is persistent:

nmcli dev dis device

and don’t use (because it isn’t persistent):

nmcli connection down device

page 27:

The following set’s static ip address 192.0.5.20/24 as well as default gateway address of 192.0.5.254:

nmcli con mod static-eth0 ipv4.addresses “192.0.5.20/24 192.0.5.254”

note: you can omit the default gateway address.


AWS CLI with jq examples

Let’s say I have an EC2 instance with the instance id of ‘i-08725957c043b6acd’ and it has the ec2 tag: env=preprod. To pull this info out using the aws cli and jq, we run:

$ aws ec2 describe-tags --output json --filters "Name=resource-id,Values=i-08725987c041b6acd" | jq --raw-output '.Tags[]|select(.Key=="env")|.Value'
preprod

Note, you can obtain the instance_id locally like this:

$ instance_id=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)