kestra icon indicating copy to clipboard operation
kestra copied to clipboard

Not possible to generate Output in shell command

Open schmanat opened this issue 3 months ago • 0 comments

Describe the issue

I'm trying to generate an (kesta) output on an ansible playbook. For that I have the following ansible playbook.

---
- name: Check OS with version
  hosts: '{{ inventory_hosts }}'
  gather_facts: true
  vars: 
    kestra: |
      from kestra import Kestra

      my_result = {
            "hostname": "{{ ansible_hostname }}",
            "distribution": "{{ ansible_facts['distribution'] }}",
            "version": "{{ ansible_facts['distribution_version'] }}"
          }
      Kestra.outputs(my_result)

  tasks:
    - name: show the used os with distribution version
      debug:
        msg: "the OS on host {{ ansible_hostname }} is {{ ansible_facts['distribution'] }} with version {{ ansible_facts['distribution_version'] }}"

    - name: create key value pair on kestra
      uri:
        url: "https://kestra.schman.dev/api/v1/namespaces/adsd.linux.system/kv/{{ ansible_hostname }}"
        method: PUT
        headers:
          Conent-Type: "application/json"
        body: |
          {
            "hostname": "{{ ansible_hostname }}",
            "distribution": "{{ ansible_facts['distribution'] }}",
            "version": "{{ ansible_facts['distribution_version'] }}"
          }
        body_format: json

    - name: create kestra output
      shell:
        cmd: echo '::{"outputs":{"hostname":"{{ ansible_hostname }}","distribution":"{{ ansible_facts["distribution"] }}","version":"{{ ansible_facts["distribution_version"] }}"}}::'

    - name: install kestra via pip
      pip: 
        name: kestra
        state: present

    - name: run python script for kestra output
      shell: "python3 -c '{{ kestra }}'"

In order to play this, i created the following flow (ansible plugin).

id: show-os
namespace: adsd.linux.system
description: "Dieser Task, gibt das verwendete Betriebsystem aus."

labels:
  type: test
  tech: ansible

inputs:
  - id: host
    type: STRING
    defaults:
      - "lx1402-00"
    displayName: "Servernam"
    description: "Servername auf dem das Playbook ausgeführt wird, mehere Hosts sind möglich (host1,host2)."
    required: true

variables:
  ansible_playbook_path: 'playbooks/system/os.yaml'

tasks:
  - id: wdir
    type: io.kestra.plugin.core.flow.WorkingDirectory

    tasks:
      - id: clone_ansible_repository
        type: io.kestra.plugin.git.Clone
        url: "{{ kv('git_url') }}"
        branch: "{{ kv('git_branch') }}"
        username: "{{ kv('git_user' )}}"
        password: "{{ secret('GITLAB_ACCESS_TOKEN') }}"

      - id: load_ssh_key
        type: io.kestra.plugin.core.storage.LocalFiles
        inputs:
          id_rsa: "{{ secret('SSH_KEY') }}"

      - id: ansible
        type: io.kestra.plugin.ansible.cli.AnsibleCLI
        commands:
          - ansible-playbook -i "{{ kv('ansible_inventory') }}" --extra-vars "inventory_hosts={{ inputs.host }}" "{{ vars.ansible_playbook_path }}"
        taskRunner:
          type: io.kestra.plugin.scripts.runner.docker.Docker
          user: "1000"
          pullPolicy: IF_NOT_PRESENT
        containerImage: cytopia/ansible:latest-tools
        env:
          ANSIBLE_HOST_KEY_CHECKING: "False"
          ANSIBLE_REMOTE_USER: "mschneid"
          ANSIBLE_BECOME_PASS: "{{ secret('MSCHNEID_SUDO_PASSWORD') }}"
          ANSIBLE_PRIVATE_KEY_FILE: id_rsa

The ansible playbook works as expected. It works also to create KV to kestra with the API. After asking in the slack channel (https://app.slack.com/client/T01JX6XH5KN/activity) Anna told me, that should work to generate the kestra output via the shell command in my playbook. So I opened this issue :)

Environment

  • Kestra Version: 0.20.0-snapshot
  • Operating System (OS/Docker/Kubernetes): docker (docker-compose)
  • Java Version (if you don't run kestra in Docker):

schmanat avatar Oct 27 '24 09:10 schmanat