# Help, advice, opinion needed about a new module for a switch

**URL:** <https://forum.ansible.com/t/help-advice-opinion-needed-about-a-new-module-for-a-switch/16351>\
**Category:** Ansible Developer\
**Created:** [September 26, 2014, 3:08am UTC](https://forum.ansible.com/t/help-advice-opinion-needed-about-a-new-module-for-a-switch/16351 "2014-09-26T03:08:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Patrick\_Galbraith](https://avatars.discourse-cdn.com/v4/letter/p/48db29/32.png) [@Patrick\_Galbraith](https://forum.ansible.com/u/Patrick_Galbraith)\
**Post date:** [September 26, 2014, 3:08am UTC](https://forum.ansible.com/t/help-advice-opinion-needed-about-a-new-module-for-a-switch/16351/1 "2014-09-26T03:08:10Z")

</div>

Hi all,

I had posted to this list a month or so ago about writing a module for an HP switch (HP V1910-24G Switch, Comware 5.0-based).

This has been quite a learning experience!

I reviewed this with my team today and there were some concerns that I might’ve taken the wrong approach.

I’ll explain what I’m dealing with:

1. This switch has an ssh interface and web UI (no exposed REST API that I can find)

2. This switch has a limited command-set that you have to enable (a “developer command-line mode”) if you want to do any useful work. Enabling means you type in a command, answer ‘Y’ to confirm, then type in a password ([http://h30499.www3.hp.com/t5/Web-and-Unmanaged/How-limited-is-the-1910-CLI/td-p/5966697#.VCTQ-ktZsnj](http://h30499.www3.hp.com/t5/Web-and-Unmanaged/How-limited-is-the-1910-CLI/td-p/5966697#.VCTQ-ktZsnj))

3. This switch has a very specific command set and levels of views within it

4. This switch doesn’t offer the ability, that I can find, to be able to do anything such as get a python binary on it (the Arista switch guys seem to do this)

What I wrote:

1. I originally looked at a connection plugin, but the connection isn’t the problem
2. The problem I had using either the default ssh or paramiko, I originally though was the ssh part. It isn’t, it is that Ansible attempts to create a temporary directory on the host it is running against. The switch can’t do this before turning on the “developer command-line mode”.
3. I needed up writing a module that uses paramiko, but from within the module itself, so this module has to use “local\_action” and supply a hostname, username, and password in the playbook to connect.

My playbooks appear like:

# file: switch.yml

- hosts: localhost  
tasks:
- name: set switch in developer mode  
local\_action:  
module: hpswitch  
developer\_mode: true  
host: 192.168.x.x  
save: true  
username: admin  
password: xxx  
timeout: 30  
vlans:
- name: VLAN 11  
id: 11  
interfaces:
- GigabitEthernet1/0/9
- GigabitEthernet1/0/10  
state: present
- name: VLAN 12  
id: 12  
interfaces:
- GigabitEthernet1/0/11
- GigabitEthernet1/0/12  
state: present

Which would create two VLANs. Or, I can use this form:

# file: switch.yml

- hosts: localhost  
tasks:
- name: set switch in developer mode  
hpswitch: developer\_mode=true host=192.168.x.x name=admin password=xxxx

It was pointed out to me the way Arista does it as something to look at:

```auto
  tasks:
         - name: create vlan 999
           action: arista_vlan vlan_id=999 logging=true

         - name: create / edit vlan 999
           action: arista_vlan vlan_id=999 name=test logging=true

         - name: remove vlan 999
           action: arista_vlan vlan_id=999 state=absent logging=true

```

This is very nice, but the Arista router has completely different features and it seems some sort of posix shell as well as python to use. I need to be very particular in how I talk to my switch.

The paramiko code I used I had to specifically use channel.send(cmd) and then read everything I could with channel.read() (as opposed to running exec\_command()).

So, it seems to me that I need to somehow “hijack” the connection and have developer mode turned on before Ansible creates the temporary directory – and evan that (temp dir) I don’t know will work.

So, first off -

1. What are the thoughts on the way I did this?
2. Is there a way to use inventory hostname such that my playbook, even though using a local action, will use a connection to the remote machine without having it be in the playbook?
3. Is having a remote temp dir required? Can it be turned off?
4. Has anyone out there done anything like this?

Thank you!

PS. My fork is: [GitHub - CaptTofu/ansible: Ansible is a radically simple IT automation platform that makes your applications and systems easier to deploy. Avoid writing scripts or custom code to deploy and update your applications— automate in a language that approaches plain English, using SSH, with no agents to install on remote systems.](https://github.com/CaptTofu/ansible), the features/hp\_switch branch (work in progress!)

---

<div class="post-metadata">

**Author:** ![Patrick\_Galbraith](https://avatars.discourse-cdn.com/v4/letter/p/48db29/32.png) [@Patrick\_Galbraith](https://forum.ansible.com/u/Patrick_Galbraith)\
**Post date:** [September 26, 2014, 3:10am UTC](https://forum.ansible.com/t/help-advice-opinion-needed-about-a-new-module-for-a-switch/16351/2 "2014-09-26T03:10:20Z")

</div>

small correction: channel.recv(size)

---

<div class="post-metadata">

**Author:** ![Paul\_Totterman](https://avatars.discourse-cdn.com/v4/letter/p/3ec8ea/32.png) [@Paul\_Totterman](https://forum.ansible.com/u/Paul_Totterman)\
**Post date:** [September 27, 2014, 11:53am UTC](https://forum.ansible.com/t/help-advice-opinion-needed-about-a-new-module-for-a-switch/16351/3 "2014-09-27T11:53:20Z")

</div>

> > 1. The problem I had using either the default ssh or paramiko, I originally though was the ssh part. It isn’t, it is that Ansible attempts to create a temporary directory on the host it is running against. The switch can’t do this before turning on the “developer command-line mode”.

Does ansible do this even with the raw module? [http://docs.ansible.com/raw\_module.html](http://docs.ansible.com/raw_module.html)

Cheers,  
Paul

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [September 27, 2014, 12:42pm UTC](https://forum.ansible.com/t/help-advice-opinion-needed-about-a-new-module-for-a-switch/16351/4 "2014-09-27T12:42:34Z")

</div>

Per some IRC discussion, I don’t recommend connections for _anything_ that does not have a Linux shell and filesystem, they are a conduit only for “normal” ansible modules.

In many cases,networking gear uses it’s own API to the switch via local\_action, and if an API is available over basic commands, that’s a good idea.

It may also have to use paramiko or a socket from the ansible module, but they shouldn’t be connections.

How the F5 load balancer modules or Arista networking modules work are different examples of how a module executes on the control machine and then references the device to talk to.
