{# http://h20628.www2.hp.com/km-ext/kmcsdirect/emr_na-c02586144-1.pdf #} {%- macro increment(dct, key, inc=1) -%} {% if dct.update({key: dct[key] + inc}) %} {% endif %} {%- endmacro -%} {%- import_yaml "netmasks.yaml" as netmasks -%} #!/usr/bin/expect -f spawn telnet {{ pillar['hosts-inet']['mgmt'][hostname] }} expect "Password:" send "{{ switch['password'] }}\r" expect ">" send "system-view\r" expect "]" send "sysname {{ hostname }}\r" expect "]" send "user-interface vty 0 4\r" expect "ui-vty0-4]" send "screen-length 0\r" expect "ui-vty0-4]" send "user privilege level 3\r" expect "ui-vty0-4]" send "set authentication password simple {{ switch['password'] }}\r" expect "ui-vty0-4]" send "quit\r" expect "{{ hostname }}]" send "local-user admin\r" expect -- "-luser-admin]" send "password simple {{ switch['password'] }}\r" expect -- "-luser-admin]" send "quit\r" expect "{{ hostname }}]" {# Enable logging #} send "info-center enable\r" expect "]" send "info-center loghost {{logging}} channel loghost facility local6\r" expect "]" send "info-center source default channel loghost log level informational\r" expect "]" {%- for name, vlan in pillar['vlans'].items() %} send "vlan {{ vlan }}\r" expect -- "-vlan{{ vlan }}]" send "name {{ name }}\r" expect -- "-vlan{{ vlan }}]" {%- if name == 'mgmt' %} {# Actually only used for mgmt_vlan, switches are not routers #} send "interface Vlan-interface {{ vlan }}\r" expect "]" {%- set net_hosts = pillar['hosts-inet'].get(name) %} {%- set ipaddr = net_hosts and net_hosts.get(hostname) %} {%- if ipaddr %} send "ip address {{ ipaddr }} {{ netmasks[pillar['subnets-inet'][name].split('/')[1]] }}\r" expect "]" {%- endif %} {%- endif %} send "quit\r" expect "{{ hostname }}]" {%- endfor %} {%- for name, conf in switch['ports'].items() %} {%- if conf['mode'] == 'trunk' or conf['mode'] == 'bond' %} {%- if conf['mode'] == 'bond' %} send "link-aggregation group {{ conf['group'] }} mode static\r" expect { "This aggregation will be modified to static mode. Continue ?" { send "Y\r" } "]" {} } send "link-aggregation group {{ conf['group'] }} description {{name}}\r" expect "]" {%- endif %} {%- for port in conf['ports'] %} send "interface {{ port }}\r" expect "]" send "undo stp edged-port\r" expect "]" {%- if conf['mode'] == 'bond' %} send "lacp enable\r" expect "]" send "undo port link-aggregation group\r" expect "]" send "port link-aggregation group {{ conf['group'] }}\r" {%- else %} send "undo lacp enable\r" {%- endif %} expect "]" send "jumboframe enable\r" expect "]" {%- if conf.get('vlans') %} send "port link-type trunk\r" expect "]" # Set dummy default vlan send "port trunk pvid vlan 4094\r" expect "]" # Deconfigure all but mgmt vlan send "undo port trunk permit vlan 2 to 4094\r" expect "]" {%- for vlan_name in conf['vlans'] %} send "port trunk permit vlan {{ pillar['vlans'][vlan_name] }}\r" expect "]" {%- endfor %} {%- else %} send "port link-type access\r" expect "]" send "port access vlan {{ pillar['vlans'][conf['access']] }}\r" expect "]" {%- endif %} send "quit\r" expect "{{ hostname }}]" {%- endfor %} {%- elif conf['mode'] == 'access' %} {%- for port in conf['ports'] %} send "interface {{ port }}\r" expect "]" send "undo port link-aggregation group\r" expect "]" send "port link-type access\r" expect "]" {%- if name == 'mgmt' %} send "undo port access vlan\r" expect "]" {%- else %} send "port access vlan {{ pillar['vlans'][name] }}\r" expect "]" {%- endif %} send "quit\r" expect "{{ hostname }}]" {%- endfor %} {%- endif %} {%- endfor %} send "save main\r" expect "Y/N]" send "YES\r" expect "press the enter key):" send "\r" expect "]" send "quit\r" expect ">" send "quit\r"