How to write script for multiple prompts?

50 views
Skip to first unread message

Vikram S

unread,
Mar 24, 2021, 12:39:05 PM3/24/21
to Ansible Project
Hi,

Below is the command i need executed as part of IOS upgrade activiity. As you can see, it involves 2 prompts. So how do i write a script using ios_command module when multiple prompts are involved?


install add file flash:cat9k_lite_iosxe.16.12.01.SPA.bin activate commit

This operation requires a reload of the system. Do you want to proceed?

Please confirm you have changed boot config to flash:packages.conf [y/n]y

--- Starting initial file syncing ---

This operation requires a reload of the system. Do you want to proceed? [y/n]y


Will the below script work?

    ios_command:

      commands:

        command: install add file flash:cat9k_lite_iosxe.16.12.01.SPA.bin activate commit

prompt: ‘Do you want to proceed?' 

 answer: 'y' 

 prompt: ‘Do you want to proceed?' 

 answer: 'y'

Teofil Mihance

unread,
Mar 30, 2021, 6:43:43 AM3/30/21
to Ansible Project
Hi, 

I end up using shell scripts for these ios interaction. ios_command and other modules are not handling in a right way the timeouts:
For some new ios version this command in one shot and without interaction could be useful:
install add file flash:cat9k_lite_iosxe.16.12.04.SPA.bin activate commit prompt-level none
For old ios versions i created shell script for prompting also:
---

     - name: Install the new image for C3850 model switch        
       shell: |
         spawn ssh {{ansible_user}}@{{inventory_hostname}}
         expect "*?assword:*"
         send "{{ansible_password}}\r"
         expect "#"
         send "install add file flash:{{ new_ver }}\r"
         set timeout 300
         expect "#"
         send "install activate\r"
         set timeout 300
         expect "This operation requires a reload of the system. Do you want to proceed? [y/n]"
         send "y\r"
         set timeout 120
         expect "#"
         set timeout 1200
         sleep 300
         exit 0

       args:
         executable: /usr/bin/expect
       delegate_to: localhost 
      
     - name: Commit        
       shell: |
         spawn ssh {{ansible_user}}@{{inventory_hostname}}
         expect "*?assword:*"
         send "{{ansible_password}}\r"
         expect "#"
         send "install commit\r"
         set timeout 300
         expect "#"
         exit 0

       args:
         executable: /usr/bin/expect
       delegate_to: localhost 

BR
Teo

Reply all
Reply to author
Forward
0 new messages