我想根据最新的现有VLAN数创建新的VLAN。例如,我在eos上的最后一个VLAN号码是100,所以我想运行我的Playbook并创建VLAN 101.但是当我运行我的Playbook时,看起来set_fact迭代不是按顺序而且为我创建了playbook而不是VLAN 15。
即使我使用的是排序过滤器,它看起来像是无法按顺序进行迭代。有谁知道怎么做?
我的剧本
- name: CHECK VLAN
hosts: eos
connection: local
tasks:
- name: CHECK FOR VLAN NUMBERS
eos_command:
commands: show vlan | json
provider: "{{ cli }}"
ignore_errors: yes
register: sh_vlan_output
- debug: var=sh_vlan_output.stdout[0].vlans.keys()
- name: SET VLAN NUMBER
set_fact:
vlan_index: "{{ item | int +1 }}"
with_items:
- "{{ sh_vlan_output.stdout[0].vlans.keys() | sort }}"
- debug: var=vlan_index
- name: CREATE VLAN
eos_config:
lines:
- vlan {{ vlan_index }}
provider: "{{ cli }}"
authorize: yes
PLAY [CHECK VLAN] **************************************************************
TASK [setup] *******************************************************************
ok: [eos-1]
TASK [CHECK FOR VLAN NUMBERS] **************************************************
ok: [eos-1]
TASK [debug] *******************************************************************
ok: [eos-1] => {
"sh_vlan_output.stdout[0].vlans.keys()": [
"11",
"10",
"13",
"12",
"14",
"1",
"100"
]
}
TASK [SET VLAN NUMBER] *********************************************************
ok: [eos-1] => (item=10)
ok: [eos-1] => (item=1)
ok: [eos-1] => (item=100)
ok: [eos-1] => (item=11)
ok: [eos-1] => (item=12)
ok: [eos-1] => (item=13)
ok: [eos-1] => (item=14)
TASK [debug] *******************************************************************
ok: [eos-1] => {
"vlan_index": "15"
}
TASK [CREATE VLAN] *************************************************************
changed: [eos-1]
PLAY RECAP *********************************************************************
eos-1
答案 0 :(得分:0)
您可以使用以下jinja表达式获取max + 1
:
{{ sh_vlan_output.stdout[0].vlans.keys() | map('int') | max + 1 }}