Skip to content

Instantly share code, notes, and snippets.

@senorsmile
Created February 12, 2019 00:20
Show Gist options
  • Select an option

  • Save senorsmile/74ce548dbbf635b48ff6b159d9977d59 to your computer and use it in GitHub Desktop.

Select an option

Save senorsmile/74ce548dbbf635b48ff6b159d9977d59 to your computer and use it in GitHub Desktop.
---
all:
vars:
k8s_pods:
- name: kuard-5-6
#state: absent
volumes:
- name: "kuard-data"
volume_type: 'nfs'
server: "{{ hostvars['k8s-slave1']['ansible_eth1']['ipv4']['address'] }}"
path: "/home/vagrant"
containers:
- name: main
image: gcr.io/kuar-demo/kuard-amd64:1
port: 8080
port_name: http
port_proto: tcp
volume_mounts:
- name: "kuard-data"
path: "/data"
---
- name: Apply pods
k8s:
name: "{{ item['name'] }}"
state: "{{ item['state'] | default('present') }}"
namespace: "{{ item['namespace'] | default('default') }}"
force: "{{ item['force'] | default(False) }}"
definition: |
{% if 'definition' in item %}
{{ item['definition'] }}
{% else %}
apiVersion: {{ item['api_version'] | default('v1') }}
kind: Pod
metadata:
name: {{ item['name'] }}
{##}{% if 'labels' in item %}
labels: {{ item['labels'] | to_json }}
{##}{% endif %}
spec:
{##}{% if 'volumes' in item %}
{# ----------------------- #}
volumes:
{# ----------------------- #}
{##}{% for volume in item['volumes'] %}
- name: "{{ volume['name'] }}"
{# -------------------- #}
{# - emptyDir --------- #}
{# -------------------- #}
{# #}{% if volume['volume_type'] == 'emptyDir' %}
emptyDir: {}
{# #}{% endif %}
{# -------------------- #}
{# - hostPath --------- #}
{# - https://kubernetes.io/docs/concepts/storage/volumes/#hostpath #}
{# -------------------- #}
{# #}{% if volume['volume_type'] == 'hostPath' %}
hostPath:
path: "{{ volume['path'] }}"
{# #}{% if 'type' in volume %}
type: "{{ volume['type'] }}"
{# #}{% endif %}
{# #}{% endif %}
{# -------------------- #}
{# - nfs --------- #}
{# -------------------- #}
{# #}{% if volume['volume_type'] == 'nfs' %}
nfs:
server: "{{ volume['server'] }}"
path: "{{ volume['path'] }}"
{# #}{% endif %}
{##}{% endfor %}{# for volume in #}
{##}{% endif %}{# 'volumes' in item #}
{# ----------------------- #}
containers:
{# ----------------------- #}
{##}{% for container in item['containers'] %}
- image: {{ container['image'] }}
name: "{{ item['name'] }}-{{ container['name'] | default('main') }}"
{# #}{% if 'port' in container %}
ports:
- containerPort: {{ container['port'] }}
name: {{ container['port_name'] }}
protocol: {{ container['port_proto'] | upper }}
{# #}{% endif %}
{# #}{% if 'liveness_probe' in container %}
livenessProbe: {{ container['liveness_probe'] | to_json }}
{# #}{% endif %}
{# #}{% if 'security_context' in container %}
securityContext: {{ container['security_context'] | to_json }}
{# #}{% endif %}
{# #}{% if 'command' in container %}
command: {{ container['command'] | to_json }}
{# #}{% endif %}
{# #}{% if 'args' in container %}
args: {{ container['args'] | to_json }}
{# #}{% endif %}
{# #}{% if 'volume_mounts' in container %}
volumeMounts:
{# #}{% for volume_mount in container['volume_mounts'] %}
- name: "{{ volume_mount['name'] }}"
mountPath: "{{ volume_mount['path'] }}"
{# #}{% endfor %}
{# #}{% endif %}
{# #}{% if 'min_cpu' in container or 'min_mem' in container or 'max_cpu' in container or 'max_mem' in container %}
resources:
{# #}{% if 'min_cpu' in container or 'min_mem' in container %}
requests:
{# #}{% if 'min_cpu' in container %}
cpu: "{{ container['min_cpu'] }}"
{# #}{% endif %}
{# #}{% if 'min_mem' in container %}
memory: "{{ container['min_mem'] }}"
{# #}{% endif %}
{# #}{% endif %}
{# #}{% if 'max_cpu' in container or 'max_mem' in container %}
limits:
{# #}{% if 'max_cpu' in container %}
cpu: "{{ container['max_cpu'] }}"
{# #}{% endif %}
{# #}{% if 'max_mem' in container %}
memory: "{{ container['max_mem'] }}"
{# #}{% endif %}
{# #}{% endif %}
{# #}{% endif %}
{##}{% endfor %}
{% endif %}
when: k8s_pods is defined
loop: "{{ k8s_pods }}"
@zahodi
Copy link

zahodi commented Feb 15, 2019

feels so messy in here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment