Interactive Commands via Ansible

Hi all, I am struggling to find documentation or a module that will help me resolve this issue that I am experiencing.

The requirement - I am SSHing into a Cisco IOS device and need to run one command that is interactive.

Example of how to do it manually (I removed the values for variables for clarity):

TEST-ROUTER# copy tftp://{{ tftp_server }}/{{ filename }} bootflash://{{ filename }} vrf {{number}}
Destination filename [XXXXXXXXXXXXXX.crt]? XXXXXXXXXXXXXX.crt
Accessing tftp://XX.XX.XX.XX/XXXXXXXXXXXXXX.crt…
Loading XXXXXXXXXXXXXX.crt from XX.XX.XX.XX (via Sdwan-system-intf): !
[OK - 5280 bytes]
5280 bytes copied in 2.639 secs (1989 bytes/sec)

So as you can see, after providing the command “copy tftp://{{ tftp_server }}/{{ filename }} bootflash://{{ filename }} vrf {{number}}”, the system prompts for the destination filename which is simply the filename passed.

I have attempted using the ansible .builtin.expect and .ios.ios_command, however, the ansible playbook continues to timeout on the first command “copy tftp://{{ tftp_server }}/{{ filename }} bootflash://{{ filename }} vrf {{number}}”.

Here is the task that is failing:

- name: Push certificate via TFTP if certificate is not present
  when: dir_check.stdout is not defined or filename not in (dir_check.stdout[0] | default(''))
  cisco.ios.ios_command:
    commands:
      - "copy tftp://{{ tftp_server }}/{{ filename }} bootflash://{{ filename }} vrf 1"
      - "{{ filename }}"
  register: copy_output

Can you provide the output of ansible-playbook -vvv ... when you run this.

Have you see the Network prompt guide

Though a cleaner solution might be to use the net_put module to copy the file onto the destination. You might need a task before that to copy from the tftp server to the machine running Ansible, unless it’s available as a network mount somewhere.

1 Like

Hi @gundalow,

Thank you for this documentation. When using the ‘ansible.netcommon.cli_command’, it is working.

The final product looks as such:

- name: Push certificate via TFTP if certificate is not present
  when: dir_check.stdout is not defined or filename not in (dir_check.stdout[0] | default(''))
  ansible.netcommon.cli_command:
    command: "copy tftp://{{ tftp_server }}/{{ filename }} bootflash://{{ filename }} vrf 1"
    prompt: "Destination filename.*"
    answer: '{{ filename }}'
  register: copy_output
1 Like

Glad it’s working, can you please mark my the solution post as “solved”, so it will help others in the future. Thanks

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.