stdout is not being passed correctly?

2020-05-07 10:35发布

Peculiar problem. For some reason, the value in stdout is not being recognized correctly. What I am trying to do is to grep value of the port being listened on and try to match. If defined port exists (i.e., being listened produced one message, if not, produce another).

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

  - name: post_message_prom_kafka
    debug: msg="Prometheus for Kafka {{  (kafka_prom_port == prom_status.stdout) | ternary ('', 'NOT') }} listening on {{ kafka_prom_port }} - Current Listen value is {{ prom_status.stdout }}"
    tags:
      - post_message_prom_kafka

Here is the output:

TASK [kafkanodes : check_prometheus_status_kafka] **********************************************************************************************************
changed: [mifid-cnj-prod-k1.bnymellon.net]

TASK [kafkanodes : debug] **********************************************************************************************************************************
ok: [mifid-cnj-prod-k1.bnymellon.net] => {
    "prom_status.stdout": "9071"
}

TASK [kafkanodes : post_message_prom_kafka] ****************************************************************************************************************
ok: [mifid-cnj-prod-k1.bnymellon.net] => {
    "msg": "Prometheus for Kafka NOT listening on 9071 - Current Listen value is 9071"
}

As you can see, Ansible is reporting that port is NOT being listend on, while the previous task explicitly tells me that port IS being listened on '9071' (as well as the very current task which gets value from prom_status.stdout shows me the port value).

Now, if I replace this line:

debug: msg="Prometheus for Kafka {{  (kafka_prom_port == prom_status.stdout) | ternary ('', 'NOT') }} listening on {{ kafka_prom_port }} - Current Listen value is {{ prom_status.stdout }}"

...with this:

debug: msg="Prometheus for Kafka {{  (kafka_prom_port == 9071) | ternary ('', 'NOT') }} listening on {{ kafka_prom_port }} - Current Listen value is {{ prom_status.stdout }}"

...it works. What am I missing here??

Thanks yet again for your help!!

Alex

标签: ansible
1条回答
狗以群分
2楼-- · 2020-05-07 10:39

Forcefully cast stdout string to number: (kafka_prom_port == prom_status.stdout|int)

查看更多
登录 后发表回答