【sshでHSRPプライオリティ変更】

---ssh接続でNWモジュールを使って設定する---

  • 前回、Cisco機器側のssh接続をtelnetモジュールを使って(無理やり)可能にしたこ
  • とで、やっとAnsibleのNWモジュールの機能を使えるようになった。telnetモジュールで大変な労力をかけてやっとできていた「HSRPのプライオリティ変更手順する」手順をNWモジュールを使ってやってみる。

<<HSRPプライオリティ変更>>

  • 例として以下のエクセルの手順書の工事を自動化することを想定*1

大項目 項目番号 項目 対象機器 設定コマンド 確認コマンド 出力確認 時間(秒)
事前確認 1_1 インタフェース設定確認 ciscoR1 sh run int vlan951 以下の通り設定があること
interface Vlan951
ip address 10.100.14.195 255.255.255.240
load-interval 30
standby 6 ip 10.100.14.193
standby 6 priority 80
standby 6 preempt delay minimum 30
end
3
1_2 HSRPステータス確認 ciscoR1 sh standby brief | i Vl951 プライオリティ値、ステータス表示が以下の通りであること
Interface Grp Pri P State Active Standby Virtual IP
Vl951 6 80 P Standby 10.100.14.194 local 10.100.14.193
3
設定 2_1 設定モード移行 ciscoR1 conf t 設定モードに移行することを以下のプロンプト出力で確認
ciscoR1(config)#
3
2_2 インタフェースモード移行 ciscoR1 nterface Vlan951 インタフェースモードに移行することを以下のプロンプト出力で確認
ciscoR1(config-if)#
3
2_3 HSRPプライオリティ変更 ciscoR1 standby 6 priority 120 以下プロンプト出力に戻ること。エラー出力がないこと。
ciscoR1(config-if)#
3
2_4 設定モード移行 ciscoR1 exit 設定モードにmodoru ことを以下のプロンプト出力で確認
ciscoR1(config)#
3
2_5 特権モード以降 ciscoR1 exit 設定モードにmodoru ことを以下のプロンプト出力で確認
ciscoR1#
3
事後確認 3_1 インタフェース設定確認 ciscoR1 sh run int vlan951 プライオリティ値が120であること。他は事前確認と差分がないこと。
interface Vlan951
ip address 10.100.14.195 255.255.255.240
load-interval 30
standby 6 ip 10.100.14.193
standby 6 priority 120
standby 6 preempt delay minimum 30
end
3
3_2 HSRPdelay確認 ciscoR1 sh standby vlan 951 delayタイマー待ちであることを確認。以降30秒待つ
Vlan951 - Group 6
State is Standby
---snip---
Preemption enabled, delay min 30 secs (21 remaining)
Active router is 10.100.14.194, priority 90 (expires in 7.528 sec)
Standby router is local
Priority 120 (configured 120)
Group name is "hsrp-Vl951-6" (default)
35
3_3 HSRPステータス確認 ciscoR1 sh standby brief | i Vl951 プライオリティ値、ステータス表示が以下の通りであること
Interface Grp Pri P State Active Standby Virtual IP
Vl951 6 120 P Active local 10.100.14.194 10.100.14.193
3
3_4 config差分確認 ciscoR1 事前事後に取得したテキストファイルをdiffツールで比較し
工事設定以外の差分がないこと
10
3_5 config保存 ciscoR1 copy running startup 以下出力確認後、プロンプトまで戻ること
Building configuration...
[OK]
10
3_6 configリモートバックアップ ciscoR1 copy running tftpXXXX 以下のようにXXX bytes copied in XXXsecsまで表示後、
プロンプトに戻ること
ciscoR1#copy running-config ftp://ktn:ktn@192.168.3.218/backupconf
Address or name of remote host [192.168.3.218]?
Destination filename [backupconf]?
Writing backupconf !
XXX bytes copied in XXX secs (XXXbytes/sec)
ciscoR1#
10

 

