Categories
Containers

Kubernetes Nginx

kubectl create deployment nginx --image=nginx:1.10.0
kubectl expose deployment nginx --port=80 --target-port=80 \
--type=LoadBalancer
Categories
Python

Regex Extract Python

import re
s = """
... some line abc
... some other line
... name my_user_name is valid
... some more lines"""
p = re.compile("name (.*) is valid")
result = p.search(s)
print(result.group(1))
Categories
Web Server

Change Hosts File

Linux and Mac

sudo nano /etc/hosts
127.0.0.1   localhost
YOUR_SERVER_IP yourdomain.com

Windows

Open c:\Windows\System32\Drivers\etc\hosts ( right click, run as administrator )

127.0.0.1   localhost
YOUR_SERVER_IP yourdomain.com
Categories
Containers

Docker bash integration

Example Dockerfile

FROM ubuntu:bionic-20200219

ARG TERRAFORM_VERSION="0.12.23"
# TODO: currnet ubuntu ansible version 2.5.1 update to use variable
# TODO: reduce size of image is pip or binary tarball better
ARG ANSIBLE_VERSION="2.5.1"
ARG PACKER_VERSION="1.5.4"
ARG AWSCLI_VERSION="1.18.19"

LABEL maintainer="Praison <contact@example.com>"
LABEL terraform_version=${TERRAFORM_VERSION}
LABEL ansible_version=${ANSIBLE_VERSION}
LABEL aws_cli_version=${AWSCLI_VERSION}

ENV DEBIAN_FRONTEND=noninteractive
ENV AWSCLI_VERSION=${AWSCLI_VERSION}
ENV TERRAFORM_VERSION=${TERRAFORM_VERSION}
ENV PACKER_VERSION=${PACKER_VERSION}
RUN apt-get update \
    && apt-get install -y ansible curl python3 python3-pip python3-boto unzip  \
    && pip3 install --upgrade awscli==${AWSCLI_VERSION} \
    && curl -LO https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip \
    && curl -LO https://releases.hashicorp.com/packer/${PACKER_VERSION}/packer_${PACKER_VERSION}_linux_amd64.zip \
    && unzip '*.zip' -d /usr/local/bin \
    && rm *.zip

RUN apt-get clean && \
    rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

CMD    ["/bin/bash"]

Building Docker Image

docker build -t praison/ansible-packer-terraform .

Examples getting versions

docker run --rm praison/ansible-packer-terraform terraform -version
docker run --rm praison/ansible-packer-terraform packer -version
docker run --rm praison/ansible-packer-terraform ansible --version
docker run --rm praison/ansible-packer-terraform aws --version

Bash Integration

Add it to

.bashrc or .bash_profile

It also maps your .ssh and .aws folders to share keys and configs that the command might need.

pansible () 
{
  docker run --rm \
             -w /opt \
             -v $(pwd):/opt/ \
             -v ~/.aws:/root/.aws \
             -v ~/.ssh:/root/.ssh \
             praison/ansible-packer-terraform ansible $@
}

paws () 
{
  docker run --rm \
             -w /opt \
             -v $(pwd):/opt/ \
             -v ~/.aws:/root/.aws \
             -v ~/.ssh:/root/.ssh \
             praison/ansible-packer-terraform aws $@
}

ppacker () 
{
  docker run --rm \
             -w /opt \
             -v $(pwd):/opt/ \
             -v ~/.aws:/root/.aws \
             -v ~/.ssh:/root/.ssh \
             praison/ansible-packer-terraform packer $@
}

pterraform () 
{
  docker run --rm \
             -w /opt \
             -v $(pwd):/opt/ \
             -v ~/.aws:/root/.aws \
             -v ~/.ssh:/root/.ssh \
             praison/ansible-packer-terraform terraform $@
}

Example usage:

$ ppacker -version
1.5.4

$ pansible --version
ansible 2.5.1
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.17 (default, Nov  7 2019, 10:07:09) [GCC 7.4.0]
Categories
Testing

Infrastructure testing with Kitchen CI

Requirements

gem install kitchen-ansible
gem install kitchen-vagrant
gem install kitchen-inspec

Commands

$ kitchen create

$ kitchen converge

$ kitchen setup

$ kitchen verify

$ kitchen destroy

Initiate Kitchen

Inside a directory

kitchen init --driver=vagrant --provisioner=ansible_playbook --create-gemfile

Ansible Playbook

playbook.yml

---

- name: Test Role
  hosts: test-kitchen
  user: vagrant
  roles:
    - nginx

roles/nginx/tasks/main.yml

---
- name: install Nginx
  yum: pkg={{ item }} state=installed
  with_items:
    - nginx

- name: ensure nginx is running automatically at boot time
  service: name=nginx state=restarted enabled=yes
$ cat hosts
[test-kitchen]
localhost

Kitchen CI

Kitchen.yml

---
driver:
  name: vagrant

provisioner:
  name: ansible_playbook
  hosts: test-kitchen
  roles_path: roles
  require_ansible_repo: true
  ansible_verbose: true
  ansible_version: latest
  require_chef_for_busser: false
  playbook: playbook.yml

platforms:
  - name: centos-7.2

verifier:
  name: inspec
  sudo_path: true

suites:
  - name: default
    verifier:
      inspec_tests:
        - test/integration/default

Inspec

test/integration/default/default_spec.rb

describe package('nginx') do
  it { should be_installed }
end

describe service('nginx') do
  it { should be_enabled   }
  it { should be_running   }
end

describe port(80) do
  it { should be_listening }
end
kitchen list
kitchen create
kitchen converge
kitchen verify

To Destroy

kitchen destroy