access: move templates to netbox
And adjust tasks to work with FS switches also.
This commit is contained in:
		
							parent
							
								
									be398e54fe
								
							
						
					
					
						commit
						0802dc8637
					
				
					 3 changed files with 18 additions and 72 deletions
				
			
		| 
						 | 
				
			
			@ -1,6 +1,10 @@
 | 
			
		|||
- name: write config
 | 
			
		||||
  ansible.netcommon.cli_command:
 | 
			
		||||
    command: "copy running-config startup-config"
 | 
			
		||||
    prompt: "Destination filename startup-config\\? \\[y/n\\]: "
 | 
			
		||||
    answer: "y"
 | 
			
		||||
    prompt:
 | 
			
		||||
      - "Destination filename startup-config\\? \\[y/n\\]: " # d-link
 | 
			
		||||
      - "Do you want to overwrite \\[/data/config.text\\]\\? \\[Y/N\\]:" # fs
 | 
			
		||||
    answer:
 | 
			
		||||
      - "y"
 | 
			
		||||
      - "y"
 | 
			
		||||
  when: "'handler' not in ansible_skip_tags"
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,7 +1,17 @@
 | 
			
		|||
# This should be provided by the netbox inventory plugin but isn’t yet.
 | 
			
		||||
# https://github.com/netbox-community/ansible_modules/issues/1007
 | 
			
		||||
- name: Fetch configuration from netbox
 | 
			
		||||
  uri:
 | 
			
		||||
    url: '{{ interfaces[0].device.url }}render-config/' # why URL can only be accessed through interface data is a mystery
 | 
			
		||||
    method: POST
 | 
			
		||||
    headers:
 | 
			
		||||
      Authorization: 'Token {{ lookup("env", "NETBOX_API_TOKEN") }}'
 | 
			
		||||
  register: config
 | 
			
		||||
 | 
			
		||||
- name: Set configuration
 | 
			
		||||
  ansible.netcommon.cli_config:
 | 
			
		||||
    config: "{{ lookup('template', 'config.j2') }}"
 | 
			
		||||
    config: '{{ config.json.content }}'
 | 
			
		||||
  register: result
 | 
			
		||||
  # These lines are not displayed by 'sho ru' and always reported as different, so ignore them.
 | 
			
		||||
  changed_when: result.commands | reject('match', '^(no shutdown|no switchport access vlan|switchport mode hybrid|interface .*)$')
 | 
			
		||||
  changed_when: result.commands | reject('match', '^(no shutdown|no switchport access vlan|switchport mode access|switchport mode hybrid|interface .*)$')
 | 
			
		||||
  notify: write config
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,68 +0,0 @@
 | 
			
		|||
terminal length default 0
 | 
			
		||||
!
 | 
			
		||||
line console
 | 
			
		||||
!
 | 
			
		||||
line telnet
 | 
			
		||||
!
 | 
			
		||||
line ssh
 | 
			
		||||
!
 | 
			
		||||
port-channel load-balance src-dst-ip
 | 
			
		||||
!
 | 
			
		||||
ip ssh server
 | 
			
		||||
ssh user admin authentication-method publickey /c:/ansible.pub
 | 
			
		||||
!
 | 
			
		||||
vlan {{ vlans | map(attribute='vid') | vlan_parser | join(',') }}
 | 
			
		||||
!
 | 
			
		||||
{% for iface in interfaces | selectattr('enabled') | selectattr('lag') %}
 | 
			
		||||
interface {{ iface.name }}
 | 
			
		||||
 channel-group 1 mode active
 | 
			
		||||
!
 | 
			
		||||
{% endfor %}
 | 
			
		||||
{% set mgmt = namespace(ip=false, gw=false) %}
 | 
			
		||||
{% for iface in interfaces | rejectattr('lag') %}
 | 
			
		||||
interface {{ iface.name }}
 | 
			
		||||
{% if iface.enabled and iface.mode %}
 | 
			
		||||
{% if iface.type.value != 'lag' %}
 | 
			
		||||
 no shutdown
 | 
			
		||||
{% endif %}
 | 
			
		||||
{% if iface.mgmt_only and iface.ip_addresses %}
 | 
			
		||||
{% set mgmt.ip = iface.ip_addresses[0].address %}
 | 
			
		||||
{% if iface.custom_fields.gateway %}
 | 
			
		||||
{% set mgmt.gw = iface.custom_fields.gateway.address %}
 | 
			
		||||
{% endif %}
 | 
			
		||||
{% endif %}
 | 
			
		||||
{% if iface.mode.value == 'access' %}
 | 
			
		||||
 switchport mode access
 | 
			
		||||
{% if iface.untagged_vlan and iface.untagged_vlan.vid != 1 %}
 | 
			
		||||
 switchport access vlan {{ iface.untagged_vlan.vid }}
 | 
			
		||||
{% else %}
 | 
			
		||||
 no switchport access vlan
 | 
			
		||||
{% endif %}
 | 
			
		||||
{% elif iface.mode.value == 'tagged' %}
 | 
			
		||||
 switchport mode trunk
 | 
			
		||||
 switchport trunk allowed vlan {{ (iface.tagged_vlans or vlans) | map(attribute='vid') | vlan_parser | join(',') }}
 | 
			
		||||
{% endif %}
 | 
			
		||||
{% else %}
 | 
			
		||||
 shutdown
 | 
			
		||||
{% endif %}
 | 
			
		||||
!
 | 
			
		||||
{% endfor %}
 | 
			
		||||
{% if mgmt.ip %}
 | 
			
		||||
interface Vlan1
 | 
			
		||||
 ip address {{ mgmt.ip | ipaddr('address') }} {{ mgmt.ip | ipaddr('netmask') }}
 | 
			
		||||
{% endif %}
 | 
			
		||||
!
 | 
			
		||||
sntp enable
 | 
			
		||||
{% for address in ntp %}
 | 
			
		||||
sntp server {{ address }}
 | 
			
		||||
{% endfor %}
 | 
			
		||||
!
 | 
			
		||||
ntp access-group default nomodify noquery
 | 
			
		||||
!
 | 
			
		||||
{% if mgmt.gw %}
 | 
			
		||||
ip route 0.0.0.0 0.0.0.0 {{ mgmt.gw | ipaddr('address') }} primary
 | 
			
		||||
{% endif %}
 | 
			
		||||
!
 | 
			
		||||
no ddp
 | 
			
		||||
!
 | 
			
		||||
end
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue