下面的變數error_code包含此字串:
“失敗”:真的
如何使用此字串作為 'when 模塊的觸發器?我不確定如何轉義這些特殊字符,以便劇本正確解釋它們。這是我嘗試過但不起作用的方法:
- name: copying index
copy:
src: /tmp/index.html
dest: /var/www/html/
notify: reloadone
register: error_code
- name: verify content
fail:
msg: There has been an error with the index file
when: " \"failed\"\: true in error_code"
handlers:
- name: reloadone
systemd:
state: restarted
name: httpd
uj5u.com熱心網友回復:
將字串放入單引號中,例如
- hosts: localhost
gather_facts: false
vars:
error_code: '"failed": true'
tasks:
- debug:
var: error_code
- name: verify content
fail:
msg: There has been an error with the index file
when: error_code == result
vars:
result: '"failed": true'
給
TASK [debug] ******************************************************
ok: [localhost] =>
error_code: '"failed": true'
TASK [verify content] *********************************************
fatal: [localhost]: FAILED! => changed=false
msg: There has been an error with the index file
下一個選項是將字串轉換為字典并測驗屬性failed的布林值,例如
- hosts: localhost
gather_facts: false
vars:
error_code: '"failed": true'
tasks:
- debug:
var: error_code|from_yaml
- name: verify content
fail:
msg: There has been an error with the index file
when: result.failed
vars:
result: "{{ error_code|from_yaml }}"
給
TASK [debug] ****************************************************
ok: [localhost] =>
error_code|from_yaml:
failed: true
TASK [verify content] *******************************************
fatal: [localhost]: FAILED! => changed=false
msg: There has been an error with the index file
如果代碼沒有失敗
error_code: '"failed": false'
條件將被跳過
TASK [debug] *****************************************************
ok: [localhost] =>
error_code|from_yaml:
failed: false
TASK [verify content] ********************************************
skipping: [localhost]
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/403466.html
標籤:
下一篇:DVC共享Windows目錄設定
