Skip to content

Instantly share code, notes, and snippets.

@kvaps
Created July 16, 2019 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kvaps/d2e01ad6a6cae235077fcfba8b87e39c to your computer and use it in GitHub Desktop.
Save kvaps/d2e01ad6a6cae235077fcfba8b87e39c to your computer and use it in GitHub Desktop.
Ansible runner for Kubernetes Custom Resources
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
#!/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
#!/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