问题原文
原帖链接 · saffron · CC BY-SA 4.0
I am getting a count from powershell command and registering it on variable. I have to use that count in when condition. I have changed it to int before using it in when condition too. Still that task ( mail notification) is getting skipped, eventhough the count is 0 here. Can someone tell me what I am doing wrong here. Below is the code I am executing
- name: Get message_count
shell: echo "{{ (output.stdout | from_json).MessageCount }}"
register: message_count #message_count is Zero here
delegate_to: localhost
- set_fact:
countt: "{{ message_count | int}}"
#tried converting to integer before passing to condition using set_fact
- debug: var=countt
- name: send mail notification
mail:
host: abc.zzzz.net
port: 25
from: <[email omitted]>
to:
- [email omitted]
subject: Test mail sent from core server
body: Test mail sent from core server
delegate_to: localhost
when: countt==0
已采纳解答
原帖链接 · Roman Spiak · CC BY-SA 4.0
here's what I did to make it work:
---
- name: answer serverfault
hosts: all
become: yes
tasks:
- name: Get message_count
shell: ls /tmp/empty | wc -l
register: tmp_count
delegate_to: localhost
- debug: var=tmp_count.stdout
- name: do something else when tmp_count.stdout == 0
shell: echo "I am doing it"
delegate_to: localhost
when: tmp_count.stdout | int == 0
and here's the playbook run result:
ripper@mini-ripper:~/Devel/ansible$ ansip ./test_playbook.yml -i localhost,
PLAY [answer serverfault] **************************************************************************************************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
[WARNING]: Platform linux on host localhost is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [localhost]
TASK [Get message_count] ******************************************************************************************************************************************************************************************
changed: [localhost -> localhost]
TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [localhost] => {
"tmp_count.stdout": "0"
}
TASK [do something else when tmp_count.stdout == 0] ***************************************************************************************************************************************************************
changed: [localhost -> localhost]
PLAY RECAP ********************************************************************************************************************************************************************************************************
localhost : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
so to recap:
- you should check if the register variable isn't more complex structure - it usually is
- you don't need another custom fact
- you need to convert your variable without using
{{ }}inwhencondition
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
