简体   繁体   English

如何在 doit 任务中使用 args、slurp、register 和 set_fact 等 ansible 参数生成变量?

[英]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.我正在创建一个 doit 任务,我需要在操作中的任务内使用诸如args、slurp、register 和 set_fact之类的 ansible 参数,以确保正确访问此任务中的变量。 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.我遇到问题的变量是scm_sha_master ,它需要使用 args、slurp、register 和 set_fact 在任务本身内生成。

Thankyou in advance.先感谢您。

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}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM