More Kestra Automation

Since starting with Kestra, I have found a few things which are annoying. The format of outputs should be consistent, but doesn't appear to be. While I am working through those, and they may well be the subject of another post, this time I thought I'd shamelessly extend what I have done before extended with some of Christian Lempa's files.

The job I am extending is my 1am Trigger. In addition to other things, I'd like this to do some clean up and update stuff on my VMs. Afterall, what is the point of automation if not to do it for you. I have had ansible playbooks for proxmox, my servers and docker containers for a while, so scheduling them should be straightforward.

The first task I'm going to add is cleaning up docker images. I run the watch tower application on several of my docker hosts and it leaves behind the older images. I know it can clean up itself, but that is only one case for images. Fortunately, Christian does have a template for doing this located here.

The first thing I did was modify the template to only run on my docker hosts called "docker" in my hosts file. While Christian's template is far more flexible, I don't need that as only the hosts that are docker hosts run docker for me.

My Kestra job looks like this:

id: cleanup_docker
namespace: ansible
description: Remove docker images

labels:
  env: prod
  project: ansible
    
tasks:
  - id: docker_cleanup
    type: io.kestra.plugin.core.flow.WorkingDirectory
    tasks:
      - id: ansible_task
        namespaceFiles:
          enabled: true
          include:
          - hosts
          - docker-cleanup.yaml
        type: io.kestra.plugin.ansible.cli.AnsibleCLI
        docker:
          image: cytopia/ansible:latest-tools
        env:
          "ANSIBLE_HOST_KEY_CHECKING": "false"
        commands:
          - apk add sshpass
          - ansible-playbook -i hosts docker-cleanup.yaml
  - id: call_outputs_slack-notifer-webhook
    type: io.kestra.plugin.core.flow.Subflow
    namespace: outputs
    flowId: slack-notifier-webhook
    inputs:
      payload: "Cleanup completed."
    wait: true
    transmitFailed: false
errors:
  - id: server_unreachable
    type: io.kestra.plugin.core.flow.Subflow
    namespace: outputs
    flowId: slack-notifier-webhook
    inputs:
      payload: "Cleanup had an issue."
    wait: true
    transmitFailed: false

concurrency:
  behavior: CANCEL
  limit: 1

You could pass the variable to the script if you wanted, but adding this solved my problem of cleaning up my docker containers periodically. Next time, I'll look at running my update script for my docker hosts as well.

As an Amazon Associate I earn from qualifying purchases.

If you have found this post useful, please consider donating.