Seite wählen

NETWAYS Blog

Ansible – Testing roles with Molecule

Ansible is a widely used and a powerful open-source configuration and deployment management tool. It can be used for simple repetitive daily tasks or complex application deployments, therefore Ansible is able to cover mostly any situation.

If used in complex or heterogene environments it is necessary to test the code to reduce time to fix code in production. To test Ansible code it is suggested to use Molecule.

Molecule is a useful tool to run automated tests on Ansible roles or collections. It helps with unit tests to ensure properly working code on different systems. Whether using the role internally or provide it to the public, it is useful to test many cases your role can be used. In addition Molecule is easily integrated into known CI/CD tools, like Github Actions or Gitlab CI/CD.

In this short introduction I’ll try get your first Molecule tests configured and running!

Please make sure you installed Molecule beforehand. On most distributions it’s easily installed via PIP.
The fastest and most common way to test roles would be in container. Due to a version problem with systemd currently it’s not possible to start services over systemd in containers. For this reason you can easily start with a vagrant instance and later migrate to docker or podman easily.


pip install molecule molecule-vagrant

If you have a role you can change into the role directory and create a default scenario.


cd ~/Documents/netways/git/thilo.my_config/
molecule init scenario -r thilo.my_config default
INFO     Initializing new scenario default...
INFO     Initialized scenario in /Users/thilo/Documents/netways/git/thilo.my_config/molecule/default successfully.

Below the molecule folder all scenarios are listed. Edit the default/molecule.yml to add the vagrant options.

Add a dependency file with your collections as with newer Ansible versions only the core is available. If needed you can add sudo privileges to your tests.

molecule/default/molecule.yml


---
dependency:
  name: galaxy
  options:
    requirements-file: collections.yml
driver:
  name: vagrant
platforms:
  - name: instance
    box: bento/centos-7
provisioner:
  name: ansible
verifier:
  name: testinfra
  options:
    sudo: true

The converge.yml is basically the playbook to run on your instance. In the playbook you define which variables should be used or if some pre-tasks should be run.

molecule/default/converge.yml


---
- name: Converge
  hosts: all
  become: true
  tasks:
    - name: "Include thilo.my_config"
      include_role:
        name: "thilo.my_config"

Now you can run your playbook with molecule. If you want to deploy and not delete your instance use converge. Otherwise you can use test, then the instance will be created, tested and destroyed afterwards.


python3 -m molecule converge -s default
or 
python3 -m molecule test -s default

Finally we can define some tests, the right tool is testinfra. Testinfra provides different modules to gather informations and check them if they have specific attributes.

Your scenario creates a tests folder with the following file: molecule/default/tests/test_default.py

In this example I’ll test the resources my role should create.


"""Role testing files using testinfra."""


def test_user(host):
    """Validate created user"""
    u = host.user("thilo")

    assert u.exists

def test_authorized_keys(host):
    """Validate pub key deployment"""
    f = host.file("/home/thilo/.ssh/authorized_keys")

    assert f.exists
    assert f.content_string == "ssh-rsa AAAA[...] \n"

And if we already converged our instance, we can verify these definitions against our deployment.


python3 -m molecule verify
INFO     default scenario test matrix: verify
INFO     Performing prerun with role_name_check=0...
[...]
INFO     Running default > verify
INFO     Executing Testinfra tests found in /Users/thilo/Documents/netways/git/thilo.my_config/molecule/default/tests/...
============================= test session starts ==============================
platform darwin -- Python 3.9.12, pytest-6.2.5, py-1.11.0, pluggy-0.13.1
rootdir: /
plugins: testinfra-6.4.0
collected 2 items

molecule/default/tests/test_default.py ..                                [100%]

============================== 2 passed in 1.79s ===============================
INFO     Verifier completed successfully.

With those easy steps you can easily test your roles for any scenario and your deployments can run without any hassle or at least you will be more relaxed during it 😉

Check out our Blog for more awesome posts and if you need help with Ansible send us a message or sign up for one of our trainings!

