按数字排序输出
Ansible sort output by numbers
我正在使用 Ansible 2.9,我想按编号订购 msg 设备,我有这个 msg :
msg: "{{ facts['ansible_facts'] | to_json | from_json | json_query('ansible_net_interfaces[?starts_with(name,`pl2`)].name') | list| sort }}"
我有这个输出:
ok: [localhost] => {
"msg": [
"pl2",
"pl2.10",
"pl2.100",
"pl2.102",
"pl2.11",
"pl2.111" ] ...
如何让它显示这样的输出:
"pl2",
"pl2.10",
"pl2.11",
"pl2.12" ] ...
以及如何捕捉最大的 pl2.* ?例如,如果最大的数字是 pl2.250?
谢谢!
编写自定义过滤器,例如
shell> cat filter_plugins/filter.py
from distutils.version import LooseVersion
def version_sort(l):
return sorted(l, key=LooseVersion)
class FilterModule(object):
''' Ansible filters for operating on versions '''
def filters(self):
return {
'version_sort' : version_sort,
}
给定列表
l:
- pl2
- pl2.10
- pl2.100
- pl2.102
- pl2.11
- pl2.111
排序符合预期
l|version_sort:
- pl2
- pl2.10
- pl2.11
- pl2.100
- pl2.102
- pl2.111
我正在使用 Ansible 2.9,我想按编号订购 msg 设备,我有这个 msg :
msg: "{{ facts['ansible_facts'] | to_json | from_json | json_query('ansible_net_interfaces[?starts_with(name,`pl2`)].name') | list| sort }}"
我有这个输出:
ok: [localhost] => {
"msg": [
"pl2",
"pl2.10",
"pl2.100",
"pl2.102",
"pl2.11",
"pl2.111" ] ...
如何让它显示这样的输出:
"pl2",
"pl2.10",
"pl2.11",
"pl2.12" ] ...
以及如何捕捉最大的 pl2.* ?例如,如果最大的数字是 pl2.250? 谢谢!
编写自定义过滤器,例如
shell> cat filter_plugins/filter.py
from distutils.version import LooseVersion
def version_sort(l):
return sorted(l, key=LooseVersion)
class FilterModule(object):
''' Ansible filters for operating on versions '''
def filters(self):
return {
'version_sort' : version_sort,
}
给定列表
l:
- pl2
- pl2.10
- pl2.100
- pl2.102
- pl2.11
- pl2.111
排序符合预期
l|version_sort:
- pl2
- pl2.10
- pl2.11
- pl2.100
- pl2.102
- pl2.111