I have dictionary-type of data that I want to iterate through and keeping the order is important:
我有字典类型的数据,我想迭代并保持顺序很重要:
with_dict_test:
one: 1
two: 2
three: 3
four: 4
five: 5
six: 6
Now when I write a task to print the keys and values, they are printed in seemingly random order (6, 3, 1, 4, 5, 2).
现在当我编写一个打印键和值的任务时,它们以看似随机的顺序打印(6,3,1,4,5,2)。
---
- name: with_dict test
debug: msg="{{item.key}} --> {{item.value}}"
with_dict: with_dict_test
How can I enforce Ansible to iterate in the given order? Or is there anything better suited than with_dict
? I really need both the key and the value during task execution...
如何强制Ansible按给定顺序迭代?或者有什么比with_dict更适合?在任务执行期间我真的需要密钥和值...
2 个解决方案
#1
7
I don't see an easy way to use dicts as they determine there order from the order of its hashed keys.
You can do the following:
我没有看到使用dicts的简单方法,因为他们根据哈希键的顺序确定顺序。您可以执行以下操作:
with_dict_test:
- { key: 'one', value: 1 }
- { key: 'two', value: 2 }
- { key: 'three', value: 3 }
- { key: 'four', value: 4 }
- { key: 'five', value: 5 }
- { key: 'six', value: 6 }
and in the playbook just replace with_dict
with with_items
:
在playbook中只需用with_items替换with_dict:
---
- name: with_dict test
debug: msg="{{item.key}} --> {{item.value}}"
with_items: with_dict_test
If you find this solution (the declaration of the variable) to ugly, you can do this:
如果你发现这个解决方案(变量的声明)很难看,你可以这样做:
key: ['one', 'two', 'three', 'four', 'five', 'six']
values: [1, 2, 3, 4, 5, 6]
and in the playbook
并在剧本中
---
- name: with_dict test
debug: msg="{{item.0}} --> {{item.1}}"
with_together:
- key
- value
#2
2
I'm not completely sure, but maybe this will help you along the way:
我不完全确定,但也许这会对你有所帮助:
- hosts: localhost
vars:
dict:
one: 1
two: 2
three: 3
sorted: "{{ dict|dictsort(false,'value') }}"
tasks:
- debug:
var: sorted
- debug:
msg: "good {{item.1}}"
with_items: sorted
I'm assuming you can use the Jinja filter to somehow sort on complex values. Another thing you might check out is combining dict.values()|list
and with_sequence
, but anything you milk out of that stone won't exactly scream "maintainable."
我假设您可以使用Jinja过滤器以某种方式对复杂值进行排序。你可以检查的另一件事是结合dict.values()| list和with_sequence,但你从那块石头中挤出的任何东西都不会完全尖叫“可维护”。
#1
7
I don't see an easy way to use dicts as they determine there order from the order of its hashed keys.
You can do the following:
我没有看到使用dicts的简单方法,因为他们根据哈希键的顺序确定顺序。您可以执行以下操作:
with_dict_test:
- { key: 'one', value: 1 }
- { key: 'two', value: 2 }
- { key: 'three', value: 3 }
- { key: 'four', value: 4 }
- { key: 'five', value: 5 }
- { key: 'six', value: 6 }
and in the playbook just replace with_dict
with with_items
:
在playbook中只需用with_items替换with_dict:
---
- name: with_dict test
debug: msg="{{item.key}} --> {{item.value}}"
with_items: with_dict_test
If you find this solution (the declaration of the variable) to ugly, you can do this:
如果你发现这个解决方案(变量的声明)很难看,你可以这样做:
key: ['one', 'two', 'three', 'four', 'five', 'six']
values: [1, 2, 3, 4, 5, 6]
and in the playbook
并在剧本中
---
- name: with_dict test
debug: msg="{{item.0}} --> {{item.1}}"
with_together:
- key
- value
#2
2
I'm not completely sure, but maybe this will help you along the way:
我不完全确定,但也许这会对你有所帮助:
- hosts: localhost
vars:
dict:
one: 1
two: 2
three: 3
sorted: "{{ dict|dictsort(false,'value') }}"
tasks:
- debug:
var: sorted
- debug:
msg: "good {{item.1}}"
with_items: sorted
I'm assuming you can use the Jinja filter to somehow sort on complex values. Another thing you might check out is combining dict.values()|list
and with_sequence
, but anything you milk out of that stone won't exactly scream "maintainable."
我假设您可以使用Jinja过滤器以某种方式对复杂值进行排序。你可以检查的另一件事是结合dict.values()| list和with_sequence,但你从那块石头中挤出的任何东西都不会完全尖叫“可维护”。