LoginSignup
12
8

More than 1 year has passed since last update.

AnsibleでCiscoルータのIPアドレスを変更する

Last updated at Posted at 2021-03-01

発端

Ansibleからネットワーク機器を触ってみようと思い、いろいろ調べました。しかしネット上のplaybookをコピペしてみたもののうまく動かず(エラーになる)、四苦八苦しました。ほかの方の参考になればと、ここにうまくいった例を挙げておきます。

SoftwareDesign2018年12月号の特集に触発されて実験しました。

環境

Ansibleサーバ:Ubuntu16にインストールして実行しました。
https://docs.ansible.com/ansible/2.9_ja/installation_guide/intro_installation.html#ubuntu-ansible

XXX@XXX:~$ ansible --version
ansible 2.9.18

対象ネットワーク機器:Cisco1812J

Router#show version
Cisco IOS Software, C181X Software (C181X-ADVIPSERVICESK9-M), Version 12.4(15)T1, RELEASE SOFTWARE (fc2)

Directory構成

.
--- playbook.yml
--- ansible.cfg
--- group_vars
   |--- ios.yml
--- inventory

inventory

[ios]
my_router ansible_host=xxx.xxx.xxx.xxx #制御したいルータのIPアドレスを記載

ios.yml

ansible_network_os: ios
ansible_connection: network_cli
ansible_user: XXXXX            #cisco routerのsshログインID
ansible_password: XXXXX        #cisco routerのsshログインパスワード
ansible_become: yes
ansible_become_method: enable
ansible_become_pass: XXXXX     #cisco routerのenableパスワード

playbook

・やりたいこと:ルータのホストネームとFastEthernet0のIPアドレスを変更する。※hostnameをNew_RouterNameに、IP addressを1.1.1.1/24に変更。sshでアクセスしている物理ポートのIPアドレスを変えてしまわないように注意(^^;)
・Globalコンフィギュレーションモードで変更できるものと、階層がことなるInterfaceコンフィギュレーションモードで変更するもので、どのように書き方を変えなければいけないか検証。

- hosts: ios
  gather_facts: no

  tasks:
    - name: set hostname        #hostnameの変更
      ios_config:
        lines:
          - hostname New_RouterName
        save_when: changed

    - name: set ip address      #IP addressの変更
      ios_config:
        lines:
          - ip address 1.1.1.1 255.255.255.0
        parents:
          - interface FastEthernet 0
        save_when: changed

はまったところ

・一次情報をあたっていないので何とも言えないのですが、commnadsにコマンドを書く方法と、linesに「-」でコマンドをを渡す方法があるらしく、モジュールの使い方をちゃんと見た方がよいw
・IPアドレスを「実験だから」と/32にしたらエラーでansibleがコケました。LoopBackだったらいけたのかも。
・ansibleサーバからciscoへsshで接続できることが前提です。そして設定方法をgroup_varsのios.ymlに書いておく必要があります。
・yamlなので、意図せずTABが入っていると動きませんw
・playbookの書式チェックに、--syntax-checkオプションを付けてお伺いを立てるのですが、

ansible-playbook -i inventory playbook.yml --syntax-check

おかしいと言われる場所が、該当の行でないことが多いです。「おかしいなぁ」と思ったら、直後の行等を調べると良いと思います。

12
8
0

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
12
8