基于任务结果的Ansible Message输出

时间:2017-11-01 19:03:49

标签: ansible

Ansible是否可以根据任务结果显示不同的消息?

例如

 - 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

我想要的是另一条消息,它会输出类似Prometheus for Kafka is NOT listening on port {{ prom_status.stdout }}

的内容

1 个答案:

答案 0 :(得分:1)

你的问题不是很清楚,但这里有一些值得考虑的事情(使用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 }}"