Setting VTP primary using cisco.ios.ios_command module

Hi all! I’m somewhat new to Ansible and looking for some help getting the Cisco ios_command module to work in my favor to set a device as VTP primary in VTPv3. I’ve tried seemingly every combination under the sun I can think of to get this to work and no dice. I have the test set up as the below targeting a device that is not VTP primary:

    - name: Make Device VTP Primary 
      cisco.ios.ios_command:
        commands:
        - command: 'vtp primary'
          prompt:
            - Enter VTP Password
            - No conflicting
          answer:
            - "{{ passwd_vtp }}"
            - confirm
      vars:
        ansible_command_timeout: 240

When I run it like this, the below is the error I get out in Ansible:

fatal: [HOSTNAME]: FAILED! => changed=false 
  msg: 'cli prompt is not identified from the last received response window: b''\r\nNo conflicting VTP3 devices found.\r\nDo you want to continue? [confirm]'''

In the actual command line entering manually, it would look like this:

HOSTNAME#vtp primary
This system is becoming primary server for feature vlan
Enter VTP Password:
No conflicting VTP3 devices found.
Do you want to continue? [confirm]
HOSTNAME#

As you can see, it’s reading and accepting my VTP password for the first prompt, but whatever I try for the second prompt, I get that same error, like it’s not matching on the regex in the prompt line I’m entering.

For additional reference, this is what our contractor discovered from the web when first starting this adventure, but it throws a VTP password mismatch error:

      cisco.ios.ios_command:
        commands:
        - command: 'vtp primary'
          prompt:
            - 'This system is becoming primary server for feature vlan'
            - 'Enter VTP Password\:'
            - 'No conflicting VTP3 devices found.'
            - 'Do you want to continue? \[confirm\]'
          answer:
            - ''
            - '{{ passwd.vtp }}'
            - ''
            - '\n'

Has anyone ever encountered this or have any suggestions? Thanks in advance!

I’m not in the office now. So, I cannot mock this up to try it out. Below is what I would try if I were in the office.

The doc site for the module doesn’t go into detail about the prompt/answer capabilities, but it seems like they are trying to mimic the expect module.

Try this:

    - name: Make Device VTP Primary 
      cisco.ios.ios_command:
        commands:
        - command: 'vtp primary'
          prompt:
            - Enter VTP Password
            - .*confirm.*
          answer:
            - "{{ passwd_vtp }}"
            - "\r"
      vars:
        ansible_command_timeout: 240

The format of the prompts in the examples implies that the prompt string are regular expressions. So, the special characters in and new lines might be messing with the behavior you are expecting.

Also, the examples only have one prompt per command. So, it could be that the module is only expecting you to use one follow up prompt per command.

If that’s the case, try this:

    - name: Make Device VTP Primary 
      cisco.ios.ios_command:
        commands:
        - command: 'vtp primary'
          prompt: Enter VTP Password
          answer: "{{ passwd_vtp }}"
        - command: "confirm"
      vars:
        ansible_command_timeout: 240

The last ditch effort is to cram all of the inputs into a single command and hope the input queue of the device properly passes them to the interactive prompts.

    - name: Make Device VTP Primary 
      cisco.ios.ios_command:
        commands: "vtp primary\r{{ passwd_vtp }}\rconfirm\r"
      vars:
        ansible_command_timeout: 240

Thanks for the help, Dustin!

Yes, I did refer to the module documentation rather thoroughly, but like you said it doesn’t really address multiple prompts. HOWEVER, this does: Working with command output and prompts in network modules — Ansible Community Documentation. Assuming this is operating in the same way as ansible.netcommon.cli_command or even using it in the background, then it SHOULD work as we tried originally. However, to your point, I think the new line characters and lack of a prompt from those is messing with things, as you said.

I tried the first option you mentioned and it gave the same “cli prompt is not identified” error I mentioned. The second option failed also, but with msg: Failed to write to ssh channel, which I’m going to guess is something to do with the fact that it’s expecting a command prompt instead of some other prompt…? The last one just threw a good old syntax error because the module just slammed it in as one big string instead of parsing the /r into a return line.

Until I get a chance to get in office and play a bit, the only other thing I could say is try the last one again with \n instead of \r.

VICTORY! It ended up having to do with the check_all setting from that ansible.netcommon.cli_command module I mentioned. I noticed in there it referenced that had to be true with multiple prompts and having that set or else it would cause each prompt to be answered by the first answer every time. It seems like it actually may do a little more than that in reality though. Thanks again for the help and guidance, Dustin!

Here’s the final working code fragment:

- name: Make Device VTP Primary 
  cisco.ios.ios_command:
    commands:
    - command: 'vtp primary'
      check_all: true
      prompt: 
        - Enter VTP Password
        - No conflicting
      answer:
        - "{{ passwd_vtp }}"
        - y
  vars:
    ansible_command_timeout: 90
1 Like

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