ansible - 如何从 Ansible 的输出中查找缺失的数字
问题描述
我的播放列表包含以下内容
- set_fact:
missnu={{out.stdout_lines}}
register: missnu
- debug: msg={{missnu}}
我得到低于输出,
TASK [debug] **********************
ok: [192.168.0.10] => {
"msg": [
"1 2 3 4"
]
}
这是预期的结果,但是我想找到其中缺少的数字。意思是如果结果是"1 2 4"
OR"2 3 4"
或者"2 4"
我将如何得到那些缺失的数字?
解决方案
您正在从某个命令打印标准输出,但我假设您知道预期结果并将其存储在某个变量中。为此,我会使用difference
过滤器。
- hosts: localhost
vars:
sample_out_ml: [ 1, 2, 3, 4 ]
sample_out_sl: "1 2 3 4"
expected: [ 1, 2, 3 ,4 ,5 ]
tasks:
- name: Show difference when cmd outputs multi-line
debug:
msg: "{{ expected | difference(sample_out_ml) }}"
- name: Show difference when cmd outputs on single line
debug:
msg: "{{ expected | difference(sample_out_sl.split(' ') | map('int') ) }}"
编辑根据您的评论,我提供了一个更强大的示例来说明您如何执行此操作。如果你要全神贯注于 Ansible,你应该尽量避免使用这些command
模块。希望这可以帮助。
推荐阅读
- c# - C# 中的 Excel 条件格式规则 - 检查不同列中的单元格是否具有特定值
- sql-server - 在已排序的列上合并联接而不是哈希联接
- python - 如何使用 Scrapy 抓取下一页
- java - 何时关闭 DL4J INDArrays
- ios - Xamarin.Forms 应用程序在调试中工作,但在 ios 上发布时崩溃
- javascript - Widget iOS 14 使用 swift ui 创建带有 react native 的 ui
- python - 需要在 python pandas 中填充下一个空单元格
- audio - Gstreamer 复制 2 声道音频
- javascript - 如何在不使用 id 或类的情况下选择特定元素
- python-3.x - 尝试使用服务帐户读取 GMAIL 时出现错误代码 400