Ansible Message Output based on task result

荒凉一梦 提交于 2021-02-08 10:21:05

问题


Is it possible in Ansible to display different messages based on a result of the task?

For example

 - name: check_prometheus_status_kafka
    shell: /usr/sbin/ss -lnt|awk '{ print $4}'|grep 9071 |sed 's/\*//'| sed 's/\://'
    register: prom_status
    tags:
      - check_prometheus_status_kafka

  - name: post_message_prom_kafka
    debug: msg="Prometheus for Kafka listening in {{ prom_status.stdout }}"
    when: prom_status.stdout == "9071"
    tags:
      - post_message_prom_kafka

What I want is an additional message which would output something like Prometheus for Kafka is NOT listening on port {{ prom_status.stdout }}


回答1:


Your question is not very clear, but here is something for your consideration (play with the value of actual_port):

---
- hosts: localhost
  vars:
    port_to_listen_on: "9071"
    actual_port: "9071"
  tasks:
    - debug:
        msg: "Prometheus for Kafka {{ (actual_port == port_to_listen_on) | ternary ('', 'NOT ') }}listening in {{ port_to_listen_on }}"

    # or

     - debug:
         msg: "Prometheus for Kafka {{ 'NOT ' if (actual_port != port_to_listen_on) else '' }}listening in {{ port_to_listen_on }}"


来源:https://stackoverflow.com/questions/47061928/ansible-message-output-based-on-task-result

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!