Ansible Playbook 将 telnet show 运行 命令保存到文件语法错误

Ansible Playbook save telnet show run command to a file syntax error

我的 Ansible 剧本的语法存在问题。我正在尝试使用 show 命令使用 ansible 的 telnet 模块,遵循这个资源:https://docs.ansible.com/ansible/latest/modules/telnet_module.html#telnet-module 它工作正常但我的问题是当我尝试添加寄存器以将输出存储到变量以便稍后将其保存到文本文件时它给我一个错误,说该变量不存在指示复制任务的错误行.

我尝试添加空格并稍作更改,但仍然显示错误

---
- hosts: telnet
  gather_facts: true
  connection: local

  tasks:
   - name: run show commands
     telnet:
       user: cisco
       password: cisco
       login_prompt: "Username: "
       prompts:
         - "[>#]"
       commands:
         - terminal length 0
         - show version
     register: output


   - copy:
       content: "{{ output.stdout[0] }}"
       dest: "/home/user/telnettest.txt"
TASK [copy] **************************************************************************task path: /home/user/telnet.yaml:20
fatal: [IP]: FAILED! => {
    "msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'stdout'\n\nThe error appears to be in '/home/user/telnet.yaml': line 20, column 6, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n   - copy:\n     ^ here\n"
}

调试任务的输出:

TASK [debug] ***************************************************************************************************************************************************************
ok: [10.149.71.200] => {
    "msg": {
        "changed": true,
        "failed": false,
        "output": [
            "terminal length 0\r\nTHOSTNAME#",
            "show version\r\nCisco IOS Software,  Software (), Version , RELEASE SOFTWARE (fc3)\r\nTechnical Support: http://www.cisco.com/techsupport\r\nCopyright (c) 1986-2016 by Cisco Systems, Inc.\r\nCompiled Wed 17-Aug-16 13:28 by prod_rel_team\r\nImage text-base: 0x01000000, data-base: 0x02F00000\r\n\r\nROM: Bootstrap program is C3750 boot loader\r\nBOOTLDR: C3750 Boot Loader (C3750-HBOOT-M) Version 12.2(44)SE5, RELEASE SOFTWARE (fc1)\r\n\r\nHOSTNAME uptime is 6 weeks, 6 days, 6 hours, 12 minutes\r\nSystem returned to ROM by power-on\r\nSystem image file is \"flash:c3750-ipservicesk9-mz.122-55.SE11.bin\"\r\n\r\n\r\nThis product contains cryptographic features and is subject to United\r\nStates and local country laws governing import, export, transfer and\r\nuse. Delivery of Cisco cryptographic products does not imply\r\nthird-party authority to import, export, distribute or use encryption.\r\nImporters, exporters, distributors and users are responsible for\r\ncompliance with U.S. and local country laws. By using this product you\r\nagree to comply with applicable laws and regulations. If you are unable\r\nto comply with U.S. and local laws, return this product immediately.\r\n\r\nA summary of U.S. laws governing Cisco cryptographic products may be found at:\r\nhttp://www.cisco.com/wwl/export/crypto/tool/stqrg.html\r\n\r\nIf you require further assistance please contact us by sending email to\r\nexport@cisco.com.\r\n\r\ncisco WS-C3750-24P (PowerPC405) processor (revision J0) with 131072K bytes of memory.\r\nProcessor board ID CAT1037NGMH\r\nLast reset from power-on\r\n2 Virtual Ethernet interfaces\r\n24 FastEthernet interfaces\r\n2 Gigabit Ethernet interfaces\r\nThe password-recovery mechanism is enabled.\r\n\r\n512K bytes of flash-simulated non-volatile configuration memory.\r\nBase ethernet MAC Address       : \r\nMotherboard assembly number     : r\nPower supply part number        : \r\nMotherboard serial number       : \r\nPower supply serial number      : \r\nModel revision number           : J0\r\nMotherboard revision number     : A0\r\nModel number                    : \r\nSystem serial number            : \r\nTop Assembly Part Number        : \r\nTop Assembly Revision Number    : B0\r\nVersion ID                      : V05\r\nCLEI Code Number                : \r\nHardware Board Revision Number  : 0x01\r\n\r\n\r\nSwitch Ports Model              SW Version            SW Image                 \r\n------ ----- -----              ----------            ----------               \r\n*    1 26    =       12.2(55)SE11          -M     \r\n\r\n\r\nConfiguration register is 0xF\r\n\r\nHOSTNAME#"
        ]
    }
}

show 运行 命令的输出:

TASK [run show commands] ***************************************************************************************************************************************************
changed: [IP]

将此块放在您的 2 个任务之间,然后再次 运行,并使用输出编辑您的第一个 post。

 - debug:
     msg: "{{ output }}"

这是可能的:

- copy:
    content: "{{ output.output }}"
    dest: "/home/user/telnettest.txt"

为了获得漂亮的输出,请在您的 /etc/ansible/ansible.cfg、Ansible 2.5+

中进行设置
stdout_callback = yaml
bin_ansible_callbacks = True