@@ -140,15 +140,15 @@ tasks:
140140如果 `ansible_facts['distribution_version']` 大于或等于 `12.04`,此测试就会返回 `True`,否则返回 `False`。
141141
142142> **译注**:类似于上一小节中的 [`ansible_mounts`](filters.md#ansible_mounts),这里的 `ansible_facts['distribution_version']` 也可以通过 `gather_subset` 动作获取到(已设置 `gather_facts: False`)。如下所示。
143-
144- ` ` ` yaml
145- - setup:
146- gather_subset:
147- - distribution_version
148- - debug:
149- msg: "{{ ansible_facts['distribution_version'] is version('12.04', '>=') }}"
150- ` ` `
151-
143+ >
144+ > ```yaml
145+ > - setup:
146+ > gather_subset:
147+ > - distribution_version
148+ > - debug:
149+ > msg: "{{ ansible_facts['distribution_version'] is version('12.04', '>=') }}"
150+ > ```
151+ >
152152> 因此也可以将 `ansible_facts[`distribution_version`]` 写作 `ansible_distribution_version`。
153153
154154
@@ -165,11 +165,11 @@ tasks:
165165` ` `
166166
167167> **译注**:在 `sample_version_var: '1.0.1a'` 时,若 `strict=True` 开启严格版本解析,则会报出错误:
168-
169- ` ` ` console
170- fatal: [debian_199]: FAILED! => {"msg": "Version comparison failed: invalid version number '1.0.1a'"}
171- ` ` `
172-
168+ >
169+ > ```console
170+ > fatal: [debian_199]: FAILED! => {"msg": "Version comparison failed: invalid version number '1.0.1a'"}
171+ > ```
172+ >
173173> 关闭严格版本解析,或以符合严格版本解析模式的版本号测试(见下文,如:`'1.0.0a0'`),则版本测试正常。
174174
175175
@@ -385,22 +385,22 @@ Ansible 包含一个 `contains` 测试,其操作类似于 Jinja2 提供的 `in
385385` ` `
386386
387387> **译注**:若修改其中一项,比如修改最后一条加以修改(删掉一个 `0`):
388-
389- ` ` ` yaml
390- - '"0.10 Gb" == 10240000|human_readable(isbits=True, unit="G")'
391- ` ` `
392-
388+ >
389+ > ```yaml
390+ > - '"0.10 Gb" == 10240000|human_readable(isbits=True, unit="G")'
391+ > ```
392+ >
393393> 则会有下面的输出:
394-
395- ` ` ` json
396- fatal: [debian_199]: FAILED! => {
397- "assertion": "\" 0.10 Gb\" == 10240000|human_readable(isbits=True, unit=\" G\" )",
398- "changed": false,
399- "evaluated_to": false,
400- "msg": "Assertion failed"
401- }
402- ` ` `
403-
394+ >
395+ > ```json
396+ > fatal: [debian_199]: FAILED! => {
397+ > "assertion": "\"0.10 Gb\" == 10240000|human_readable(isbits=True, unit=\"G\")",
398+ > "changed": false,
399+ > "evaluated_to": false,
400+ > "msg": "Assertion failed"
401+ > }
402+ > ```
403+ >
404404> 输出不仅显示出示例中的断言失败,还给出了因具体哪个条目失败,该条目的求值结果。
405405
406406
@@ -468,36 +468,37 @@ fatal: [debian_199]: FAILED! => {
468468
469469
470470> **注意**:从版本 2.1 开始,咱们还可以使用 `success`、`failure`、`change` 和 `skip`,用于上面这些语法匹配,满足对语法要求严格的人的需要。
471- >
472- > **译注**:由于 `/usr/bin/foo` 并不存在,因此第一项任务会失败,且目标主机状态会改变。故该 playbook 的执行结果如下。
473-
474- ` ` ` console
475- TASK [shell] ********************************************************************************************************
476- fatal: [debian_199]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3.11"}, "changed": true, "cmd": "/usr/bin/foo", "delta": "0:00:00.003051", "end": "2025-01-11 17:12:04.559226", "msg": "non-zero return code", "rc": 127, "start": "2025-01-11 17:12:04.556175", "stderr": "/bin/sh: 1: /usr/bin/foo: not found", "stderr_lines": ["/bin/sh: 1: /usr/bin/foo: not found"], "stdout": "", "stdout_lines": []}
477- ...ignoring
478-
479- TASK [debug] ********************************************************************************************************
480- ok: [debian_199] => {
481- "msg": "it failed"
482- }
483-
484- TASK [debug] ********************************************************************************************************
485- ok: [debian_199] => {
486- "msg": "it changed"
487- }
488-
489- TASK [debug] ********************************************************************************************************
490- skipping: [debian_199]
491-
492- TASK [debug] ********************************************************************************************************
493- skipping: [debian_199]
494471
495- TASK [debug] ********************************************************************************************************
496- skipping: [debian_199]
497472
498- PLAY RECAP **********************************************************************************************************
499- debian_199 : ok=3 changed=1 unreachable=0 failed=0 skipped=3 rescued=0 ignored=1
500- ` ` `
473+ > **译注**:由于 `/usr/bin/foo` 并不存在,因此第一项任务会失败,且目标主机状态会改变。故该 playbook 的执行结果如下。
474+ >
475+ > ```console
476+ > TASK [shell] ********************************************************************************************************
477+ > fatal: [debian_199]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3.11"}, "changed": true, "cmd": "/usr/bin/foo", "delta": "0:00:00.003051", "end": "2025-01-11 17:12:04.559226", "msg": "non-zero return code", "rc": 127, "start": "2025-01-11 17:12:04.556175", "stderr": "/bin/sh: 1: /usr/bin/foo: not found", "stderr_lines": ["/bin/sh: 1: /usr/bin/foo: not found"], "stdout": "", "stdout_lines": []}
478+ > ...ignoring
479+ >
480+ > TASK [debug] ********************************************************************************************************
481+ > ok: [debian_199] => {
482+ > "msg": "it failed"
483+ > }
484+ >
485+ > TASK [debug] ********************************************************************************************************
486+ > ok: [debian_199] => {
487+ > "msg": "it changed"
488+ > }
489+ >
490+ > TASK [debug] ********************************************************************************************************
491+ > skipping: [debian_199]
492+ >
493+ > TASK [debug] ********************************************************************************************************
494+ > skipping: [debian_199]
495+ >
496+ > TASK [debug] ********************************************************************************************************
497+ > skipping: [debian_199]
498+ >
499+ > PLAY RECAP **********************************************************************************************************
500+ > debian_199 : ok=3 changed=1 unreachable=0 failed=0 skipped=3 rescued=0 ignored=1
501+ > ```
501502
502503
503504# # 类型测试
0 commit comments