Working with Ansible


Note

For more background information, make sure to check out the demos overview page.

This section documents several different ways to use ansible in one place.

If you are curious to see a tutorial-style introduction to the actual implementation of ansible in k8s.mk, see also this part of the compose.mk docs, but you won't need that background to follow these examples.

Source Code


Ad-hoc Mode


The simplest way to work with ansible is using the adhoc-mode, which we have seen before in the cluster lifecycle demo. After finding some useful ansible module, probably something inside the kubernetes core collection, then you can just call it.

From a project makefile, it looks something like this:

...

deploy.helm:
    ${jb} name=ahoy \
        chart_ref=hello-world \
        release_namespace=default \
        chart_repo_url="https://helm.github.io/examples" \
    | ${make} ansible.helm 

...

In the above example, jb constructs json in the way you'd expect, and the result is passed to ansible. From console you can do basically the same thing like this:

$ ./k8s.mk jb name=ahoy \
        chart_ref=hello-world \
        release_namespace=default \
        chart_repo_url="https://helm.github.io/examples" \
    | KUBECONFIG=./my-kubeconfig ./k8s.mk ansible.helm
{ .. json .. }

Task-Lists


Another option is to describe partial playbooks, i.e. inlined task-lists without the hassle of inventories or the rest of the playbook config context.

Summary
#!/usr/bin/env -S make -f
# Several ways to work with ansible, part 1.
# Demonstrating direct execution of a task-list, without a playbook.
#
# See the documentation here[1] for more discussion.
# This demo ships with the `k8s-tools` repo and runs as part of the test-suite.
#
# USAGE: 
#   ./demos/ansible.mk clean create deploy 
#
# REF:
#   [1] https://robot-wranglers.github.io/k8s-tools/demos/ansible/
#░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░

# Cluster lifecycle basics and other boilerplate. (The same for most demos)
include k8s.mk
export KUBECONFIG:=./local.cluster.yml
$(call compose.import, file=k8s-tools.yml)

$(eval $(call k8s.scaffold.cluster, \
  name=ansible \
  template=minikube \
  args='--driver=docker -v1 --wait=all --embed-certs'))

__main__: clean create deploy 
deploy: my_ansible_tasks

#░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░

my_ansible_tasks:
    $(call ansible_tasks, -e extra=Variables -e are=allowed)
define my_ansible_tasks
- name: "{{extra}} {{are}} in tasks."
  kubernetes.core.helm:
    name: ahoy
    chart_ref: hello-world
    release_namespace: default
    chart_repo_url: "https://helm.github.io/examples"
endef

Interactive Workflows