Thilo Wening
Thilo Wening
Manager Consulting

Thilo hat bei NETWAYS mit der Ausbildung zum Fachinformatiker, Schwerpunkt Systemadministration begonnen und unterstützt nun nach erfolgreich bestandener Prüfung tatkräftig die Kollegen im Consulting. In seiner Freizeit ist er athletisch in der Senkrechten unterwegs und stählt seine Muskeln beim Bouldern. Als richtiger Profi macht er das natürlich am liebsten in der Natur und geht nur noch in Ausnahmefällen in die Kletterhalle.

A short introduction to Kubernetes

Ever since I was working in a DevOps role I was very curious about Kubernetes, what better way is there to learn through creating a YouTube video? Let me guide through the process of creating the YouTube Tutorial!

 

How did I get started?

First off I tried to watch a few different YouTube videos myself, and then transitioned to writing a script for it.
Since I was familiar with OpenStack, it was easier to get into it by diving head first into the documentation.
Even though the Kubernetes Documentation might be too technical or not well explained in some parts, in combination with the youtubers and blog posts you can get a good overview of K8s.
I gathered all the information I could find about all the objects and features in Kubernetes, and tried to get a broad understanding on how everything works.

 

Let’s hit the record button!

Since then I have been trying to see how I could record it best and which information I was getting across and what I needed to change.
After redoing my script and selecting, what Kubernetes knowledge I’d like to convey, I was finally able to get more into doing a proper recording.
Check out our webinar room at NETWAYS HQ! I was really happy that we already have such a good lighting and recording setup!

Rinse and repeat.

I recorded with OBS and really had a fun time, trying to get everything right and present it in a nice way.
As soon as I got some proper recordings down I demuxed the files with OBS and went on to edit the footage.
First I sorted everything into Intro, Welcome, Objects, Features, Benefits of Kubernetes and how to get started folders.
So I could easily edit the footage before putting it together.
Then I went into Adobe Premiere and cut the clips short and removed the unnecessary audio tracks.
After that I put everything together, threw in the Intro and Outro.
I was almost done after that, just needed to denoise the audio tracks and find some music for the intro and outro!

 

It’s a wrap!

Then the whole thing was finished, yay!
Who would’ve thought, creating a 12-minute-video would need that many steps prior, before it is actually uploaded on the internet? 😀 It was exciting to create the Kubernetes YouTube Tutorial, but now it’s even more exciting having the video ready!
Here’s the result and I hope you have fun watching the video, and maybe it helps you to get into Kubernetes more easily now!

YouTube player

 

If you are curious about your own Managed Kubernetes environment, then our NWS website is a great source of information for that. Our MyEngineers are also always available to help you create your dream cluster.
Cheers and have a good one!

NETWAYS Webinare: Icinga 2 Serie

NETWAYS Webinare
Heute möchten wir euch darüber informieren, dass wir uns einige Gedanken über unsere beliebten Icinga 2 Webinare gemacht haben. Anders als bisher wollen wir nicht statisch auf einzelne Themen und Inhalte eingehen, sondern einen ganzheitlichen Eindruck der Möglichkeiten von Icinga 2 vermitteln.
In diesem Zuge werden wir gemeinsam in einer Reihe von Webinaren vom Grundaufbau des Monitorings, der Anbindung an Graphite und Grafana bis hin zum Windows Monitoring und der Integration von Rocket.Chat und Request Tracker für die Alarmierung.
Am Ende der Serie soll als Ziel eine fertige Icinga 2 Umgebung bereitstehen, welche vom Grundaufbau im Rahmen der Webinare erstellt wurde und künftig für weitere Themen bereitsteht.
Die geplanten Termine sind alle direkt auf unserer Webinar Seite zu finden. Eine kurze Übersicht möchten wir trotzdem bereitstellen:

Wir freuen uns wie immer auf eine rege Teilnahme!

Christian Stein
Christian Stein
Manager Sales

