[英]How to loop through a list of dictionary, and print out every key and value pair in Ansible
I have an list of dictionary in Ansible config我在 Ansible 配置中有一个字典列表
myList
- name: Bob
age: 25
- name: Alice
age: 18
address: USA
I write code as我把代码写成
- name: loop through
debug: msg ="{{item.key}}:{{item.value}}"
with_items: "{{ myList }}"
I want to print out like我想打印出来
msg: "name:Bob age:25 name:Alice age:18 address:USA"
How can I loop through this dictionary and get key and value pairs?如何遍历此字典并获取键值对? Because it don't know what is the key.因为它不知道什么是关键。 If I change as {{ item.name }}, ansible will work, but I also want to know key如果我更改为 {{ item.name }},ansible 将起作用,但我也想知道 key
If you want to loop through the list and parse every item separately:如果要遍历列表并分别解析每个项目:
- debug: msg="{{ item | dictsort | map('join',':') | join(' ') }}"
with_items: "{{ myList }}"
Will print:将打印:
"msg": "age:25 name:Bob"
"msg": "address:USA age:18 name:Alice"
If you want to join everything into one line, use:如果要将所有内容合并为一行,请使用:
- debug: msg="{{ myList | map('dictsort') | sum(start=[]) | map('join',':') | join(' ') }}"
This will give:这将给出:
"msg": "age:25 name:Bob address:USA age:18 name:Alice"
Keep in mind that dicts are not sorted in Python, so you generally can't expect your items to be in the same order as in yaml-file.请记住,dicts 不在 Python 中排序,因此您通常不能期望您的项目与 yaml 文件中的顺序相同。 In my example, they are sorted by key name after dictsort filter.在我的示例中,它们在 dictsort 过滤器之后按键名排序。
Here is your text: myList - name: Bob age: 25 - name: Alice age: 18 address: USA这是您的文本:myList - 姓名:鲍勃年龄:25 - 姓名:爱丽丝年龄:18 地址:美国
Here is the Answer:这是答案:
text='''myList - name: Bob age: 25 - name: Alice age: 18 address: USA''' text='''myList - 姓名:鲍勃年龄:25 - 姓名:爱丽丝年龄:18 地址:美国'''
>>> final_text=''
for line in text.split('\\n'): line1=line.replace(' ','').replace('-','') if 'myList' not in line1: final_text+=line1+' '对于 text.split('\\n') 中的行: line1=line.replace(' ','').replace('-','') 如果 'myList' 不在 line1 中: final_text+=line1+' '
final_text 'name:Bob age:25 name:Alice age:18 address:USA ' final_text '姓名:鲍勃年龄:25 姓名:爱丽丝年龄:18 地址:美国'
class Person(object):
def __init__(self, name, age, address):
self.name = name
self.age = age
self.address = address
def __str__(self):
# String Representation of your Data.
return "name:%s age:%d address:%s" % (self.name, self.age, self.address)
then you can have a list of objects you created.然后你可以有一个你创建的对象列表。 I am using some sample data here to create a list of dicts.我在这里使用一些示例数据来创建一个字典列表。
dict={"name":"Hello World", "age":23, "address":"Test address"}
myList = [dict,dict,dict]
objList = []
for row in myList:
objList.append(str(Person(**row)))
result = ' '.join(objList)
print(result)
It ends out printing: name:Hello World age:23 address:Test address name:Hello World age:23 address:Test address name:Hello World age:23 address:Test address最终打印:姓名:Hello World 年龄:23 地址:测试地址名称:Hello World 年龄:23 地址:测试地址名称:Hello World 年龄:23 地址:测试地址
I was going to do REPR but that is used a bit more differently, and i thought this might be better suited for your needs.我打算做 REPR,但它的使用方式有点不同,我认为这可能更适合您的需求。
If you want to keep it really simple you can do this:如果你想保持它非常简单,你可以这样做:
dict={"name":"Hello World", "age":23, "address":"Test address"}
myList = [dict,dict,dict]
objList = []
for row in myList;
tmp = []
for k in row:
tmp.append("%s:%s" % (k, row[k]))
objList.append(" ".join(tmp))
print(" ".join(objList))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.