<<確認>>

  • 特権モードに移行しcisco機器への設定が可能なこと
  • 冪等性の担保ができていること


<<結果と課題>>

  • playbookを1回実行するだけで事前確認⇒設定⇒事後確認⇒startupメモリ保存⇒バックアップまで一通り公式サイトのサンプルだけで簡単に実装できた。
  • HSRPの状態はHSRPグループ内のプライオリティ値により相対的に変わってくるので変更前のあるべき状態を確認する工程も必要になる。Ansibleモジュールではconfigの冪等性は担保してくれるがHSRPのステータスまで見てくれないので事前確認のタスクでplaybookの2回目の実行からはfailedになってしまう。「ACTに遷移する設定を実行する場合、もともとACTなら何もしない、、、」という冪等性の担保をyamlで持たせたい。書き方を勉強中。
  • CiscoのHSRPのデフォルトプライオリティは100でconfig上に表示されないため、設定を100にした場合は、何度playbookを実行してもchanged=1になった。

<<思ったこと>>

  • これまでのNWの運用手順を考えると、状態を判断させる方法をAnsible(モジュールまたはyaml)で実装できればNWの運用も自動化に近づけるかな。

 

 <<Playbook>>

   ios_hsrp_mod.yml 

---
- hosts: cisco
  connection: network_cli
  gather_facts: no

  tasks:
    - name: show current status
      ios_command:
        commands:
          - "show standby Vlan{{Vlan_num}}"
        wait_for:
          - result[0] contains "{{mod_state}} router is {{peer_add}}, priority {{peer_pri}}"
          - result[0] contains "Priority {{pre_pri}}"
          - result[0] contains "{{pre_state}} router is local"
    
    - name: mod hsrp priority
      ios_config:
        lines:
          - standby {{Grnum}} priority {{mod_pri}}
        parents: "interface Vlan{{Vlan_num}}"

    - name: wait preempt delay
      pause:
        seconds: 10

    - name: after hsrp status
      ios_command:
        commands:
          - "show standby Vlan{{Vlan_num}}"
        wait_for:
          - result[0] contains "{{pre_state}} router is {{peer_add}}, priority {{peer_pri}}"
          - result[0] contains "Priority {{mod_pri}}"
          - result[0] contains "{{mod_state}} router is local"

    - name: check the startup-config against the running-config
      ios_config:
        diff_against: startup
        diff_ignore_lines:
          - ntp clock .*

    - name: save running to startup when modified
      ios_config:
        save_when: modified

    - name: Backup current RT config
      ios_config:
        backup: yes
      register: backup_ios_location
      when: ansible_network_os == 'ios'

    - name: Display Result
      debug:
        var: backup_ios_location

 - name: show current statusのタスクはHSRPの状態を確認する部分なので1回目の状態で変更されるため2回目の実行ではfailedになる。

 <<inventory>>

 inventory/cisco_hsrp

[cisco]
ciscoR1 ansible_host=192.168.3.215 Vlan_num="951" Grnum="6"  pre_state="Active" pre_pri="120" mod_state="Standby" mod_pri="80" peer_add="10.100.14.194" #to ACT
#ciscoR1 ansible_host=192.168.3.215 Vlan_num="951" Grnum="6"  pre_state="Standby" pre_pri="80" mod_state="Active" mod_pri="120" peer_add="10.100.14.194" #to SBY

[cisco:vars]
ansible_ssh_user=hoge
ansible_ssh_pass=1234
ansible_network_os=ios
ansible_connection=network_cli
ansible_become=yes    
ansible_become_method=enable    
ansible_become_password="1234"
peer_pri="90"

ACT/SBYの冗長構成でプライオリティ値は固定、どちらもpreempt設定がある場合を想定している。

インベントリーファイルのinventory_hostnameの行でACTにするかSBYにするかをコメントの付け外しで指定する。

 

*1:手順書は実在のものではなくイメージで、パラメータも架空のものです