ansible - Ansible:从列表中获取除输入之外的其他数字
问题描述
我有一组三个数字:19、20 和 21,我需要得到其中一个,除了输入的那个。我的方法是使用 jinja-difference 过滤器,它工作正常,但我不知道如何在差异中用变量(在我的示例中为“hostnumber”)替换硬编码数字(在我的示例中为“19”) -文件管理器。
例子:
ansible-playbook get_other_host.yml -e "hostname=abcd0015"
---
- hosts: all
gather_facts: false
tasks:
- name: Extract the hostnumber
set_fact:
hostnumber: "{{ (hostname[6:] | int) + 4 }}"
# Output is: 19
- name: The hostnumber is
debug:
msg: "{{ hostnumber }}"
# Output is: 20 and 21
- name: The other hosts are
debug:
msg: "{{ [19,20,21] | difference([19]) }}"
# Output is: 20 or 21
- name: One other host is
debug:
msg: "{{ [19,20,21] | difference([19]) | random }}"
解决方案
Jinja2 可以访问所有变量,包括hostnumber
之前设置的变量;它可能已经是int
,但是| int
在已经是的变量上使用int
是无害的
- debug:
msg: "{{ [19,20,21] | difference([hostnumber | int]) }}"
推荐阅读
- amazon-web-services - Amazon AWS Athena HIVE_CANNOT_OPEN_SPLIT:打开 Hive 拆分时出错/Parquet 文件无效,parquet 文件使用 Athena 压缩为 gzip
- python - pycharm 无法使用 python3.8 连接到控制台
- ios - iOS 13 后台杀
- azure - 部署到 Azure 后无法将源内容类型 Umbraco.Web.PublishedCache.NuCache.PublishedContent 绑定到模型类型
- python - 使用 Python 向 Microsoft Teams 发送自动消息
- forms - 表单中的 PowerShell 弹出窗口
- inno-setup - Inno Setup:强制组件始终具有与其父级相同的检查状态
- redux - 如何通过键名删除 redux 存储
- tensorflow - 我的 tensorflow 2.0 自定义模型没有收到我期望的形状或值
- image - 图片库保护程序锁主线程flutter