ansible / ansible-modules-core

Ansible modules - these modules ship with ansible
1.3k stars 1.95k forks source link

Unclear if ios_command allows to 'config t' commands #3921

Closed pporada-gl closed 8 years ago

pporada-gl commented 8 years ago
ISSUE TYPE

ios_command

ANSIBLE VERSION
ansible 2.1.0.0
  config file = 
  configured module search path = Default w/o overrides
CONFIGURATION
[defaults]
inventory=hosts
nocows=1
scp_if_ssh=true
timeout=5
OS / ENVIRONMENT

Ubuntu 16.04

SUMMARY

I have a list of commands to send to a switch. It appears as if commands that require config t are skipped or fail to execute.

STEPS TO REPRODUCE
- name: Cisco 2960 XR Switch vlan swap
  hosts: cisco_switch_2960xr_nonpoe
  connection: local
  gather_facts: false
  remote_user: "{{device_username}}"
  tasks:

    - name: Define provider
      set_fact:
        provider:
          host: "{{inventory_hostname}}"
          username: "{{device_username}}"
          password: "{{device_password}}"
          auth_pass: "{{device_secret_password}}"
          timeout: 10

    - name: Change interface VLAN
      ios_command:
        provider: "{{provider}}"
        authorize: true
        commands:
          - config t
          - interface gi1/0/5
          - switchport access vlan 42

    - debug:
        msg: "{{output.stdout_lines}}"
EXPECTED RESULTS

The switchport changes to the correct vlan as specified by the command list.

Switch# config t
Switch(config)# interface gi1/0/5
Switch(config-if)# switchport access vlan 42
ACTUAL RESULTS
ok: [A.B.C.D] => {
    "msg": [
        [
            "Enter configuration commands, one per line.  End with CNTL/Z."
        ], 
        [
            ""
        ], 
        [
            ""
        ]
    ]
}

The interface does not switch to the appropriate vlan.

alikins commented 8 years ago

Switch# config t Switch(config)# interface gi1/0/5 Switch(config-if)# switchport access vlan 42

Could the cli prompt "Switch(config)#" be confusing ios_commands.py? Looks like there is a regex to match cli prompts at: https://github.com/ansible/ansible/blob/devel/lib/ansible/module_utils/ios.py#L40 If that regex fails to match, potentially means this is a bug in ansible/ansible module_utils.

But that's just a guess, I haven't tested that regex and know nothing about ios. Reassigning to @privateip for more knowledgable take.

privateip commented 8 years ago

Please do not use ios_command to send configuration commands to the device, thats the purpose of ios_config or ios_template :) In a future release (2.2), ios_command will not accept a list of commands that include configuration.

If there is some functionality that is missing in either ios_config or ios_template that prevents its use, please open a feature request issue.

pporada-gl commented 8 years ago

Would it be possible to get an example of what I was trying to do, but with ios_config? I don't quite understand the syntax from the documentation.

privateip commented 8 years ago
- name: Cisco 2960 XR Switch vlan swap
  hosts: cisco_switch_2960xr_nonpoe
  connection: local
  gather_facts: false

  vars:
    provider:
      host: "{{ inventory_hostnae }}"
      username: "{{ device_username }}"
      password: "{{ device_password }}"
      auth_pass: "{{ device_secret_password }}"
      authorize: yes
      timeout: 10

  tasks:
    - name: Change interface VLAN
      ios_config:
        lines:
          - switchport access vlan 42
        parents: interface GigabitEthernet1/0/5
        provider: "{{ provider }}"
      register: output

    - debug:
        msg: "{{output.stdout_lines}}"
pporada-gl commented 8 years ago

Thank you. I appreciate that very much.