Skip to content

Instantly share code, notes, and snippets.

View danielnegreiros's full-sized avatar

Daniel Barros danielnegreiros

View GitHub Profile

Learning Production Ansible from real projects: A Dive into Kubespray Practices

Hi. Welcome to this exploration of Ansible real projects to help us close (or at least shorten) the gap between training knowledge and practical experience that are used in production-grade projects.

Often, the training deeply focuses on specific topics such as modules, filters, and inventories which are highly valuable, but misses on best practices and comprehensive knowledge that should bring everything together.

So my favorite practice is to explore real projects, clone them in my local environment, and play with them until I become comfortable with the ideas and the practices. In this post, I'll share some insights I got from observing Kubespray, an Ansible tool to deploy and maintain Kubernetes clusters.

An intermediate knowledge or experience in Ansible is desired to follow along.

name: Molecule Testes on Push
"on":
push:
jobs:
find_changed_roles:
runs-on: ubuntu-latest
outputs:
name: Molecule Tests on Push
"on":
push:
jobs:
ansible_testing:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
# Python 3
$ pip3 install molecule ansible-lint docker molecule[docker]
# Older Python
$ pip install molecule ansible-lint docker molecule[docker]
cd ~/ansible_molecule/roles/nginx
molecule test
# Get inside the role directory
cd ansible_molecule/roles/nginx/
molecule init scenario --driver-name docker default
---
dependency:
name: galaxy
driver:
name: docker
platforms:
- name: instance
image: quay.io/centos/centos:stream8
pre_build_image: true
command: /sbin/init
---
# This is an example playbook to execute Ansible tests.
- name: Verify
hosts: all
gather_facts: false
tasks:
- name: Get info from Nginx running process
ansible.builtin.command: systemctl status nginx
---
- name: Converge
hosts: all
tasks:
- name: "Include role"
include_role:
name: "nginx"
---
dependency:
name: galaxy
driver:
name: docker
platforms:
- name: instance
image: quay.io/centos/centos:stream8
pre_build_image: true
provisioner: