Giriş: Kod Yazmadan Avtomatlaşdırma

Ansible şəbəkə cihazlarında serverlərdən fərqli işləyir: cihazda Python yoxdur, ona görə modullar control node-da işləyir və cihaza SSH (network_cli) və ya API (netconf, httpapi) ilə qoşulur. Nəticə — Python bilməyən şəbəkə mühəndisi də YAML ilə yüzlərlə cihazı idarə edə bilir.

PlatformaCollectionansible_network_os
Cisco IOS / IOS XEcisco.ioscisco.ios.ios
Cisco NX-OScisco.nxoscisco.nxos.nxos
Arista EOSarista.eosarista.eos.eos
Juniper Junosjunipernetworks.junosjunipernetworks.junos.junos
FortiGatefortinet.fortiosfortinet.fortios.fortios (httpapi)

1. Hazırlıq

pip install ansible ansible-pylibssh
ansible-galaxy collection install cisco.ios

# inventory.yml
all:
  children:
    routers:
      hosts:
        EDGE-R1: { ansible_host: 10.10.0.254 }
    switches:
      hosts:
        CORE-SW1: { ansible_host: 10.10.0.1 }
        ACC-SW1:  { ansible_host: 10.10.0.11 }
  vars:
    ansible_connection: ansible.netcommon.network_cli
    ansible_network_os: cisco.ios.ios
    ansible_user: netadmin
    ansible_password: "{{ vault_net_password }}"
    ansible_become: true
    ansible_become_method: enable

Parol group_vars/all/vault.yml faylında Ansible Vault ilə şifrələnir, playbook --ask-vault-pass ilə işlədilir.

2. Facts və Show Əmrləri

# inventory_report.yml
- name: Cihaz inventarı
  hosts: all
  gather_facts: false
  tasks:
    - name: Facts topla
      cisco.ios.ios_facts:
        gather_subset: min

    - name: Versiya çap et
      ansible.builtin.debug:
        msg: "{{ inventory_hostname }}: {{ ansible_net_model }} / {{ ansible_net_version }} / {{ ansible_net_serialnum }}"

    - name: OSPF qonşuları
      cisco.ios.ios_command:
        commands: show ip ospf neighbor
      register: ospf

    - ansible.builtin.debug:
        var: ospf.stdout_lines[0]

3. Konfiqurasiya: ios_config və Resource Modullar

- name: Standart xidmətlər
  hosts: all
  gather_facts: false
  tasks:
    - name: NTP və logging
      cisco.ios.ios_config:
        lines:
          - ntp server 10.0.0.10
          - logging host 10.0.0.50
        save_when: modified          # yalnız dəyişiklik olanda write memory

    - name: ACL — ardıcıllıq vacibdir
      cisco.ios.ios_config:
        parents: ip access-list standard MGMT
        lines:
          - permit 10.0.0.0 0.0.0.255
          - deny any log
        before: no ip access-list standard MGMT
        match: exact

4. Konfiqurasiya Backup-ı

- name: Gecəlik backup
  hosts: all
  gather_facts: false
  tasks:
    - cisco.ios.ios_config:
        backup: true
        backup_options:
          dir_path: ./backups
          filename: "{{ inventory_hostname }}.cfg"

# cron / CI: hər gecə
ansible-playbook -i inventory.yml backup.yml --vault-password-file ~/.vault_pass
cd backups && git add -A && git commit -m "backup $(date +%F)" || true

5. Uyğunluq Yoxlaması

- name: Təhlükəsizlik standartı
  hosts: all
  gather_facts: false
  tasks:
    - cisco.ios.ios_command:
        commands: show running-config
      register: run

    - name: Telnet bağlı olmalıdır
      ansible.builtin.assert:
        that: "'transport input ssh' in run.stdout[0]"
        fail_msg: "{{ inventory_hostname }}: VTY-də yalnız SSH olmalıdır"
      ignore_errors: true

    - name: Parollar şifrələnməlidir
      ansible.builtin.assert:
        that: "'service password-encryption' in run.stdout[0]"
      ignore_errors: true

Bu yoxlamaları --check rejimli konfiqurasiya playbook-u ilə birləşdirsəniz, hansı cihazın standartdan kənara çıxdığını (drift) hər gün avtomatik görərsiniz.