mirror of
https://github.com/PyratLabs/ansible-role-k3s.git
synced 2024-11-22 11:48:22 +01:00
Updated for 0.10.0, adding molecule testing with Travis-CI
This commit is contained in:
parent
2327d0433d
commit
efc703541c
5
.gitignore
vendored
5
.gitignore
vendored
@ -4,5 +4,6 @@ VAULT_PASSWORD
|
|||||||
VAULT_PASS
|
VAULT_PASS
|
||||||
.vault_pass
|
.vault_pass
|
||||||
.vault_pass.asc
|
.vault_pass.asc
|
||||||
tests/fetch
|
vagramt/fetch
|
||||||
tests/ubuntu-*.log
|
vagrant/ubuntu-*.log
|
||||||
|
__pycache__
|
||||||
|
39
.travis.yml
Normal file
39
.travis.yml
Normal 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
11
.yamllint
Normal 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
|
@ -3,6 +3,8 @@
|
|||||||
Ansible role for installing [Racher Labs k3s](https://k3s.io/) ("Lightweight
|
Ansible role for installing [Racher Labs k3s](https://k3s.io/) ("Lightweight
|
||||||
Kubernetes") as either a standalone server or cluster.
|
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
|
## Requirements
|
||||||
|
|
||||||
This role has been tested on Ansible 2.6.0+ against the following Linux Distributions:
|
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
|
- Debian 10
|
||||||
- openSUSE Leap 15
|
- openSUSE Leap 15
|
||||||
- Ubuntu 18.04 LTS
|
- Ubuntu 18.04 LTS
|
||||||
|
- Amazon Linux 2
|
||||||
|
|
||||||
## Disclaimer
|
## Disclaimer
|
||||||
|
|
||||||
|
@ -1,18 +1,20 @@
|
|||||||
---
|
---
|
||||||
|
|
||||||
- name: reload systemctl
|
- name: reload systemd
|
||||||
command: systemctl daemon-reload
|
systemd:
|
||||||
args:
|
daemon_reload: true
|
||||||
warn: false
|
# when: molecule_is_test is not defined
|
||||||
|
|
||||||
- name: restart k3s
|
- name: restart k3s
|
||||||
service:
|
service:
|
||||||
name: k3s
|
name: k3s
|
||||||
state: restarted
|
state: restarted
|
||||||
enabled: true
|
enabled: true
|
||||||
|
# when: molecule_is_test is not defined
|
||||||
|
|
||||||
- name: restart docker
|
- name: restart docker
|
||||||
service:
|
service:
|
||||||
name: docker
|
name: docker
|
||||||
state: restarted
|
state: restarted
|
||||||
enabled: true
|
enabled: true
|
||||||
|
# when: molecule_is_test is not defined
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
---
|
---
|
||||||
|
|
||||||
galaxy_info:
|
galaxy_info:
|
||||||
|
role_name: k3s
|
||||||
author: Xan Manning
|
author: Xan Manning
|
||||||
description: Ansible role for installing k3s as either a standalone server or cluster
|
description: Ansible role for installing k3s as either a standalone server or cluster
|
||||||
company: Pyrat Ltd.
|
company: Pyrat Ltd.
|
||||||
@ -38,6 +39,11 @@ galaxy_info:
|
|||||||
- name: EL
|
- name: EL
|
||||||
versions:
|
versions:
|
||||||
- 7
|
- 7
|
||||||
|
- name: Amazon
|
||||||
|
- name: Fedora
|
||||||
|
versions:
|
||||||
|
- 29
|
||||||
|
- 30
|
||||||
- name: Debian
|
- name: Debian
|
||||||
versions:
|
versions:
|
||||||
- buster
|
- buster
|
||||||
|
26
molecule/default/Dockerfile.j2
Normal file
26
molecule/default/Dockerfile.j2
Normal 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”]
|
22
molecule/default/INSTALL.rst
Normal file
22
molecule/default/INSTALL.rst
Normal 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]'
|
40
molecule/default/molecule.yml
Normal file
40
molecule/default/molecule.yml
Normal 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
|
9
molecule/default/playbook-docker.yml
Normal file
9
molecule/default/playbook-docker.yml
Normal 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
|
8
molecule/default/playbook.yml
Normal file
8
molecule/default/playbook.yml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
---
|
||||||
|
- name: Converge
|
||||||
|
hosts: all
|
||||||
|
become: true
|
||||||
|
vars:
|
||||||
|
molecule_is_test: true
|
||||||
|
roles:
|
||||||
|
- role: xanmanning.k3s
|
14
molecule/default/tests/test_default.py
Normal file
14
molecule/default/tests/test_default.py
Normal 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'
|
BIN
molecule/default/tests/test_default.pyc
Normal file
BIN
molecule/default/tests/test_default.pyc
Normal file
Binary file not shown.
@ -3,7 +3,12 @@
|
|||||||
- name: Ensure ansible_host is mapped to inventory_hostname
|
- name: Ensure ansible_host is mapped to inventory_hostname
|
||||||
lineinfile:
|
lineinfile:
|
||||||
path: /tmp/inventory.txt
|
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
|
create: true
|
||||||
loop: "{{ play_hosts }}"
|
loop: "{{ play_hosts }}"
|
||||||
|
|
||||||
@ -14,11 +19,11 @@
|
|||||||
|
|
||||||
- name: Ensure control node is delegated to for obtaining a token
|
- name: Ensure control node is delegated to for obtaining a token
|
||||||
set_fact:
|
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
|
- name: Ensure the control node address is registered in Ansible
|
||||||
set_fact:
|
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
|
- name: Ensure NODE_TOKEN is captured from control node
|
||||||
slurp:
|
slurp:
|
||||||
@ -31,16 +36,26 @@
|
|||||||
src: k3s.service.j2
|
src: k3s.service.j2
|
||||||
dest: /etc/systemd/system/k3s.service
|
dest: /etc/systemd/system/k3s.service
|
||||||
notify:
|
notify:
|
||||||
- reload systemctl
|
- reload systemd
|
||||||
- restart k3s
|
- restart k3s
|
||||||
|
|
||||||
- meta: flush_handlers
|
- 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
|
- name: Wait for all nodes to be ready
|
||||||
command: "{{ k3s_install_dir }}/kubectl get nodes"
|
command: "{{ k3s_install_dir }}/kubectl get nodes"
|
||||||
changed_when: false
|
changed_when: false
|
||||||
|
failed_when: false
|
||||||
register: kubectl_get_nodes_result
|
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
|
retries: 30
|
||||||
delay: 20
|
delay: 20
|
||||||
when: k3s_control_node
|
when: k3s_control_node
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
dest: /etc/systemd/system/k3s.service
|
dest: /etc/systemd/system/k3s.service
|
||||||
when: k3s_control_node
|
when: k3s_control_node
|
||||||
notify:
|
notify:
|
||||||
- reload systemctl
|
- reload systemd
|
||||||
|
|
||||||
- meta: flush_handlers
|
- meta: flush_handlers
|
||||||
|
|
||||||
@ -27,4 +27,4 @@
|
|||||||
name: k3s
|
name: k3s
|
||||||
state: started
|
state: started
|
||||||
enabled: true
|
enabled: true
|
||||||
when: k3s_control_node
|
when: k3s_control_node # and molecule_is_test is not defined
|
||||||
|
@ -4,7 +4,7 @@ Documentation=https://k3s.io
|
|||||||
After=network.target
|
After=network.target
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
Type=notify
|
Type={{ 'notify' if k3s_control_node else 'exec' }}
|
||||||
ExecStartPre=-/sbin/modprobe br_netfilter
|
ExecStartPre=-/sbin/modprobe br_netfilter
|
||||||
ExecStartPre=-/sbin/modprobe overlay
|
ExecStartPre=-/sbin/modprobe overlay
|
||||||
{% if k3s_control_node %}
|
{% if k3s_control_node %}
|
||||||
@ -18,6 +18,9 @@ LimitNOFILE=infinity
|
|||||||
LimitNPROC=infinity
|
LimitNPROC=infinity
|
||||||
LimitCORE=infinity
|
LimitCORE=infinity
|
||||||
TasksMax=infinity
|
TasksMax=infinity
|
||||||
|
TimeoutStartSec=0
|
||||||
|
Restart=always
|
||||||
|
RestartSec=5s
|
||||||
|
|
||||||
[Install]
|
[Install]
|
||||||
WantedBy=multi-user.target
|
WantedBy=multi-user.target
|
||||||
|
0
tests/Vagrantfile → vagrant/Vagrantfile
vendored
0
tests/Vagrantfile → vagrant/Vagrantfile
vendored
@ -1,3 +1,5 @@
|
|||||||
|
---
|
||||||
|
|
||||||
all:
|
all:
|
||||||
vars:
|
vars:
|
||||||
ansible_become: true
|
ansible_become: true
|
Loading…
Reference in New Issue
Block a user