Updated for 0.10.0, adding molecule testing with Travis-CI

This commit is contained in:
Xan Manning 2019-10-01 21:31:35 +01:00
parent 2327d0433d
commit efc703541c
21 changed files with 215 additions and 14 deletions

5
.gitignore vendored
View File

@ -4,5 +4,6 @@ VAULT_PASSWORD
VAULT_PASS
.vault_pass
.vault_pass.asc
tests/fetch
tests/ubuntu-*.log
vagramt/fetch
vagrant/ubuntu-*.log
__pycache__

39
.travis.yml Normal file
View File

@ -0,0 +1,39 @@
---
# Thanks to geerlingguy!
language: python
services: docker
env:
global:
- ROLE_NAME: k3s
matrix:
- MOLECULE_DISTRO: centos8
- MOLECULE_DISTRO: centos7
- MOLECULE_DISTRO: ubuntu1804
- MOLECULE_DISTRO: debian10
- MOLECULE_DISTRO: fedora29
- MOLECULE_DISTRO: fedora30
- MOLECULE_DISTRO: amazonlinux2
# Test other role features.
- MOLECULE_DISTRO: centos7
MOLECULE_PLAYBOOK: playbook-docker.yml
install:
# Install test dependencies.
- pip install molecule docker jmespath
before_script:
# Use actual Ansible Galaxy role name for the project directory.
- cd ../
- mv ansible-role-$ROLE_NAME xanmanning.$ROLE_NAME
- cd xanmanning.$ROLE_NAME
script:
# Run tests.
- molecule test
notifications:
webhooks: https://galaxy.ansible.com/api/v1/notifications/

11
.yamllint Normal file
View File

@ -0,0 +1,11 @@
extends: default
rules:
braces:
max-spaces-inside: 1
level: error
brackets:
max-spaces-inside: 1
level: error
line-length: disable
truthy: disable

View File

