测试使用循环的任务结果?
Testing a task result which uses a loop?
所以第一个任务总是被跳过,因为检测到 RedHat 8 应该触发失败模块 运行 但这也被跳过。虽然没有循环也能正常工作。
- name: installing packages
hosts: ansible2
ignore_errors: true
vars_files: varsfile
tasks:
- name: install software based on OS distro and version
yum:
name: "{{ item.name }}"
state: latest
loop: "{{ packages }}"
when: >
( ansible_distribution == "RedHat" and ansible_distribution_major_version is version('12', '>=') )
or
( ansible_distribution == "CentOS" and ansible_distribution_major_version | int >= 8 )
register: result
- fail:
msg: error {{ ansible_hostname }} does not meet minimum requirements
when: result is skipped
关于
So the first task is always skipped because RedHat 8 is detected which should trigger the fail module to run but this gets skipped also.
使用小型测试设置和 debug
task for the variable name to debug
---
- hosts: test.example.com
become: false
gather_facts: true
tasks:
- name: Install software based on OS distro and version
debug:
msg: "{{ ansible_distribution }} {{ ansible_distribution_major_version }}"
register: result
when: ansible_distribution == "RedHat" and ansible_distribution_major_version is version('12', '>=')
with_items: ['pkg1', 'pkg2']
- name: Show result
debug:
var: result
- fail:
msg: "Error: {{ ansible_hostname }} does not meet minimum requirements"
when: result.results[0].skipped
导致输出
TASK [Gathering Facts] ********************************
ok: [test.example.com]
TASK [Show result] ************************************
ok: [test.example.com] =>
result:
changed: false
msg: All items completed
results:
- ansible_loop_var: item
changed: false
item: pkg1
skip_reason: Conditional result was False
skipped: true
...
TASK [fail] *******************************************
fatal: [test.example.com]: FAILED! => changed=false
msg: 'Error: test does not meet minimum requirements'
你可以看到循环会创建一个 list.
- name: Show result
debug:
var: result.results | type_debug
TASK [Show result] ****************
ok: [test.example.com] =>
result.results | type_debug: list
因此您需要将 Conditional 设置为 when: result.results[0].skipped
。
关于
Is works fine without the loop though.
建议使用以下方法简化您的用例
- name: Install software based on OS distro and version
yum:
name: "{{ packages }}"
state: latest
根据yum
模块Notes
When used with a loop:
each package will be processed individually, it is much more efficient to pass the list directly to the name
option.
而且速度更快,消耗的资源更少。此外,结果集和条件不那么复杂。
所以第一个任务总是被跳过,因为检测到 RedHat 8 应该触发失败模块 运行 但这也被跳过。虽然没有循环也能正常工作。
- name: installing packages
hosts: ansible2
ignore_errors: true
vars_files: varsfile
tasks:
- name: install software based on OS distro and version
yum:
name: "{{ item.name }}"
state: latest
loop: "{{ packages }}"
when: >
( ansible_distribution == "RedHat" and ansible_distribution_major_version is version('12', '>=') )
or
( ansible_distribution == "CentOS" and ansible_distribution_major_version | int >= 8 )
register: result
- fail:
msg: error {{ ansible_hostname }} does not meet minimum requirements
when: result is skipped
关于
So the first task is always skipped because RedHat 8 is detected which should trigger the fail module to run but this gets skipped also.
使用小型测试设置和 debug
task for the variable name to debug
---
- hosts: test.example.com
become: false
gather_facts: true
tasks:
- name: Install software based on OS distro and version
debug:
msg: "{{ ansible_distribution }} {{ ansible_distribution_major_version }}"
register: result
when: ansible_distribution == "RedHat" and ansible_distribution_major_version is version('12', '>=')
with_items: ['pkg1', 'pkg2']
- name: Show result
debug:
var: result
- fail:
msg: "Error: {{ ansible_hostname }} does not meet minimum requirements"
when: result.results[0].skipped
导致输出
TASK [Gathering Facts] ********************************
ok: [test.example.com]
TASK [Show result] ************************************
ok: [test.example.com] =>
result:
changed: false
msg: All items completed
results:
- ansible_loop_var: item
changed: false
item: pkg1
skip_reason: Conditional result was False
skipped: true
...
TASK [fail] *******************************************
fatal: [test.example.com]: FAILED! => changed=false
msg: 'Error: test does not meet minimum requirements'
你可以看到循环会创建一个 list.
- name: Show result
debug:
var: result.results | type_debug
TASK [Show result] ****************
ok: [test.example.com] =>
result.results | type_debug: list
因此您需要将 Conditional 设置为 when: result.results[0].skipped
。
关于
Is works fine without the loop though.
建议使用以下方法简化您的用例
- name: Install software based on OS distro and version
yum:
name: "{{ packages }}"
state: latest
根据yum
模块Notes
When used with a
loop:
each package will be processed individually, it is much more efficient to pass the list directly to thename
option.
而且速度更快,消耗的资源更少。此外,结果集和条件不那么复杂。