AnsibleでCatalystのconfig取ってみた(telnet版)


Ansibleが身近でも流行り始めたので、ちょっと試してみました。

しかし、我が家のCatalyst2960-8TC-LにはSSHが積まれいないので、とりあえずtelnetモジュールを試すことに。
(SSH対応の別スイッチは後で調達したのでその話はまたいずれ)

環境は、
MacOS 10.14
VirtualBox 5.2.22
Ubuntu 18.04
ansible 2.5.1
IOS 12.2(53)SE2

まずはこちらを参考にしました。
リンク「Ansibleでtelnetする

作ったYMLはこんな感じ

main.yml
---
  - name: show run
    telnet:
#      user: cisco
      password: password
      login_prompt: "Password: "
      prompts:
        - "[>|#]|Password: "
#        - "[>|#]"
      command:
       - terminal length 0
       - enable
       - cisco
       - show run
      changed_when: False
      register: command_result

  - name: debug
    debug:
      var: command_result

  - name: log export
    local_action:
      module: copy
      owner: hayashi
      group: hayashi
      mode: 0644
      dest: "{{ log_dir }}/show_run.log"
      content: "{{ command_result.output[1] }}"
    changed_when: False

しかし結果は、NG


debug:
"command_result": "VARIABLE IS NOT DEFINED!"

log report:
fatal: [192.168.0.201]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'command_result' is undefined\n\nThe error appears to have been in '/home/hayashi/cisco/tasks/main.yml': line 23, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: log export\n ^ here\n"

ということで、変数に値が入っていないと怒られるわけです。

変数名を変えてみたり等試してみましたがなかなか埒が明きそうにないので、別のリンクを参考にして階層構造をやめてみることにしました。

リンク「Ansible telnetモジュールでYamahaルータを操作

新しいYMLがこちら

cisco.yml
----
- hosts: R1
  connection: local
  gather_facts: False
#  roles:
#    - cisco

  tasks:
  - name: show run
    telnet:
#      user: cisco
      password: password
      login_prompt: "Password: "
      prompts:
        - "[>|#]|Password: "
      command:
        - terminal length 0
        - enable
        - password
        - show run
#        - show ver
      changed_when: False
    register: command_result

  - name: debug
    debug:
      var: command_result.output

  - name: log export
    local_action:
      module: copy
      owner: hayashi
      group: hayashi
      mode: 0644
      dest: "/home/hayashi/show_run.log"
#      dest: "{{ log_dir }}/show_run.log"
      content: "{{ command_result.output[3] }}"
    changed_when: False

結果です。上手くいきました。


debugに全部出力されてますね。。
結果

hayashi@hayashi-VirtualBox:~$ sudo ansible-playbook -i ./hosts cisco.yml

PLAY [R1] **********************************************************************

TASK [show run] ****************************************************************
<font color="orange">changed: [192.168.0.201]</font>

TASK [debug] *******************************************************************
ok: [192.168.0.201] => {
    "command_result.output": [
        "terminal length 0\r\nSwitch>", 
        "enable\r\nPassword: ", 
        "\r\nSwitch#", 
        "show run\r\nBuilding configuration...\r\n\r\nCurrent configuration : 935 bytes\r\n!\r\nversion 12.2\r\nno service pad\r\nservice timestamps debug datetime msec\r\nservice timestamps log datetime msec\r\nno service password-encryption\r\n!\r\nhostname Switch\r\n!\r\nboot-start-marker\r\nboot-end-marker\r\n!\r\nenable password password\r\n!\r\n!\r\n!\r\nno aaa new-model\r\nsystem mtu routing 1500\r\nauthentication mac-move permit\r\nip subnet-zero\r\n!\r\n!\r\n!\r\n!\r\n!\r\n!\r\nspanning-tree mode pvst\r\nspanning-tree etherchannel guard misconfig\r\nspanning-tree extend system-id\r\n!\r\nvlan internal allocation policy ascending\r\n!\r\n!\r\n!\r\ninterface FastEthernet0/1\r\n!\r\ninterface FastEthernet0/2\r\n!\r\ninterface FastEthernet0/3\r\n!\r\ninterface FastEthernet0/4\r\n!\r\ninterface FastEthernet0/5\r\n!\r\ninterface FastEthernet0/6\r\n!\r\ninterface FastEthernet0/7\r\n!\r\ninterface FastEthernet0/8\r\n!\r\ninterface GigabitEthernet0/1\r\n!\r\ninterface Vlan1\r\n ip address 192.168.0.201 255.255.255.0\r\n no ip route-cache\r\n!\r\nip http server\r\n!\r\nline con 0\r\n password password\r\nline vty 0\r\n password password\r\n login\r\nline vty 1 4\r\n login\r\nline vty 5 15\r\n login\r\n!\r\nend\r\n\r\nSwitch#"
    ]
}

TASK [log export] **************************************************************
ok: [192.168.0.201 -> localhost]

PLAY RECAP *********************************************************************
192.168.0.201    : ok=3    changed=1    unreachable=0    failed=0

もちろんshow_run.logも出来ています。

今後は、変数対応や、複数ホスト、複数コマンド対応、Username対応など、telnetモジュールで出来ることを確認していく予定です。そのうちSSHも。

複数コマンド、複数ノード対応について書きました

以上