LoginSignup
21
19

More than 5 years have passed since last update.

Ansible telnetモジュールでYamahaルータを操作

Posted at

以前、あきばで2000円でGetしたYamaha RTX1100が僕のお家にございます。

これをAnsibleで操作しようと試したのですが、↓の方が書かれているように
Yamahaのルータはsshによるコマンド実行ができないんですねー。

こうなってしまいます
$ ssh yamaha@10.0.0.1 'show config'
yamaha@10.0.0.1's password: 
Received disconnect from 10.0.0.1 port 22:2: Channel request 'exec' is not supported
Disconnected from 10.0.0.1 port 22

よろしいならば、2.4から加わったtelnetモジュールだということで試してみたら、こんなエラーが出ました。
※Versionは2.5.0でした

$ ansible-playbook -i hosts rtx1100_telnet.yml

PLAY [rtx1100] *************************************************************************************

TASK [show config] *********************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: TypeError: a bytes-like object is required, not 'AnsibleUnicode'
fatal: [10.0.0.1]: FAILED! => {"msg": "Unexpected failure during module execution.", "stdout": ""}

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

な、なんだってー Σ(゚Д゚)
telnetlibのサンプルコードは上手く行ったのに...orz

ぐぐるとどうやらBugだったようで8日前にmergeされてました。

ではmergeされたのを試してみます。
いったんvenvを切って、dev版をインストール

$ python3 -m venv dev
$ source dev/bin/activate
$ python3 -m pip install git+https://github.com/ansible/ansible.git@devel

2.7.0.dev0が入りました。

$ ansible --version
ansible 2.7.0.dev0
  config file = None
  configured module search path = ['/home/lulucan/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /work/an/dev/lib/python3.5/site-packages/ansible
  executable location = /work/an/dev/bin/ansible
  python version = 3.5.2 (default, Nov 23 2017, 16:37:01) [GCC 5.4.0 20160609]

これで試してみると...

$ ansible-playbook -i hosts rtx1100_telnet.yml --check

PLAY [rtx1100] *************************************************************************************

TASK [show config] *********************************************************************************
skipping: [10.0.0.1]

TASK [debug] ***************************************************************************************
ok: [10.0.0.1] => {
    "result.output": "VARIABLE IS NOT DEFINED!"
}

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

(dev) lulucan@:nw$ ansible-playbook -i hosts rtx1100_telnet.yml 

PLAY [rtx1100] *************************************************************************************

TASK [show config] *********************************************************************************
changed: [10.0.0.1]

TASK [debug] ***************************************************************************************
ok: [10.0.0.1] => {
    "result.output": [
        "1 (Mon Apr 25 13:46:36 2011)\r\n  Copyright (c) 1994-2011 Yamaha Corporation.\r\n  Copyright (c) 1991-1997 Regents of the University of California.\r\n  Copyright (c) 1995-2004 Jean-loup Gailly and Mark Adler.\r\n  Copyright (c) 1998-2000 Tokyo Institute of Technology.\r\n  Copyright (c) 2000 Japan Advanced Institute of Science and Technology, HOKURIKU.\r\n  Copyright (c) 2002 RSA Security Inc. All rights reserved.\r\n  Copyright (c) 1997-2004 University of Cambridge. All rights reserved.\r\n  Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, All rights reserved.\r\n  Copyright (c) 1995 Tatu Ylonen , Espoo, Finland All rights reserved.\r\n  Copyright (c) 1998-2004 The OpenSSL Project.  All rights reserved.\r\n  Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) All rights reserved.\r\n  Copyright (c) 2006 Digital Arts Inc. All Rights Reserved.\r\n00:a0:de:68:37:a1, 00:a0:de:68:37:a2, 00:a0:de:68:37:a3, \r\nMemory 32Mbytes, 3LAN, 1BRI\r\n>"
    ]
}

PLAY RECAP *****************************************************************************************
10.0.0.1                   : ok=2    changed=1    unreachable=0    failed=0   


キタ━(゚∀゚)━!

ちゃんと通りました。
outputが1行で戻ってくるのか...
改行で区切るtemplateにでも突っ込めばいいのかなぁ..??

Playbookはこんな感じでした。

rtx1100_telnet.yml
---
- hosts: rtx1100
  connection: local
  gather_facts: False

  tasks:
  - name: show config
    telnet:
      user: "{{ user }}"
      password: "{{ pass }}"
      login_prompt: "Password: "
      prompts:
        - "[>|#]|Password: "
      command:
        - show config
    register: result

  - debug:
      var: result.output

おしまいです。(・ω<) てへぺろ

21
19
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
21
19