Ontwikkel een Ansible Playbook voor Geautomatiseerde Beheer van Netwerkapparatuur
description
Met deze prompt kan de gebruiker een gespecialiseerd Ansible playbook ontwikkelen dat netwerkapparatuur effectief automatiseert, wat tijd bespaart en menselijke fouten vermindert. Het helpt bij het structureren van configuraties, het toepassen van beveiligingsmaatregelen en het implementeren van rollback-mechanismen, wat essentieel is voor robuust netwerkbeheer. Dit is uniek omdat het zich richt op netwerkapparatuur, een gebied dat in bestaande prompts niet direct wordt behandeld.
prompt
Help me een Ansible playbook te ontwerpen dat netwerkapparatuur zoals switches, routers en firewalls automatisch configureert en beheert. Mijn netwerkapparatuur merken en modellen: <voer de merken en modellen van netwerkapparatuur in>
Configuratie taken: <beschrijf de configuratietaken die geautomatiseerd moete ...
try_prompt
generate_helper
disclaimerOnPageApi
tags
ai_answers
provider: openai
model: gpt-4.1-nano
Natuurlijk! Hier is een overzichtelijk advies voor het structureren van je Ansible playbook, het beheer van variabelen en foutafhandeling voor jouw netwerkapparatuur.
1. Structuur van het Playbook
Gebruik een gestructureerde directory-indeling:
playbooks/
├── site.yml
├── group_vars/
│ ├── cisco.yml
│ └── juniper.yml
├── roles/
│ ├── cisco/
│ │ ├── tasks/
│ │ │ ├── main.yml
│ │ │ ├── configure.yml
│ │ │ └── rollback.yml
│ │ ├── vars/
│ │ │ └── main.yml
│ │ └── templates/
│ └── juniper/
│ ├── tasks/
│ │ ├── main.yml
│ │ ├── configure.yml
│ │ └── rollback.yml
│ ├── vars/
│ │ └── main.yml
│ └── templates/
2. Beheer van Variabelen
Gebruik group_vars om specifieke configuratievariabelen per apparaatgroep:
- Bijvoorbeeld, in group_vars/cisco.yml:
ansible_network_os: cisco
ssh_password: "JouwWachtwoord"
enable_password: "JouwEnableWachtwoord"
dot1x_profile_name: "AuthProfile"
- In group_vars/juniper.yml:
ansible_network_os: juniper
ssh_password: "JouwWachtwoord"
authentication_profile: "AuthProfile"
Deze variabelen worden in je roltemplates en taken gebruikt om dynamische configuraties te genereren.
3. Foutafhandeling en Rollback Strategie
Gebruik Ansible's 'block', 'rescue' en 'always' om foutafhandeling te structureren:
- In je rol voor elke configuratie:
tasks:
- name: Voer configuratie uit op apparaat
block:
- name: Pas configuratie toe
# Gebruik juiste modules zoals ios_config of junos_config
ios_config:
lines:
- interface GigabitEthernet1/0/1
- switchport mode access
- authentication port-control auto
register: config_result
ignore_errors: yes
- name: Controleer op fouten
fail:
msg: "Configuratie mislukt, roll back wordt gestart."
when: config_result.failed
rescue:
- name: Voer rollback uit
include_tasks: rollback.yml
- name: Stuur e-mail notificatie over fout
mail:
host: smtp.jouwdomein.com
port: 587
username: jouw_email@jouwdomein.com
password: jouw_mail_wachtwoord
to: admin@jouwdomein.com
subject: "Fout bij netwerkconfiguratie"
body: "Er is een fout opgetreden tijdens de configuratie van {{ inventory_hostname }}. Rollback is uitgevoerd."
always:
- name: Log uitkomst
debug:
msg: "Configuratieproces voltooid voor {{ inventory_hostname }}."
- Rollback taken in rollback.yml:
- name: Herstel vorige configuratie
ios_config:
src: "backup/config_backup_{{ inventory_hostname }}.cfg"
replace: line
match: '^!'
when: backup_exists.stat.exists
4. Overige Tips
- Gebruik 'ansible-vault' om gevoelige gegevens zoals wachtwoorden veilig te bewaren.
- Implementeer versiebeheer van configuratie-backups.
- Test je playbook eerst in een testomgeving.
- Documenteer je playbook en variabelen voor onderhoudbaarheid.
Door deze structuur te volgen, zorg je voor een flexibel, veilig en betrouwbaar netwerkbeheer met automatische configuratie en foutafhandeling.
provider: deepseek
model: deepseek-chat
Hier is een gestructureerd advies voor uw Ansible playbook voor netwerkautomatisering:
## 1. Playbook Structuur
```
inventory/
production/
host_vars/
switch-01.yml
switch-02.yml
group_vars/
cisco.yml
juniper.yml
all.yml
roles/
base_config/
tasks/main.yml
handlers/main.yml
templates/
security/
tasks/main.yml
templates/
backup/
tasks/main.yml
monitoring/
tasks/main.yml
playbooks/
deploy_network.yml
rollback_config.yml
files/
scripts/
certificates/
templates/
cisco/
juniper/
```
## 2. Hoofd Playbook (deploy_network.yml)
```yaml
---
- name: Netwerkapparatuur configuratie en beheer
hosts: network_devices
gather_facts: no
connection: network_cli
vars:
ansible_network_os: "{{ network_os }}"
rollback_timeout: 300
email_notification: "admin@uwbedrijf.nl"
tasks:
- name: Configuratie backup maken voor rollback
include_role:
name: backup
tags: backup
- name: Basisconfiguratie toepassen
include_role:
name: base_config
tags: base
- name: Beveiligingsconfiguratie toepassen
include_role:
name: security
tags: security
- name: Configuratie valideren
include_role:
name: monitoring
tags: validation
```
## 3. Variabelen Beheer
**group_vars/cisco.yml:**
```yaml
network_os: ios
ssh_config:
version: 2
timeout: 60
authentication_retries: 3
aaa_config:
new_model: true
authentication: "dot1x"
authorization: "network"
accounting: "system"
dot1x_config:
radius_servers:
- host: "10.1.1.10"
key: "{{ radius_key }}"
- host: "10.1.1.11"
key: "{{ radius_key }}"
```
**group_vars/juniper.yml:**
```yaml
network_os: junos
ssh_config:
root_login: deny
connection_limit: 10
protocol_version: v2
dot1x_config:
radius_servers:
- host: "10.1.1.10"
secret: "{{ radius_key }}"
```
## 4. Rollen Implementatie
**roles/security/tasks/main.yml:**
```yaml
- name: SSH-only toegang configureren
block:
- name: SSH configureren voor Cisco
cisco.ios.ios_config:
lines:
- "ip ssh version 2"
- "ip ssh time-out 60"
- "ip ssh authentication-retries 3"
- "transport input ssh"
when: network_os == "ios"
- name: SSH configureren voor Juniper
junipernetworks.junos.junos_config:
lines:
- "set system services ssh"
- "set system services telnet no"
when: network_os == "junos"
rescue:
- name: Rollback bij SSH fout
include_tasks: ../backup/tasks/rollback.yml
```
**roles/security/tasks/dot1x.yml:**
```yaml
- name: 802.1X authenticatie configureren
block:
- name: RADIUS servers configureren
cisco.ios.ios_config:
lines:
- "radius server {{ item.host }}"
- "address ipv4 {{ item.host }} auth-port 1812 acct-port 1813"
- "key {{ item.key }}"
loop: "{{ dot1x_config.radius_servers }}"
when: network_os == "ios"
- name: 802.1X globaal inschakelen
cisco.ios.ios_config:
lines:
- "dot1x system-auth-control"
when: network_os == "ios"
```
## 5. Rollback Strategie
**roles/backup/tasks/main.yml:**
```yaml
- name: Huidige configuratie backup maken
cisco.ios.ios_config:
backup: yes
backup_options:
filename: "{{ inventory_hostname }}_{{ ansible_date_time.epoch }}.cfg"
when: network_os == "ios"
- name: Juniper configuratie rescue maken
junipernetworks.junos.junos_rescue:
save: true
when: network_os == "junos"
```
**roles/backup/tasks/rollback.yml:**
```yaml
- name: Rollback configuratie
cisco.ios.ios_config:
replace: config
src: "{{ backup_path }}/{{ inventory_hostname }}_backup.cfg"
when: network_os == "ios"
- name: Rollback Juniper configuratie
junipernetworks.junos.junos_rescue:
load: rescue
when: network_os == "junos"
- name: E-mail notificatie sturen
community.general.mail:
host: "smtp.uwbedrijf.nl"
port: 587
username: "ansible@uwbedrijf.nl"
password: "{{ smtp_password }}"
to: "{{ email_notification }}"
subject: "Configuratiefout - Rollback uitgevoerd op {{ inventory_hostname }}"
body: |
Er is een fout opgetreden tijdens configuratie van {{ inventory_hostname }}.
Rollback is automatisch uitgevoerd naar de vorige werkende configuratie.
Foutdetails: {{ ansible_failed_result }}
```
## 6. Foutafhandeling
```yaml
- name: Configuratie valideren
block:
- name: SSH connectiviteit testen
wait_for:
host: "{{ ansible_host }}"
port: 22
timeout: 30
- name: Configuratie verschillen controleren
cisco.ios.ios_config:
diff_against: intended
intended_config: "{{ lookup('template', 'intended_config.j2') }}"
rescue:
- name: Fout loggen
debug:
msg: "Configuratie validatie gefaald voor {{ inventory_hostname }}"
- name: Rollback uitvoeren
include_role:
name: backup
tasks_from: rollback.yml
- name: Playbook stoppen bij kritieke fout
meta: end_host
```
## 7. Aanbevelingen
1. **Gebruik Ansible Vault** voor gevoelige data:
```bash
ansible-vault encrypt group_vars/all/vault.yml
```
2. **Test altijd in lab-omgeving** voor productie implementatie
3. **Implementeer change control** processen
4. **Monitor configuratie compliance** met Ansible Tower/AWX
5. **Gebruik tags** voor specifieke taken:
```bash
ansible-playbook deploy_network.yml --tags "security,dot1x"
```
Dit ontwerp biedt een robuuste basis voor uw netwerkautomatisering met ingebouwde beveiliging en foutafhandeling.