首页 > 解决方案 > Ansible:使用 if else 条件在调试消息中打印循环“项目”

问题描述

我正在尝试编写多个服务的停止和启动的摘要。我的代码看起来像

vars:
    profiles:
      - service01
      - service02

    - name: Service  STOP
      command: command to stop a service
      with_items: "{{profiles}}"
      register: stop
      
    - debug: "var= stop.results.{{item}}.stdout"
      with_sequence: "0-{{profiles|length - 1}}"
      
    - name:  Service START
      shell: Command to start a service
      with_items: "{{profiles}}"
      register: start
      
    - debug: "var=start.results.{{item}}.stdout"
      with_sequence: "0-{{profiles|length - 1}}"
     

    - name: Summary of the WebServer valdiation
      debug:
        msg: |
         "{{'WebServer Stop: Success' if stop.results.['item'].rc == 0 else stop.results.['item'].stderr}}"
         "{{'WebServer Start: Success' if start.results.['item'].rc == 0 else start.results.['item'].stderr}}"
      with_sequence: "0-{{profiles|length - 1}}"
     
     

我无法弄清楚如何打印最后一个任务(WebServer valdiation 的摘要)。它必须遍历项目。有人可以帮我如何打印如下所示的输出。

    WebServer Stop: Success
    WebServer Start: Success

标签: ansible

解决方案


推荐阅读