List VLAN number and based on the last number create new VLAN

11 views
Skip to first unread message

czopereq76

unread,
Feb 6, 2017, 9:41:47 AM2/6/17
to Ansible Project
Hi All,

I am trying to add VLAN according to the last number available on the switch. In my case right now the last VLAN number is 100 so I would like to add next one with number 101. However running my playbook it looks like iteration over VLAN list is not in order and playbook created VLAN 15 instead of 101.

I would like to run iteration in order from 1 to 100 and then create VLAN 101. Does any one know how to achieve this?




---
- 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                      : ok=6    changed=1    unreachable=0    failed=0  

                 

Kai Stian Olstad

unread,
Feb 6, 2017, 11:14:33 AM2/6/17
to ansible...@googlegroups.com
On 04. feb. 2017 20:27, czopereq76 wrote:
> I am trying to add VLAN according to the last number available on the
> switch. In my case right now the last VLAN number is 100 so I would like to
> add next one with number 101. However running my playbook it looks like
> iteration over VLAN list is not in order and playbook created VLAN 15
> instead of 101.
>
> ---
> - 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: VLAN NUMBER
set_fact:
vlan_index: {{ sh_vlan_output.stdout[0].vlans.keys() | map('int') |
list | max + 1 }}

This should do what you are looking for.

--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages