下面的变量error_code包含以下字符串:
“失败”:真的
如何使用此字符串作为“何时模块”的触发器?我不确定如何转义这些特殊字符,以便剧本正确地解释这些字符。下面是我尝试过的内容,但它不起作用:
- 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
发布于 2022-01-02 22:19:34
将字符串放入单国佬中。
- 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
下一个选项是将字符串转换为字典,并测试属性的布尔值失败。
- 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://stackoverflow.com/questions/70561386
复制相似问题