首页 > 解决方案 > 如何在ansible中显示两个不同任务寄存器变量的值

问题描述

下面是我的剧本,

---
- hosts: all
  tasks:
  - name: asyn task 1 use time command to see diff use when hostname to get faster output
    command: sleep 15
    async: 2
    poll: 0
    register: result1

  - name: asyn task
    command: sleep 2
    register: result2

  - name: showing result1
    debug:
     var: result1
     var1: result2

  - name: debugging output
    debug: msg=this is the {‌{ result1 }} and {‌{ result2 }}
#   with_items:
#   - {‌{ result1 }}
#   - {‌{ result2 }}

低于错误,

changed: [vishwa]



TASK [showing result1] **************************************************************************************************

fatal: [rudra]: FAILED! => {"changed": false, "msg": "'var1' is not a valid option in debug"}

fatal: [arya]: FAILED! => {"changed": false, "msg": "'var1' is not a valid option in debug"}

fatal: [vishwa]: FAILED! => {"changed": false, "msg": "'var1' is not a valid option in debug"}

        to retry, use: --limit @/home/admin/ansibledemo/asynch.retry

标签: ansibleansible-2.x

解决方案


  1. debug 没有参数 var1 或 var。您不能只为模块创建参数。错误信息很清楚。
  2. 如果您想存储 result1 或 result2 则使用 set_fact 模块。
  3. 您正在执行在后台运行的异步任务。异步任务发生火灾后忘记执行。该任务将被触发并处于运行状态,而另一个正在被触发。使用 2 https://docs.ansible.com/ansible/latest/modules/async_status_module.html 获取异步任务的结果。

参考:https ://docs.ansible.com/ansible/2.7/user_guide/playbooks_async.html


推荐阅读