LoginSignup
27

More than 5 years have passed since last update.

Ansibleで複数のCatalystのパスワードを一括変更する

Last updated at Posted at 2016-06-05

1.はじめに

Ansible 2.1 で ネットワーク関係のモジュールがコアモジュールになったので、
Cisco IOSに対応したモジュール「ios_command」や「ios_config」を利用して
パスワードの一括変更をやってみます

2.環境

  • Ubuntu16.04
  • Ansible 2.1 (apt-getでインストール)
  • IOS関係モジュール(標準のまま)
user@ubuntu-vm:/etc/ansible$ ansible-doc --list | grep ios
ios_command                        Run arbitrary commands on ios devices.
ios_config                         Manage Cisco IOS configuration sections
ios_template                       Manage Cisco IOS device configurations over SSH
iosxr_command                      Run arbitrary commands on ios devices.
iosxr_config                       Manage Cisco IOS XR configuration sections
iosxr_template                     Manage Cisco IOSXR device configurations over SSH
nagios                             Perform common tasks in Nagios related to downtime and notifications.
  • Cisco Catalyst 3750 (IOS 12系) 2台

3.各ファイル

3.1. ansible.cfg

インストール直後から特に変更なし

3.2.インベントリファイル

/etc/ansible/hosts
[cisco]        # Catalytのグループciscoを定義
192.168.1.1    # Catalyst 1台目
192.168.1.2    # Catalyst 2台目

[cisco:vars]   # グループcisco共通の変数定義
ansible_user=admin             # ログインユーザー
ansible_password=adminpas      # ログインパスワード
cisco_enable_secret=enable     # 特権パスワード

3.3.Playbook

/etc/ansible/main.yml
---
- hosts: cisco        # インベントリファイルで定義したciscoグループが操作対象
  gather_facts: no
  connection: local

  tasks:
    - name: change password
      ios_config:
        lines:
          - username ope password opeope   # 実行させたいコマンド
        provider: "{{ cli }}"   # 後で定義する接続情報を辞書で渡す
      register: result          # 結果をデバッグ表示するため変数resultに登録
    - name: DEBUG
      debug: var=result         # 結果デバッグ

    - name: save config
      ios_command:
        commands:
          - write memory        # running-configの保存
        provider: "{{ cli }}"
      register: result
    - name: DEBUG
      debug: var=result

  vars:
    cli:   # 接続情報を辞書で定義(usernameとそれ以降はインベントリファイルから取得定義)
      host:     "{{ inventory_hostname }}"    # ホスト対象ホスト
      username: "{{ ansible_user }}"          # ログインユーザー
      password: "{{ ansible_password }}"      # ログインパスワード
      authorize: true                         # 特権モードに移行
      auth_pass: "{{ cisco_enable_secret }}"  # 特権パスワード

4.実行

4.1. 事前確認

SSH用にusernameを設定してありますので変更前を確認します。

1台目(192.168.1.1)
SW1#show start | inc username
username ope password 7 011C6917641B03
2台目(192.168.1.2)
SW2#show start | inc username
username ope password 7 61467B03003945

4.2. Playbook実行

user@ubuntu-vm:/etc/ansible$ ansible-playbook main.yml

PLAY [cisco] *******************************************************************

TASK [change password] *********************************************************
changed: [192.168.1.2]
changed: [192.168.1.1]

TASK [DEBUG] *******************************************************************
ok: [192.168.1.1] => {
    "result": {
        "changed": true,
        "responses": [
            ""
        ],
        "updates": [
            "username ope password opeope"
        ]
    }
}
ok: [192.168.1.2] => {
    "result": {
        "changed": true,
        "responses": [
            ""
        ],
        "updates": [
            "username ope password opeope"
        ]
    }
}

TASK [save config] *************************************************************
ok: [192.168.1.2]
ok: [192.168.1.1]

TASK [DEBUG] *******************************************************************
ok: [192.168.1.1] => {
    "result": {
        "changed": false,
        "stdout": [
            "Building configuration...\n[OK]"
        ],
        "stdout_lines": [
            [
                "Building configuration...",
                "[OK]"
            ]
        ]
    }
}
ok: [192.168.1.2] => {
    "result": {
        "changed": false,
        "stdout": [
            "Building configuration...\n[OK]"
        ],
        "stdout_lines": [
            [
                "Building configuration...",
                "[OK]"
            ]
        ]
    }
}

PLAY RECAP *********************************************************************
192.168.1.1               : ok=4    changed=1    unreachable=0    failed=0
192.168.1.2               : ok=4    changed=1    unreachable=0    failed=0

正常実行されたようです。

4.3. 事後確認

1台目(192.168.1.1)
SW1#show start | inc username
username ope password 7 161615121D1B71
2台目(192.168.1.2)
SW2#show start | inc username
username ope password 7 1416183E03182E

2台とも変更されました(値はダミーです)

5.補足

  • 実用性より「なるほどネットワーク機器対応ってこういうことか」を体験するためにやってみたという趣旨です。
  • 上記では省略していますが、Ansibleから接続する際のユーザー、パスワードは別に設定済みで、 今回パスワードを変更しているのは、また別のユーザーです。
  • Playbookを複数回実行するとその都度変更コマンドが実行されます。実際は同じパスワードで設定していますが、冪等性は無いやり方です。
  • 「ログとろうよ」「今どき write memory は無いわ」「変数の持ち方工夫したら?」など改善の余地しかないですが、今回はここまで・・。

6.参考情報

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
27