using Ansible nxos_config, I want to delete a file from bootflash: and sending a 'y' but I'm getting an error - ansible

I wonder if you can assist please?
I'm writing a simple Ansible play to delete a file from Nexus 3k's bootflash
when I issue the command locally:
N3K# del bootflash:1.txt
Do you want to delete "/1.txt" ? (yes/no/abort) [y]
therefore I'm sending a "y' in the playbook
---
- name: Upgrading Nexus
connection: network_cli
hosts: n3k
vars:
to_delete: '*.txt'
tasks:
- name: delete a file
nxos_config:
commands:
- del bootflash:1.txt
- echo 'y'
When I run this play, I get:
TASK [delete a file] **********************************************************************************************************************************************************************
task path: /etc/ansible/brrrr.yml:8
<el-cagcc00-01mnl03> ESTABLISH LOCAL CONNECTION FOR USER: root
<el-cagcc00-01mnl03> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /root/.ansible/tmp/ansible-local-25838fJQR1e/ansible-tmp-1573503060.87-17981903721769 `" && echo ansible-tmp-1573503060.87-17981903721769="` echo /root/.ansible/tmp/ansible-local-25838fJQR1e/ansible-tmp-1573503060.87-17981903721769 `" ) && sleep 0'
Using module file /usr/lib/python2.7/site-packages/ansible/modules/network/nxos/nxos_config.py
<el-cagcc00-01mnl03> PUT /root/.ansible/tmp/ansible-local-25838fJQR1e/tmpxuqVCz TO /root/.ansible/tmp/ansible-local-25838fJQR1e/ansible-tmp-1573503060.87-17981903721769/AnsiballZ_nxos_config.py
<el-cagcc00-01mnl03> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-local-25838fJQR1e/ansible-tmp-1573503060.87-17981903721769/ /root/.ansible/tmp/ansible-local-25838fJQR1e/ansible-tmp-1573503060.87-17981903721769/AnsiballZ_nxos_config.py && sleep 0'
<el-cagcc00-01mnl03> EXEC /bin/sh -c '/usr/bin/python /root/.ansible/tmp/ansible-local-25838fJQR1e/ansible-tmp-1573503060.87-17981903721769/AnsiballZ_nxos_config.py && sleep 0'
<el-cagcc00-01mnl03> EXEC /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-local-25838fJQR1e/ansible-tmp-1573503060.87-17981903721769/ > /dev/null 2>&1 && sleep 0'
The full traceback is:
WARNING: The below traceback may *not* be related to the actual failure.
File "/tmp/ansible_nxos_config_payload_i5IR9V/ansible_nxos_config_payload.zip/ansible/module_utils/network/nxos/nxos.py", line 187, in load_config
resp = connection.edit_config(config, replace=replace)
File "/tmp/ansible_nxos_config_payload_i5IR9V/ansible_nxos_config_payload.zip/ansible/module_utils/connection.py", line 186, in __rpc__
raise ConnectionError(to_text(msg, errors='surrogate_then_replace'), code=code)
fatal: [el-cagcc00-01mnl03]: FAILED! => {
"changed": false,
"invocation": {
"module_args": {
"after": null,
"auth_pass": null,
"authorize": null,
"backup": false,
"backup_options": null,
"before": null,
"commands": [
"del bootflash:1.txt",
"echo 'y'"
],
"defaults": false,
"diff_against": null,
"diff_ignore_lines": null,
"host": null,
"intended_config": null,
"lines": [
"del bootflash:1.txt",
"echo 'y'"
],
"match": "line",
"parents": null,
"password": null,
"port": null,
"provider": null,
"replace": "line",
"replace_src": null,
"running_config": null,
"save_when": "never",
"src": null,
"ssh_keyfile": null,
"timeout": null,
"transport": null,
"use_ssl": null,
"username": null,
"validate_certs": null
}
},
"msg": "timeout value 30 seconds reached while trying to send command: del bootflash:1.txt"
}
PLAY RECAP ********************************************************************************************************************************************************************************
el-cagcc00-01mnl03 : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
Please advise
Thanx in advance

Try this
tasks:
- name: delete a file
nxos_command:
commands:
- delete bootflash:///1.txt no prompt

Related

Ansible - how are tasks executed on host machine? Manually executing a failed task on host machine to see the output

One of my ansible tasks is failing without an error trace.The only message was that there was a non-zero return code (caused due to set -e ?) on running the bitbucket diy backup script. This was running earlier but is failing now after making some changes (enabling imdsv2) on the ec2 bitbucket server.
It was recommended that I manually run the task on the host to see if I can get some output, but I find myself needing to recreate the entire directory of shell scripts on the host machine to do that.
So I have 2 questions -
Is there a better way to approach this?
How would I be able to run the task from host machine from the point of failure? Some tasks before it were successful, would ansible create a directory for running tasks? And I can continue there?
Sorry I am really new to ansible, and tried going over the docs, but couldn't find a way to debug this stuff properly.
The debug log on ansible doesn't give any helpful output -
<ip-addr> (1, b'\\r\\n{"changed": true, "stdout": "", "stderr": "", "rc": 22, "cmd": ["/apps/bitbucket/atlassian-bitbucket-diy-backup/bitbucket.diy-backup.sh"], "start": "2023-02-16 22:31:18.187412", "end": "2023-02-16 22:31:18.280648", "delta": "0:00:00.093236", "failed": true, "msg": "non-zero return code", "invocation": {"module_args": {"_raw_params": "/apps/bitbucket/atlassian-bitbucket-diy-backup/bitbucket.diy-backup.sh", "_uses_shell": false, "warn": false, "stdin_add_newline": true, "strip_empty_ends": true, "argv": null, "chdir": null, "executable": null, "creates": null, "removes": null, "stdin": null}}}\\r\\n', b'Shared connection to ip-addr closed.\\r\\n')
<ip-addr> Failed to connect to the host via ssh: Shared connection to ip-addr closed.
<ip-addr> ESTABLISH SSH CONNECTION FOR USER: ubuntu
<ip-addr> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="ubuntu"' -o ConnectTimeout=10 -o 'ControlPath="/runner/cp/72bdb94d8e"' ip-addr '/bin/sh -c '"'"'rm -f -r /home/ubuntu/.ansible/tmp/ansible-tmp-1676547077.8146338-376-91399646558620/ > /dev/null 2>&1 && sleep 0'"'"''
<ip-addr> (0, b'', b'')
fatal: [ip-addr]: FAILED! => {
"changed": true,
"cmd": [
"/apps/bitbucket/atlassian-bitbucket-diy-backup/bitbucket.diy-backup.sh"
],
"delta": "0:00:00.093236",
"end": "2023-02-16 22:31:18.280648",
"invocation": {
"module_args": {
"_raw_params": "/apps/bitbucket/atlassian-bitbucket-diy-backup/bitbucket.diy-backup.sh",
"_uses_shell": false,
"argv": null,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true,
"warn": false
}
},
"msg": "non-zero return code",
"rc": 22,
"start": "2023-02-16 22:31:18.187412",
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": []
}
PLAY RECAP *********************************************************************
ip-addr : ok=13 changed=2 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
So I tried manually running the script on the host machine but I'm not sure I did it right. I expected an error trace for the bitbucket diy backup script, added some echo statements to see where it went and doesn't output anything after this line (should ideally output debug)
set -e
echo "start" # manually added
SCRIPT_DIR=$(dirname "$0")
source "${SCRIPT_DIR}/utils.sh"
echo "debug" #manually added

Ansible extract values from stdout_lines to use in subsequent loop

I am trying to write a role to remove all helper addresses for a Cisco Router interface. There can be one or more helper addresses returned in stdout_lines. I use filters to remove labels and extra scapes from the lines and put the results into an array to use in the subsequent command loop. I seem to get the correct info but using the value fails.
Here is my role:
---
- name: read helpers
register: helper_facts
ios_command:
commands:
- show ip interface {{ interface }} | section Helper
- set_fact:
helper_addr: []
- set_fact:
helper_addr: "{{ helper_addr }} + [ '{{ item | regex_replace ('Helper address.* ','') | trim }}' ]"
with_items: "{{ helper_facts.stdout_lines }}"
- name: remove helpers
ios_command:
commands:
- no ip helper-address {{ item }}
with_items: "{{ helper_addr }}"
Here is a snip of the output with the error I get:
ok: [ONTMDFRTR01] => (item= 172.16.0.22) => {
"ansible_facts": {
"helper_addr": [
"192.1.0.1",
"172.16.0.22"
]
},
"ansible_loop_var": "item",
"changed": false,
"item": " 172.16.0.22"
}
TASK [remove_helpers : remove helpers] **********************************************************************************
task path: /home/msimon/USPS/roles/remove_helpers/tasks/main.yml:15
Monday 03 May 2021 16:44:23 -0400 (0:00:00.704) 0:00:05.874 ************
<10.32.178.252> ESTABLISH LOCAL CONNECTION FOR USER: root
<10.32.178.252> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /root/.ansible/tmp/ansible-local-23291P_KVGE `"&& mkdir "1-258083848203070 `" && echo ansible-tmp-1620074664.59-23371-258083848203070="` echo /root/.ansible/tmp/ansible-local-232
Using module file /usr/lib/python2.7/site-packages/ansible/modules/network/ios/ios_command.py
<10.32.178.252> PUT /root/.ansible/tmp/ansible-local-23291P_KVGE/tmpuSDML5 TO /root/.ansible/tmp/ansible-local-23291P_KVG
<10.32.178.252> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-local-23291P_KVGE/ansible-tmp-1620074664.59-23371-24.59-23371-258083848203070/AnsiballZ_ios_command.py && sleep 0'
<10.32.178.252> EXEC /bin/sh -c '/usr/bin/python /root/.ansible/tmp/ansible-local-23291P_KVGE/ansible-tmp-1620074664.59-2
<10.32.178.252> EXEC /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-local-23291P_KVGE/ansible-tmp-1620074664.59-23371-25
The full traceback is:
WARNING: The below traceback may *not* be related to the actual failure.
File "/tmp/ansible_ios_command_payload_IFZQ7s/ansible_ios_command_payload.zip/ansible/module_utils/network/ios/ios.py",
return connection.run_commands(commands=commands, check_rc=check_rc)
File "/tmp/ansible_ios_command_payload_IFZQ7s/ansible_ios_command_payload.zip/ansible/module_utils/connection.py", line
raise ConnectionError(to_text(msg, errors='surrogate_then_replace'), code=code)
failed: [ONTMDFRTR01] (item=192.1.0.1) => {
"ansible_loop_var": "item",
"changed": false,
"invocation": {
"module_args": {
"auth_pass": null,
"authorize": null,
"commands": [
"no ip helper-address 192.1.0.1"
],
"host": null,
"interval": 1,
"match": "all",
"password": null,
"port": null,
"provider": null,
"retries": 10,
"ssh_keyfile": null,
"timeout": null,
"username": null,
"wait_for": null
}
},
"item": "192.1.0.1",
"msg": "no ip helper-address 192.1.0.1\r\nno ip helper-address 192.1.0.1\r\n ^\r\n% Invalid input detected at '^' m
}```
Thanks to mdaniel's comment I found the answer. I cut this from a larger playbook and left out the commands to setup the interface ("config t" , "int GigabitEthernet0/0/0") needed before the no helper_addres command. My role is now working as expected. Thanks, I hope this helps someone else.

sshfs with ansible does not give the same result as running it manually on the host

I am working on backups for my server. I am using sshfs for this. When wanting to back up a folder the backup server asks for a password. This is what my task (handler) looks like:
- name: Mount backup folder
become: yes
expect:
command: "sshfs -o allow_other,default_permissions {{ backup_server.user }}#{{ backup_server.host }}:/ /mnt/backup"
echo: yes
responses:
(.*)password(.*): "{{ backup_server.password }}"
(.*)Are you sure you want to continue(.*): "yes"
listen: mount-backup-folder
It runs and produces this output:
changed: [prod1.com] => {
"changed": true,
"cmd": "sshfs -o allow_other,default_permissions user#hostname.com:/ /mnt/backup",
"delta": "0:00:00.455753",
"end": "2021-01-26 14:57:34.482440",
"invocation": {
"module_args": {
"chdir": null,
"command": "sshfs -o allow_other,default_permissions user#hostname.com:/ /mnt/backup",
"creates": null,
"echo": true,
"removes": null,
"responses": {
"(.*)Are you sure you want to continue(.*)": "yes",
"(.*)password(.*)": "password"
},
"timeout": 30
}
},
"rc": 0,
"start": "2021-01-26 14:57:34.026687",
"stdout": "user#hostname.com's password: ",
"stdout_lines": [
"user#hostname.com's password: "
]
}
But when I go to the server the folder is not synced with the backup server. BUT when I run the command manually:
sshfs -o allow_other,default_permissions user#hostname.com:/ /mnt/backup
The backup DOES work. Does anybody know how this is possible?
I suspect sshfs was killed by SIGHUP. I know nothing about Ansible so don't know if it has the official way to ignore SIGHUP. As a workaround you can write like this:
expect:
command: bash -c "trap '' HUP; sshfs -o ..."
I installed sshfs and verified this bash -c "trap ..." workaround with Expect (spawn -ignore HUP ...) and sexpect (spawn -nohup ...). I believe it'd also work with Ansible (seems like its expect module uses Python's pexpect).

How to decode a Base64 var to a binary file with Ansible module

I am reading a base64 file from HashiCorp’s vault with the help of the hashi_vault module. Sample of code:
- name: Vault get b64.pfx file
set_fact:
b64_pfx: "{{ lookup('hashi_vault',
'secret={{ path_pfx }} token={{ token }} url={{ url }} cacert={{ role_path}}/files/CA.pem')}}"
Then as a next step I need to decode this base64 var to a binary format and store it in in a file. I am currently using shell module to do the work. Sample of code:
- name: Decode Base64 file to binary
shell: "echo {{ b64_pfx }} | base64 --decode > {{ pfxFile }}"
delegate_to: localhost
I was looking online for possible solutions e.g. (
Copy module with base64-encoded binary file adds extra character and How to upload encrypted file using ansible vault?).
But the only working solution that I can found is using the shell module. Since this is an old problem is there any workaround on this?
Update:
Do not use Python 2.7 as there seems to be a bug on the b64decode filter (sample below):
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: root
<localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /tmp/ansible-tmp-1573819503.84-50241917358990 `" && echo ansible-tmp-1573819503.84-50241917358990="` echo /tmp/ansible-tmp-1573819503.84-50241917358990 `" ) && sleep 0'
Using module file /usr/lib/python2.7/site-packages/ansible/modules/commands/command.py
<localhost> PUT /tmp/ansible-local-18pweKi1/tmpjQGOz8 TO /tmp/ansible-tmp-1573819503.84-50241917358990/AnsiballZ_command.py
<localhost> EXEC /bin/sh -c 'chmod u+x /tmp/ansible-tmp-1573819503.84-50241917358990/ /tmp/ansible-tmp-1573819503.84-50241917358990/AnsiballZ_command.py && sleep 0'
<localhost> EXEC /bin/sh -c '/usr/bin/python /tmp/ansible-tmp-1573819503.84-50241917358990/AnsiballZ_command.py && sleep 0'
<localhost> EXEC /bin/sh -c 'rm -f -r /tmp/ansible-tmp-1573819503.84-50241917358990/ > /dev/null 2>&1 && sleep 0'
changed: [hostname -> localhost] => {
"changed": true,
"cmd": "shasum -a 1 /tmp/binary_file\nshasum -a 1 /tmp/binary_file.ansible\n",
"delta": "0:00:00.126279",
"end": "2019-11-15 13:05:04.227933",
"invocation": {
"module_args": {
"_raw_params": "shasum -a 1 /tmp/binary_file\nshasum -a 1 /tmp/binary_file.ansible\n",
"_uses_shell": true,
"argv": null,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true,
"warn": true
}
},
"rc": 0,
"start": "2019-11-15 13:05:04.101654",
"stderr": "",
"stderr_lines": [],
"stdout": "4a71465d449a0337329e76106569e39d6aaa5ef0 /tmp/binary_file\nead5cb632f3ee80ce129ef5fe02396686c2761e0 /tmp/binary_file.ansible",
"stdout_lines": [
"4a71465d449a0337329e76106569e39d6aaa5ef0 /tmp/binary_file",
"ead5cb632f3ee80ce129ef5fe02396686c2761e0 /tmp/binary_file.ansible"
]
}
Solution: use Python 3 with b64decode filter (sample below):
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: root
<localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /tmp/ansible-tmp-1573819490.9511943-224511378311227 `" && echo ansible-tmp-1573819490.9511943-224511378311227="` echo /tmp/ansible-tmp-1573819490.9511943-224511378311227 `" ) && sleep 0'
Using module file /usr/local/lib/python3.6/site-packages/ansible/modules/commands/command.py
<localhost> PUT /tmp/ansible-local-18epk_0jsv/tmp4t3gnm7u TO /tmp/ansible-tmp-1573819490.9511943-224511378311227/AnsiballZ_command.py
<localhost> EXEC /bin/sh -c 'chmod u+x /tmp/ansible-tmp-1573819490.9511943-224511378311227/ /tmp/ansible-tmp-1573819490.9511943-224511378311227/AnsiballZ_command.py && sleep 0'
<localhost> EXEC /bin/sh -c '/usr/bin/python /tmp/ansible-tmp-1573819490.9511943-224511378311227/AnsiballZ_command.py && sleep 0'
<localhost> EXEC /bin/sh -c 'rm -f -r /tmp/ansible-tmp-1573819490.9511943-224511378311227/ > /dev/null 2>&1 && sleep 0'
changed: [hostname -> localhost] => {
"changed": true,
"cmd": "shasum -a 1 /tmp/binary_file\nshasum -a 1 /tmp/binary_file.ansible\n",
"delta": "0:00:00.135427",
"end": "2019-11-15 13:04:51.239969",
"invocation": {
"module_args": {
"_raw_params": "shasum -a 1 /tmp/binary_file\nshasum -a 1 /tmp/binary_file.ansible\n",
"_uses_shell": true,
"argv": null,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true,
"warn": true
}
},
"rc": 0,
"start": "2019-11-15 13:04:51.104542",
"stderr": "",
"stderr_lines": [],
"stdout": "4a71465d449a0337329e76106569e39d6aaa5ef0 /tmp/binary_file\n4a71465d449a0337329e76106569e39d6aaa5ef0 /tmp/binary_file.ansible",
"stdout_lines": [
"4a71465d449a0337329e76106569e39d6aaa5ef0 /tmp/binary_file",
"4a71465d449a0337329e76106569e39d6aaa5ef0 /tmp/binary_file.ansible"
]
}
Since Python 2 is reaching the end of life in (January 1, 2020) there is no point of raising the bug.
Using the b64decode filter at least on ansible 2.9 does what you want:
- copy:
dest: '{{ pfxFile }}'
content: '{{ b64_pfx | b64decode }}'
delegate_to: localhost
I confirmed it writes only the specified bytes (no trailing whitespace) and is binary safe.
If you tried that behavior, and it doesn't work for you, then update your question to say that and to include the version of ansible you are using. I also think that bug you linked to has been fixed, because I tried their exact case on ansible 2.9 and it did the right thing:
- hosts: localhost
connection: local
gather_facts: no
tasks:
- set_fact:
string_in_base64: 'sxZARwIVokeqOMGPygc1S20CaGPiKDRGRzg0oSVGmCF2oXHua+9fVhriUQRd8vkmvpHoBmSsI6Y='
- copy:
dest: binary_file.ansible
content: '{{ string_in_base64 | b64decode }}'
- shell: |
echo '{{ string_in_base64 }}' | base64 --decode > binary_file
shasum -a 1 binary_file
shasum -a 1 binary_file.ansible
{
"changed": true,
"cmd": "echo 'sxZARwIVokeqOMGPygc1S20CaGPiKDRGRzg0oSVGmCF2oXHua+9fVhriUQRd8vkmvpHoBmSsI6Y=' | base64 --decode > binary_file\nshasum -a 1 binary_file\nshasum -a 1 binary_file.ansible\n",
"delta": "0:00:00.162251",
"end": "2019-11-13 13:10:56.683186",
"rc": 0,
"start": "2019-11-13 13:10:56.520935",
"stderr": "",
"stderr_lines": [],
"stdout": "7e88df04cf47019ae22e9c658b62c26b706c6ea5 binary_file\n7e88df04cf47019ae22e9c658b62c26b706c6ea5 binary_file.ansible",
"stdout_lines": [
"7e88df04cf47019ae22e9c658b62c26b706c6ea5 binary_file",
"7e88df04cf47019ae22e9c658b62c26b706c6ea5 binary_file.ansible"
]
}
This is simpler than the other answers. Just pipe the function at the end of the lookup()
- name: Vault get b64.pfx file
set_fact:
b64_pfx: "{{ lookup('hashi_vault',
'secret={{ path_pfx }} token={{ token }} url={{ url }} cacert={{ role_path}}/files/CA.pem') | b64decode }}"

Ansible command using variable input fails to execute

Below I'm trying to get a command running involving input from previous output, in this case copy flash:file01 flash:file02 with 2 derived from the REGEX_FILTERING task.
- name: REGEX_FILTERING
command: "cat /home/ubuntu/show_switch.txt"
register: line1
- debug: var="{{ line1.stdout_lines | regex_replace('(?<!\*).?') }}"
register: number
- name: COMMAND_INPUT
cli_command:
command: 'copy flash:file01 flash:file0 {{ number }}'
prompt: '[confirm]'
answer: ''
Here's the error message:
<ommited>
"stdout_lines": [
"Switch/Stack Mac Address : d4a0.2ae9.ec00",
" H/W Current",
"----------------------------------------------------------",
" 1 Member 001f.9df6.2d80 5 0 Ready ",
"*2 Master d4a0.2ae9.ec00 15 0 Ready ",
" 3 Member 0016.c733.cd00 5 0 Ready"
]
}
TASK [debug] *****************************************************************************************************************************************************************************
task path: /home/ubuntu/test1.yml:109
ok: [CSR1] => {
"2": "2"
}
TASK [COMMAND_INPUT] *********************************************************************************************************************************************************************
task path: /home/ubuntu/test1.yml:112
<192.168.255.133> ESTABLISH LOCAL CONNECTION FOR USER: ubuntu
<192.168.255.133> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/ansible-tmp-1542986468.48-164922344690120 `" && echo ansible-tmp-1542986468.48-164922344690120="` echo /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/ansible-tmp-1542986468.48-164922344690120 `" ) && sleep 0'
Using module file /usr/lib/python2.7/dist-packages/ansible/modules/network/cli/cli_command.py
<192.168.255.133> PUT /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/tmpkozHVx TO /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/ansible-tmp-1542986468.48-164922344690120/AnsiballZ_cli_command.py
<192.168.255.133> EXEC /bin/sh -c 'chmod u+x /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/ansible-tmp-1542986468.48-164922344690120/ /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/ansible-tmp-1542986468.48-164922344690120/AnsiballZ_cli_command.py && sleep 0'
<192.168.255.133> EXEC /bin/sh -c '/usr/bin/python /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/ansible-tmp-1542986468.48-164922344690120/AnsiballZ_cli_command.py && sleep 0'
<192.168.255.133> EXEC /bin/sh -c 'rm -f -r /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/ansible-tmp-1542986468.48-164922344690120/ > /dev/null 2>&1 && sleep 0'
The full traceback is:
WARNING: The below traceback may *not* be related to the actual failure.
File "/tmp/ansible_cli_command_payload_U1p385/__main__.py", line 150, in main
response = connection.get(**module.params)
File "/tmp/ansible_cli_command_payload_U1p385/ansible_cli_command_payload.zip/ansible/module_utils/connection.py", line 173, in __rpc__
raise ConnectionError(to_text(msg, errors='surrogate_then_replace'), code=code)
fatal: [CSR1]: FAILED! => {
"changed": false,
"invocation": {
"module_args": {
"answer": [
""
],
"check_all": false,
"command": "copy flash:file01 flash:file0 {'failed': False, u'2': u'2', 'changed': False}",
"prompt": [
"[confirm]"
],
"sendonly": false
}
},
"msg": "copy flash:file01 flash:file0 {'failed': False, u'2': u'2', 'changed': False}\r\n ^\r\n% Invalid input detected at '^' marker.\r\n\r\nCSR1#\r\nCSR1#"
"command": "copy flash:file01 flash:file0 {'failed': False, u'2': u'2', 'changed': False}",
Is happening because the register: number is not in fact the number, rather it's the ansible result of the debug: output. Since number is a python dict, when it is injected into your command, it is just serialized as if str() were called upon the dict
It is far more likely that what you want is to capture that variable locally in the task, to make it just be the result of the jinja2 expression, and not the result of an ansible task:
- cli_command:
command: 'copy flash:file01 flash:file0 {{ number }}'
vars:
number: "{{ line1.stdout_lines | regex_replace('(?<!\*).?') }}"
I also suspect you will be happier with line1.stdout, which is a str of the entire output, versus line1.stdout_lines which is a list of str, making the regex_replace on a list kind of weird -- I'm surprised it actually even let you do that, to be honest

Resources