LoginSignup
13

More than 5 years have passed since last update.

AnsibleでCisco IOSのコンフィグを標準機能だけで収集する

Last updated at Posted at 2016-06-15

1. 方針

ios_templateというモジュールにはbackupオプションというものがあり、
実行時にバックアップ用にコンフィグをとってくれるようです。
https://docs.ansible.com/ansible/ios_template_module.html

When this argument is configured true, the module will backup the running-config from the node prior to making any changes. The backup file will be written to backup_{{ hostname }} in the root of the playbook directory.

なのでこれを利用して、コンフィグの収集をしてみます。

※2016/10/08追記
ansibleのCHANGELOGによると、 ネットワークモジュールの
*_template モジュールの機能は、 *_config に統合され 2.4で削除されるようです。
https://github.com/ansible/ansible/blob/stable-2.2/CHANGELOG.md

All functionality from *_template network modules have been combined into *_config module

2. 各ファイルの準備

ios_templateの本来の用途はテンプレートファイルを用意して、流し込みをする
なのですが、今回はコンフィグ変更はしてほしくないので、空のテンプレートファイル
を用意して、それをPlaybookから呼ぶことにします。

2.1. Playbookの作成

getconfig.yml
- hosts: cisco   # イベントファイルで 192.168.1.1を定義済み。もちろん複数定義もOK。
  gather_facts: no
  connection: local

  tasks:
    - name: set
      ios_template:
        provider: "{{ cli }}"
        src: config.j2           # ★★別途定義するテンプレートファイル(今回は空)
        backup: true             # ★★バックアップを取得するオプション
        include_defaults: false  # これを指定しない場合「show run all」になります。
  vars:
    cli:   # 接続情報を辞書で定義(usernameとそれ以降はインベントリファイルから取得定義)
      host:     "{{ inventory_hostname }}"    # ホスト対象ホスト
      username: "{{ ansible_user }}"          # ログインユーザー
      password: "{{ ansible_password }}"      # ログインパスワード
      authorize: true                         # 特権モードに移行
      auth_pass: "{{ cisco_enable_secret }}"  # 特権パスワード

2.2. j2ファイルの作成

空のテンプレートファイルをPlaybookと同じディレクトリに配置します。
ここでは「config.j2」というファイル名にします。
空ファイルは避けたい場合は、!hoge などコンフィグに影響がない内容にしておきます。

3. Playbook実行

3.1. Playbook実行

バックアップコンフィグを書き出すときのパーミッションの関係で、sudoしてます。

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

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

TASK [set] *********************************************************************
ok: [192.168.1.1]

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

3.2. 収集コンフィグの確認

backupディレクトリの下に、ホスト名と日時を含むファイル名でコンフィグが
保存されていることを確認します。

user@ubuntu-vm:/etc/ansible$ head -n 10 backup/192.168.1.1_config.2016-06-15@22\:17\:32
Building configuration...

Current configuration : 7375 bytes
!
! Last configuration change at 22:06:22 JST Wed Jun 15 2016 by user
! NVRAM config last updated at 21:21:22 JST Wed Jun 15 2016 by user
!
version 12.2
no service pad
service timestamps debug datetime msec

ありました。

4. 注意点

  • 試した限り、もう一度実行すると古いバックアップは削除されます。

5. 参考情報

コンフィグファイルの名前を自分で決めたり、もっと柔軟にやりたい場合は
【Ansible】AnsibleでCisco機器を制御してみる(サンプルコード1 config収集編)
が参考になると思います。

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
13