Christian kommt ursprünglich aus der Personalberatungsbranche, wo er aber schon immer auf den IT Bereich spezialisiert war. Bei NETWAYS arbeitet er als Manager Sales und berät unsere Kunden in der vertrieblichen Phase rund um das Thema Monitoring. Gemeinsam mit Georg hat er sich Mitte 2012 auch an unserem Hardware-Shop "vergangen".

Einbinden von NWS Icinga 2 Satelliten

Über unsere SaaS Plattform NWS bieten wir seit Beginn die Möglichkeit, Icinga 2 Satelliten Systeme zu betreiben und diese in die eigene Icinga 2 Monitoring Infrastruktur zu integrieren.
Dadurch wird ermöglicht, dass bspw. Webseiten oder andere externe Dienste (Mail, DNS, FTP, etc.) von außen überwacht werden können und die Ergebnisse in das Hausinterne Monitoring übertragen werden.
Hosted man bspw. einen Webshop wie wir (shop.netways.de), reicht es nicht nur zu überprüfen ob die MariaDB oder Apache Dienste funktionieren und die virtuelle Maschine in unserer Cloud verfügbar ist, sondern es ist auch wichtig die externe Sicht der Kunden zu überwachen. Somit erfährt man gleich ob eine externe Erreichbarkeit sichergestellt ist und Kunden bspw. einkaufen können.
In diesem Blogartikel möchte ich beschreiben, wie man den Icinga 2 Satelliten aufsetzt und per Icinga 2 Cluster Protokoll in das eigene Monitoring integriert.
Hat man sich in der NWS Plattform angemeldet, erreicht man über den oberen Reiter „Apps“ alle von uns angebotenen Produkte. Nachdem man einen Icinga 2 Satelliten gestartet hat, klickt man auf diese App und folgender Bildschirm erscheint:
mehr lesen…

Christian Stein
Christian Stein
Manager Sales

Christian kommt ursprünglich aus der Personalberatungsbranche, wo er aber schon immer auf den IT Bereich spezialisiert war. Bei NETWAYS arbeitet er als Manager Sales und berät unsere Kunden in der vertrieblichen Phase rund um das Thema Monitoring. Gemeinsam mit Georg hat er sich Mitte 2012 auch an unserem Hardware-Shop "vergangen".

Ja ist denn schon wieder Webinar Zeit?

Auch in diesem Jahr wollen wir natürlich wieder effektiv mit unseren Webinaren auf Neuheiten im Open Source Bereich, sei es Icinga 2, Ansible, uvm. oder auf Produktneuheiten in unserem Online-Store, wie bspw. die neue Braintower Firmware hinweisen.
Die folgenden Themen und Termine stehen Stand heute schon fest:

Titel Zeitraum Registrierung
Server-Überwachung mit Icinga 2 15. Februar 2017 – 10:30 Uhr Anmelden
Icinga Director: Neues vom Config Tool 02. März 2017 – 10:30 Uhr Anmelden
Braintower: Neuheiten in Firmware 3.5 16. März 2017 – 10:30 Uhr Anmelden
Icinga Web 2: Erstellen und verwalten von Business Prozessen 30. März 2017 – 10:30 Uhr Anmelden
AKCP SP2+: Die Neuheiten und Icinga 2 Integration 27. April 2017 – 10:30 Uhr Anmelden

Selbstverständlich werden die Webinare wieder aufgezeichnet und sind anschließend in unserem Webinar Archiv verfügbar.
Wir freuen uns über eine rege Teilnahme!

Christian Stein
Christian Stein
Manager Sales

Christian kommt ursprünglich aus der Personalberatungsbranche, wo er aber schon immer auf den IT Bereich spezialisiert war. Bei NETWAYS arbeitet er als Manager Sales und berät unsere Kunden in der vertrieblichen Phase rund um das Thema Monitoring. Gemeinsam mit Georg hat er sich Mitte 2012 auch an unserem Hardware-Shop "vergangen".