在不同的网络设备型号上使用Ansible

目标

用ansible来管理不同型号的网络设备,例如Cisco、Aruba和HPE Comware。
由于不同型号的设备需要执行不同的命令,因此当通过SSH登录到网络设备并输入命令时,需要注意设备型号和命令的语法。通过使用ansible,可以实现无需考虑设备型号即可进行配置。
通过在ServiceNow上批准变更后执行操作,可以实现在ServiceNow上管理和配置网络设备的功能。

这篇文章的内容

本文将创建一个使用Ansible Playbook在不同的设备上登录并获取版本信息的操作。

前提 (Qian ti)

    • スイッチで実行する基本的なコマンド

 

    • ansible及びansible-playbookの基本的に知識

 

    • ansible環境。本記事の例ではansible-vaultを利用しています

 

    機器名をDNSに登録(任意)されていない場合はIPアドレスを利用する

目录结构


~/ansible # ansible playbook用ディレクトリ
  |- network # ネットワーク機器用ディレクトリ
       |- show_version.yml # "show version"用のplaybook
       |- roles # ansible roles
            |- show_version # "show version"用roleディレクトリ
                 |- tasks # "show version"用実行ansibleコマンド用ディレクトリ
                     |- main.yml # "show version"コマンド用のansible-playbook用roleスクリプト
                     |- cisco.ios.ios.yml # cisco用スクリプト
                     |- aruba.yml # aruba用スクリプト
                     |- comware.yml # HPE Comware用スクリプト
                 |- vars # "show version"用roleの変数
                     |- main.yml # 変数定義ファイル
~/scripts # bash用スクリプトディレクトリ
  |- network # ネットワーク機器用スクリプトディレクトリ
        |- show_version.sh # "show version"用のbashスクリプト
/etc/ansible # ansible設定ファイルディレクトリ
  |- hosts # ansible用インベントリファイル
  |- group_vars 
       |- all
            |- vault.yml # ansible-vaultの暗号化ユーザ名/パスワードファイル

使用方法

可以直接运行ansible-playbook,但为了方便那些不太熟悉ansible的人,我创建了一个bash脚本。语法如下所示。

./show_version.sh -h
Usage: show_version.sh [-n ホスト名] [-h ヘルプ]
./show_version.sh -ncisco-switch-01

hosts文件

请在/etc/ansible目录下创建一个名为hosts的文件,内容如下:”aruba_user”,”aruba_password”等是在ansible-vault文件vault.yml中定义的用户名/密码。还可以通过ServiceNow动态获取设备信息。


[aruba]
aruba-switch-01 ansible_host=192.168.0.1
aruba-switch-02 ansible_host=192.168.0.2

[aruba:vars]
ansible_user =     "{{ aruba_user }}"
ansible_password = "{{ aruba_password }}"
ansible_network_os = aruba
ansible_connection = network_cli

[cisco]
cisco-switch-01 ansible_host=192.168.0.10
cisco-switch-02 ansible_host=192.168.0.11

[cisco:vars]
ansible_user =     "{{ cisco_user }}"
ansible_password = "{{ cisco_password }}"
ansible_become_password = "{{ cisco_become_password }}"
ansible_network_os = cisco.ios.ios
ansible_connection = network_cli

[comware]
comware-switch-01 ansible_host=192.168.0.20
comware-switch-02 ansible_host=192.168.0.21

[comware:vars]
ansible_user =     "{{ comware_user }}"
ansible_password = "{{ comware_password }}"
ansible_network_os = comware
ansible_connection = local

[network:children] # すべてのネットワーク機器
aruba
cisco
comware

播放脚本文件show_version.yml

---
- name: get network device version info
  hosts: "{{ host | default('network') }}" # 引数が指定されていない場合は"network"(すべての機器)の情報を取得する
  gather_facts: no

  roles:
     - show_version # role"show_version"を実行する

– 主演/展示_版本/任务/主要.yml

根据hosts文件变量中定义的ansible_network_os值选择要执行的yml文件。

---
- name: execute show version (aruba)
  block:
    - include_tasks: "{{ ansible_network_os }}.yml" # 機種に対応してymlファイルを読込み、実行する
  when:
      - ansible_network_os is defined # 変数ansible_network_osが定義されている場合のみ実行する

角色/显示版本/任务/思科.ios.ios.yml

思科的Playbook文件。

- name: execute command (cisco)
  no_log: true
  ios_command:
    commands:
      - terminal length 0
      - show version
  register: result

- name: debug cisco
  debug:
    msg: "{{ result.stdout_lines[1] }}"
  when:
    - result.stdout_lines[1] is defined

角色/显示版本/任务/Aruba.yml

阿鲁巴(Aruba)的播放书文件。

- name: execute command (aruba)
  no_log: true
  aruba_command:
    commands:
      - no page
      - show version
  register: result

- name: aruba debug
  debug:
    msg: "{{ result.stdout_lines }}"
  when:
    - result.stdout_lines is defined

角色/显示版本/任务/阿鲁巴.yml

HPE Comware的playbook文件。若要与HPE Comware连接,请使用hpe-cw7-ansible-main。
https://github.com/HPENetworking/hpe-cw7-ansible

- name: execute command (comware)
  no_log: true
  comware_command:
    command:
      - screen-length disable
      - show version
    type: display
    username: "{{ username }}"
    password: "{{ password }}"
    hostname: "{{ inventory_hostname }}"
  register: result

- name: debug comware
  debug:
    msg: "{{ result.response | replace('\\r', '') }}"
  when:
    - result.response is defined

角色/显示版本/变量/主文件.yml


username: "{{ ansible_user }}"
password: "{{ ansible_password }}"

中国话母语者的中文改写如下:
~/脚本/网络/显示版本.sh

#!/bin/bash

HOST=""

usage() {
  echo "Usage: show_version.sh [-n ホスト名] [-h ヘルプ]" 1>&2
}

exit_abnormal() {
  usage
  exit 1
}

call_ansible_script() {
  cd ~/ansible/network/
  if [ -z "$HOST"  ]; then
    ansible-playbook show_version.yml --ask-vault-pass
  else
    ansible-playbook show_version.yml --ask-vault-pass -e "host=$HOST"
  fi
}

while getopts ":n:h" OPT; do
  case $OPT in
  n) HOST=${OPTARG}
    call_ansible_script
    exit 0
    ;;
  h) usage
    exit 0
    ;;
  :) echo "-$OPTARGはホスト名が必須です"
    exit_abnormal;;
  esac
done
echo "execute ansible host:$HOST"
call_ansible_script
exit 0

最后的话

该文章讨论了从开关获取基本信息的示例,但实际操作中可以使用模板批量进行不同机型的设置等操作。

上述内容

广告
将在 10 秒后关闭
bannerAds