Created
July 16, 2019 11:33
-
-
Save kvaps/d2e01ad6a6cae235077fcfba8b87e39c to your computer and use it in GitHub Desktop.
Ansible runner for Kubernetes Custom Resources
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apiVersion: apiextensions.k8s.io/v1beta1 | |
kind: CustomResourceDefinition | |
metadata: | |
name: foo.example.org | |
spec: | |
group: example.org | |
names: | |
kind: Foo | |
listKind: FooList | |
plural: foos | |
singular: foo | |
scope: Cluster | |
subresources: | |
status: {} | |
version: v1alpha1 | |
versions: | |
- name: v1alpha1 | |
served: true | |
storage: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -eo pipefail | |
kubectl get foo.example.org -o name -w | | |
while true; do | |
while read -t 1 name; do | |
hosts="$hosts,${name##*/}" | |
done | |
if ! kill -0 $(($$+1)); then | |
exit 1 | |
fi | |
if [ -z "$hosts" ]; then | |
continue | |
fi | |
ansible-playbook playbook.yaml -l "localhost,$hosts" | |
unset hosts | |
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ansible-playbook | |
--- | |
- hosts: localhost | |
gather_facts: no | |
tasks: | |
- set_fact: | |
resources: "{{ lookup('k8s', api_version='example.org/v1alpha1', kind='Foo') }}" | |
- add_host: | |
hostname: "{{ item }}" | |
groups: "process_resources" | |
ansible_connection: local | |
metadata: "{{ metadata }}" | |
spec: "{{ spec }}" | |
status: "{{ status }}" | |
loop: "{{ resources | map(attribute='metadata.name') | list }}" | |
vars: | |
res: "{{ resources | selectattr('metadata.name', 'equalto', item) | first }}" | |
metadata: "{{ res.metadata }}" | |
spec: "{{ res.spec }}" | |
status: "{{ res.status | default({}) }}" | |
#when: "status.generation is not defined or status.generation != metadata.generation" | |
changed_when: false | |
- hosts: process_resources | |
gather_facts: no | |
tasks: | |
- meta: end_host | |
when: "status.generation is defined and status.generation|int == metadata.generation|int" | |
- debug: | |
msg: "do something" | |
changed_when: true | |
- k8s_status: | |
api_version: 'example.org/v1alpha1' | |
kind: 'Foo' | |
name: '{{ metadata.name }}' | |
status: | |
generation: "{{ metadata.generation }}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment