ansible - Ansible:如何从列表中连接字典的属性?
问题描述
假设我有这个列表:
hosts:
- {"ip": "1.2.3.4", "hostname": "www.example.com"}
- {"ip": "42.42.42.42", "hostname": "www.wikipedia.com"}
我想获得这样的字符串(将其放入模板中):
'1.2.3.4,42.42.42.42'
它类似于我用 获得的hosts|join(',')
,但我只想连接 的值ip
,所以可能我应该首先获得一个 IP 列表......但是如何?
解决方案
你可以通过hosts
tomap(attribute='ip')
过滤器。
这里有一个 PB 来试试:
---
- hosts: localhost
gather_facts: false
vars:
hosts:
- {"ip": "1.2.3.4", "hostname": "www.example.com"}
- {"ip": "42.42.42.42", "hostname": "www.wikipedia.com"}
tasks:
- debug:
var: hosts | map(attribute='ip') | list | join(',')
干杯
推荐阅读
- python - Heroku 部署问题显示应用程序错误
- javascript - 基于环境(开发/产品)REACT 处理 .env api url 的最佳方法是什么?
- php - 正则表达式 - 如何获取第二个和第三个逗号之间的所有内容?
- java - 无效的项目位置 CardStackView
- asp.net-core-mvc - 用户首次登录显示错误 Null Reference 异常
- javascript - 需要,js不同数量的参数
- c - 32 位比较比 64 位比较快吗?
- c# - 正则表达式地址模式
- amazon-web-services - 第二次运行 AWS Rekognition 人脸搜索
- c# - 如何解决 ASP.NET 中的多个端点匹配问题