アンサーブルTelnetが機能しない

アンサーブルTelnetが機能しない

初期設定をプッシュするために Ansible Expect モジュールで telnet コマンドを使用しようとしても機能しません。応答部分に問題があるようです。ここに私のプレイブックがあります。

- hosts: wired
  gather_facts: false
  connection: local
  tasks:
  - name: telnet,login and execute command
    ignore_errors: no
    expect:
      command: telnet "{{ inventory_hostname }}" 2009
      responses:
        .*>: "enable"
        .*Password: "mainpass"
        .*#: "show runn | in hostname"
    register: telnet_output
  - name: Debug output
    debug: var=telnet_output.stdout_lines

端末からリモートでログインしようとすると、次のメッセージが表示されます。

Trying 10.1.1.1...
Connected to switch.lab.local.
Escape character is '^]'.

Enterを押す必要があります。これにより、以下が表示されます。

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
To log in to this device, use the following password/credentials:

enable password mainpass   
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


switch>

その後、アクティベーションコマンドを入力する必要があり、アクティベーションパスワードの入力を求められます。

switch>enable
Password: 
switch#

上記の内容を私のPlaybookの返信セクションに翻訳することはできません。実行すると、次の内容が表示されます。

fatal: [switch]: FAILED! => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "cmd": "telnet \"switch\" 2009",
    "delta": "0:00:00.312863",
    "end": "2022-11-08 15:01:27.707396",
    "invocation": {
        "module_args": {
            "chdir": null,
            "command": "telnet \"switch\" 2009",
            "creates": null,
            "echo": false,
            "removes": null,
            "responses": {
                ".*#": "show runn | in hostname",
                ".*>": "enable",
                ".*Password": "mainpass",
                "Trying 10.1.1.1....*": "\n",
                "telnet: Unable to connect to remote host: Connection refused": ""
            },
            "timeout": 30
        }
    },
    "msg": "non-zero return code",
    "rc": 1,
    "start": "2022-11-08 15:01:27.394533",
    "stdout": "Trying 10.1.1.1...\r\ntelnet: Unable to connect to remote host: Connection refused",
    "stdout_lines": [
        "Trying 10.1.1.1...",
        "telnet: Unable to connect to remote host: Connection refused"
    ]
}

ベストアンサー1

接続が拒否されましたというメッセージが表示されるため、Ansible接続が期待どおり/必要に応じて機能しないとします。 Ansibleはネットワークデバイスを処理するための特別なモジュールを構築しました。ここを参照してください。https://docs.ansible.com/ansible/latest/network/getting_started/network_differences.html

最良の方法は、マニュアルを読んで設定方法を学ぶことだと思います。また、Telnetはプレーンテキストで有線で移動するため、デバイスがサポートしている場合は実際にSSHまたはHTTPS APIにアップグレードする必要があります。

おすすめ記事