@ -3,6 +3,8 @@
Ansible role for installing [Racher Labs k3s](https://k3s.io/) ("Lightweight
Kubernetes") as either a standalone server or cluster.
[![Build Status](https://www.travis-ci.org/PyratLabs/ansible-role-k3s.svg?branch=master)](https://www.travis-ci.org/PyratLabs/ansible-role-k3s)
## Requirements
This role has been tested on Ansible 2.6.0+ against the following Linux Distributions:
@ -12,6 +14,7 @@ This role has been tested on Ansible 2.6.0+ against the following Linux Distribu
- Debian 10
- openSUSE Leap 15
- Ubuntu 18.04 LTS
- Amazon Linux 2
## Disclaimer

View File

@ -1,18 +1,20 @@
---
- name: reload systemctl
command: systemctl daemon-reload
args:
warn: false
- name: reload systemd
systemd:
daemon_reload: true
# when: molecule_is_test is not defined
- name: restart k3s
service:
name: k3s
state: restarted
enabled: true
# when: molecule_is_test is not defined
- name: restart docker
service:
name: docker
state: restarted
enabled: true
# when: molecule_is_test is not defined

View File

@ -1,6 +1,7 @@
---
galaxy_info:
role_name: k3s
author: Xan Manning
description: Ansible role for installing k3s as either a standalone server or cluster
company: Pyrat Ltd.
@ -38,6 +39,11 @@ galaxy_info:
- name: EL
versions:
- 7
- name: Amazon
- name: Fedora
versions:
- 29
- 30
- name: Debian
versions:
- buster

View File

@ -0,0 +1,26 @@
# Molecule managed
{% if item.registry is defined %}
FROM {{ item.registry.url }}/{{ item.image }}
{% else %}
FROM {{ item.image }}
{% endif %}
RUN if [ $(command -v apt-get) ]; then apt-get update && apt-get install -y python systemd sudo bash ca-certificates && apt-get clean; \
elif [ $(command -v dnf) ]; then dnf makecache && dnf --assumeyes install python systemd sudo python-devel python*-dnf bash && dnf clean all; \
elif [ $(command -v yum) ]; then yum makecache fast && yum install -y python systemd sudo yum-plugin-ovl bash && sed -i 's/plugins=0/plugins=1/g' /etc/yum.conf && yum clean all; \
elif [ $(command -v zypper) ]; then zypper refresh && zypper install -y python systemd sudo bash python-xml && zypper clean -a; \
elif [ $(command -v apk) ]; then apk update && apk add --no-cache python sudo systemd bash ca-certificates; \
elif [ $(command -v xbps-install) ]; then xbps-install -Syu && xbps-install -y python systemd sudo bash ca-certificates && xbps-remove -O; fi
RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == systemd-tmpfiles-setup.service ] || rm -f $i; done); \
rm -f /lib/systemd/system/multi-user.target.wants/*; \
rm -f /etc/systemd/system/*.wants/*; \
rm -f /lib/systemd/system/local-fs.target.wants/*; \
rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
rm -f /lib/systemd/system/basic.target.wants/*; \
rm -f /lib/systemd/system/anaconda.target.wants/*;
VOLUME [/sys/fs/cgroup]
CMD [/usr/sbin/init]

View File

@ -0,0 +1,22 @@
*******
Docker driver installation guide
*******
Requirements
============
* Docker Engine
Install
=======
Please refer to the `Virtual environment`_ documentation for installation best
practices. If not using a virtual environment, please consider passing the
widely recommended `'--user' flag`_ when invoking ``pip``.
.. _Virtual environment: https://virtualenv.pypa.io/en/latest/
.. _'--user' flag: https://packaging.python.org/tutorials/installing-packages/#installing-to-the-user-site
.. code-block:: bash
$ pip install 'molecule[docker]'

View File

@ -0,0 +1,40 @@
---
dependency:
name: galaxy
driver:
name: docker
lint:
name: yamllint
platforms:
- name: node1
image: "geerlingguy/docker-${MOLECULE_DISTRO:-centos8}-ansible:latest"
command: ${MOLECULE_DOCKER_COMMAND:-""}
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
privileged: true
pre_build_image: true
- name: node2
image: "geerlingguy/docker-${MOLECULE_DISTRO:-centos8}-ansible:latest"
command: ${MOLECULE_DOCKER_COMMAND:-""}
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
privileged: true
pre_build_image: true
- name: node3
image: "geerlingguy/docker-${MOLECULE_DISTRO:-centos8}-ansible:latest"
command: ${MOLECULE_DOCKER_COMMAND:-""}
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
privileged: true
pre_build_image: true
provisioner:
name: ansible
lint:
name: ansible-lint
playbooks:
converge: ${MOLECULE_PLAYBOOK:-playbook.yml}
verifier:
name: testinfra
lint:
name: flake8

View File

@ -0,0 +1,9 @@
---
- name: Converge
hosts: all
become: true
vars:
molecule_is_test: true
k3s_ensure_docker_installed: true
roles:
- role: xanmanning.k3s

View File

@ -0,0 +1,8 @@
---
- name: Converge
hosts: all
become: true
vars:
molecule_is_test: true
roles:
- role: xanmanning.k3s

View File

@ -0,0 +1,14 @@
import os
import testinfra.utils.ansible_runner
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')
def test_hosts_file(host):
f = host.file('/etc/hosts')
assert f.exists
assert f.user == 'root'
assert f.group == 'root'

Binary file not shown.

View File

@ -3,7 +3,12 @@
- name: Ensure ansible_host is mapped to inventory_hostname
lineinfile:
path: /tmp/inventory.txt
line: "{{ item }}@@@{{ hostvars[item].ansible_host }}@@@{{ hostvars[item].k3s_control_node }}"
line: >-
{{ item }}
@@@
{{ hostvars[item].ansible_host | default(hostvars[item].ansible_fqdn) }}
@@@
{{ hostvars[item].k3s_control_node }}
create: true
loop: "{{ play_hosts }}"
@ -14,11 +19,11 @@
- name: Ensure control node is delegated to for obtaining a token
set_fact:
k3s_control_delegate: "{{ k3s_control_delegate_raw.stdout.split('@@@')[0] }}"
k3s_control_delegate: "{{ k3s_control_delegate_raw.stdout.split(' @@@ ')[0] }}"
- name: Ensure the control node address is registered in Ansible
set_fact:
k3s_control_node_address: "{{ hostvars[k3s_control_delegate].ansible_host }}"
k3s_control_node_address: "{{ hostvars[k3s_control_delegate].ansible_host | default(hostvars[k3s_control_delegate].ansible_fqdn) }}"
- name: Ensure NODE_TOKEN is captured from control node
slurp:
@ -31,16 +36,26 @@
src: k3s.service.j2
dest: /etc/systemd/system/k3s.service
notify:
- reload systemctl
- reload systemd
- restart k3s
- meta: flush_handlers
- name: Wait for control plane to be ready to accept connections
wait_for:
port: 6443
delay: 5
sleep: 5
timeout: 300
when: k3s_control_node
- name: Wait for all nodes to be ready
command: "{{ k3s_install_dir }}/kubectl get nodes"
changed_when: false
failed_when: false
register: kubectl_get_nodes_result
until: kubectl_get_nodes_result.stdout.find("NotReady") == -1
until: kubectl_get_nodes_result.rc == 0
and kubectl_get_nodes_result.stdout.find("NotReady") == -1
retries: 30
delay: 20
when: k3s_control_node

View File

@ -6,7 +6,7 @@
dest: /etc/systemd/system/k3s.service
when: k3s_control_node
notify:
- reload systemctl
- reload systemd
- meta: flush_handlers
@ -27,4 +27,4 @@
name: k3s
state: started
enabled: true
when: k3s_control_node
when: k3s_control_node # and molecule_is_test is not defined

View File

@ -4,7 +4,7 @@ Documentation=https://k3s.io
After=network.target
[Service]
Type=notify
Type={{ 'notify' if k3s_control_node else 'exec' }}
ExecStartPre=-/sbin/modprobe br_netfilter
ExecStartPre=-/sbin/modprobe overlay
{% if k3s_control_node %}
@ -18,6 +18,9 @@ LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
TasksMax=infinity
TimeoutStartSec=0
Restart=always
RestartSec=5s
[Install]
WantedBy=multi-user.target

View File

@ -1,3 +1,5 @@
---
all:
vars:
ansible_become: true