NW機器では古い環境になるとSSHアクセスではなく、Telnetアクセスをすることが多いです。
AnsibleでTelnet接続をしてPlaybookを動かすため方法をまとめました。
目次
TelnetでCiscoNW機器へアクセス・出力結果をログに保存するPlaybookの作成
今回の環境はCentOS8の環境で実施しています。
# cat /etc/redhat-release CentOS Linux release 8.2.2004 (Core)
Ansibleのインストール方法は以下の記事を参考にしてください。
あわせて読みたい


Ansibleインストール_Docker(CentOS8)コンテナ環境
Dockerコンテナ上でCentOS8を構築し、Ansibleをインストールしたので手順を作成しました。 DockerコンテナでCentOS8の構築手順は以下のページを参考にしてください。 Do...
まず、設定ファイルですが、デフォルトで用意されている「hosts」と「cisco.yml」の2つを作成します。
ファイルの階層は以下となります。
$ pwd /etc/ansible
フォルダの中身は以下となります。
「cisco.yml」ファイルと「LOG」フォルダは追加で作成します。
.
├── LOG
└── show_run.log
├── ansible.cfg
├── cisco.yml
├── hosts
└── roles
「hosts」にCiscoNW機器のアクセス先を追加
以下のように「hosts」ファイルにグループ名と対象機器のIPを追記します。
$ vim hosts
[cisco_r1] 192.168.0.1
「Playbook」作成
次に「cisco.yml」ファイルを作成します。
$ vim cisco.yml
---
- hosts: cisco_r1
connection: local
gather_facts: False
tasks:
- name: show run
telnet:
user: cisco
password: test_password
login_prompt: "Password: "
prompts:
- "[>|#]|Password: "
command:
- terminal length 0
- enable
- test_password
- show run
- show ver
changed_when: False
register: command_result
- name: debug
debug:
var: command_result
- name: log export
local_action:
module: copy
owner: root
group: root
mode: 0644
dest: "/etc/ansible/LOG/show_run.log"
content: "{{ command_result.output[3] }}"
changed_when: False
※ログの出力ですが、出力先は「/etc/ansible/LOG/show_run.log」となります。
また、今回は「show run」の出力結果をログに出力するため、以下の設定とします。
content: "{{ command_result.output[3] }}"
これは、Playbookの「command: 」で番号がリストで「0」から始まり、「show run」は「3」となるためこのような設定となっています。
Ansible「Playbook」実行
実際にPlaybookを以下コマンドで事項します。
# ansible-playbook -i hosts cisco.yml
PLAY [cisco_r1] *****************************************************************************************
TASK [show run] *****************************************************************************************
changed: [192.168.0.1]
TASK [debug] ********************************************************************************************
ok: [192.168.0.1] => {
"command_result": {
"changed": true,
"failed": false,
"output": [
"terminal length 0\r\nRT_A>",
"enable\r\nPassword: ",
"\r\nRT_A#",
"show run\r\nBuilding configuration...\r\n\r\nCurrent configuration : 1283 bytes\r\n!\r\n! Last
------中略------
(Read/Write)\r\n\r\n\r\nLicense Info:\r\n\r\nLicense UDI:\r\n\r\n-------------------------------------------------\r\nDevice#"
]
}
}
TASK [log export] ***************************************************************************************
ok: [192.168.0.1]
PLAY RECAP **********************************************************************************************
192.168.0.1 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
$
正常に完了すると上記のようになります。
ログの出力結果を確認
ログの出力も以下のように「show run」コマンドの出力結果が保存されていることが確認できます。
# cat /etc/ansible/LOG/show_run.log show run Building configuration... Current configuration : 1283 bytes ! ! Last configuration change at 03:56:44 UTC Wed Nov 25 2020 ! version 15.1 service timestamps debug datetime msec service timestamps log datetime msec no service password-encryption ! hostname RT_A ! boot-start-marker boot config flash:892config.txt
以上がAnsibleのTelnetアクセスとログのエクスポートを実施するPlaybookの作成方法です。
ITエンジニアの開発・検証・学習としてインターネット上で専用のサーバ(VPS)を利用しましょう!
実務経験はVPSで学べます。

コメント