'How to generate variable using ansible parameters like args, slurp, register and set_fact inside a doit task?

I am creating a doit task where I need to use ansible parameters like args, slurp, register and set_fact inside the task within actions to make sure the variable inside this task is accessed properly. Do I have to define these params separately within the task before actions?

An example of how I would like to have it look like and current situation:

def task_xyz_load():
    actions = [
        f'set -x',
        f'git rev-parse --short master > \ {build_dir}/{xyz_project}_tables/sha_master',
        args:
            chdir: "{src_dir}"
            executable: /bin/bash,
        slurp:
            src: "{build_dir}/{xyz_project}_tables/sha_master",
        register: scm_sha_master_b64
        set_fact:
            scm_sha_master: "{scm_sha_master_b64['content'] | b64decode | trim}",
        ["echo", f"{xyz_PATH}/bin/xyz", "--url", xyz_URL,
        "load", "--name", f"{scm_sha_master}", "--force",
        f"{xyz_PROJECT}/{xyz_OUTPUT_PATH}/{xyz_PROJECT}_tables"],
    ]
    return {"actions": actions}

The variable I am having issue with is scm_sha_master which needs to be generated within the task itself using args, slurp, register and set_fact.

Thankyou in advance.



Solution 1:[1]

The issue was resolved importing subprocess as below:

import subprocess

scm_sha_master = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).decode('ascii').strip()

def task_xyz():

actions = [
    ["echo", f"{xyz_PATH}/bin/xyz", "--url", xyz_URL,
    "load", "--name", f"{scm_sha_master}", "--force",
    f"{xyz_PROJECT}/{xyz_OUTPUT_PATH}/{xyz_PROJECT}_tables"],
]
return {"actions": actions}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sujan Shah