获取 ansible 任务名称作为变量

问题描述 投票:0回答:1
tasks:
  - name: Hello Sherlock
    debug: 
      msg: "{{ some_registered_output_or_var }}"

  - name: Hello Avengers
    debug:
      msg: "{{ some_var }}"

从上面的内容来看,ansible中有没有什么方法可以将任务名称作为变量 动态地,

例如:

var1=Hello Sherlock
var2=Hello Avengers

感谢对此的帮助。

variables ansible
1个回答
0
投票

...在执行期间将任务名称即时写入文件。有人会引用任务成功列表的文件,并通过向某些编排工具提供输入来在失败的同一任务中启动 (

--start-at-task
) 任务...

这正是

log_plays
回调 - 将 playbook 输出写入日志文件  正在执行的操作。如果通过
ansible.cfg

下配置
[defaults]
stdout_callback         = yaml
callback_whitelist      = log_plays

[callback_log_plays]
log_folder              = /tmp/ansible/hosts

一个最小的示例手册

---
- hosts: test
  become: false
  gather_facts: false

  tasks:

  - name: Execute shell command
    shell: sleep 0

  - name: Debug task
    debug:
      msg: "Debug output"

将导致

stdout

PLAY [test.example.com] **********************

TASK [Execute shell command] *****************
changed: [test.example.com]

TASK [Debug task] ****************************
ok: [test.example.com] =>
  msg: Debug output

PLAY RECAP ***********************************
test.example.com           : ok=2    changed=1

和一个日志文件

/tmp/ansible/hosts/test.example.com
,内容为

Oct 16 2023 19:45:00 - log_plays.yml - Execute shell command - shell - OK - {"module_args": {"creates": null, "executable": null, "_uses_shell": true, "strip_empty_ends": true, "_raw_params": "sleep 0", "removes": null, "argv": null, "warn": false, "chdir": null, "stdin_add_newline": true, "stdin": null}} => {"stderr_lines": [], "changed": true, "end": "2023-10-16 19:45:00.296408", "_ansible_no_log": false, "stdout": "", "cmd": "sleep 0", "rc": 0, "start": "2023-10-16 19:45:00.287217", "stderr": "", "delta": "0:00:00.009191", "stdout_lines": [], "msg": ""}

Oct 16 2023 19:45:00 - log_plays.yml - Debug task - debug - OK - {"msg": "Debug output", "changed": false, "_ansible_verbose_always": true, "_ansible_no_log": false}

另一个问答

© www.soinside.com 2019 - 2024. All rights reserved.