I am facing problems with the syntax of my Ansible playbook. I am trying to use show commands using ansible's telnet module, following this resource: https://docs.ansible.com/ansible/latest/modules/telnet_module.html#telnet-module It works fine but my problem is when I try to add the register to store the output to a variable to save it to a text file later it gives me an error, saying that the variable does not exist indicating the error line for the copy task.
I have tried to add spaces and changed it a bit but still the error is showing
---
- 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"
}
Output of the debug task:
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#"
]
}
}
Output of the show run commands:
TASK [run show commands] ***************************************************************************************************************************************************
changed: [IP]
Place this block between your 2 tasks and run it again, and edit your first post with the output.
- debug:
msg: "{{ output }}"
This is possible:
- copy:
content: "{{ output.output }}"
dest: "/home/user/telnettest.txt"
For pretty output set this in your /etc/ansible/ansible.cfg
, Ansible 2.5+
stdout_callback = yaml
bin_ansible_callbacks = True