Python API

Python API

Networking-Ansible can be called directly via python API. This method does not require a running OpenStack, with neutron.

In this section, this use case will be exercised in a set of example commands to show how end users could import the networking-ansible API and call it to execute switch level network configuration.

  1. In a python environment import the networking-ansible class.

    from networking_ansible.api import NetworkingAnsible
    
  2. Instantiate the NetworkingAnsible class. This requires a dictionary that represents an Ansible Inventory data structure. This data structure could be read from a file or built dynamically by the code that is instantiating the class. This example will statically assign the data structure to a variable to show the expected data structure.

    inventory = {'all':
      {'hosts':
        {'examplehost':
          {'ansible_network_os': 'openswitch',
           'ansible_host': '5.6.7.8',
           'ansible_user': 'ansible',
           'ansible_ssh_pass': 'password',
          }
        }
      }
    }
    net_ans = NetworkingAnsible(inventory)
    
  3. Call functions to configure the inventory.

    host = 'examplehost'
    port = 'port123'
    vlan_id = 37
    
    # create the VLAN
    net_ans.create_vlan(host, vlan_id)
    # configure a port in access mode on the VLAN
    net_ans.update_access_port(host, port, vlan_id)
    # shutdown the port
    net_ans.delete_port(host, port)
    # delete the VLAN
    net_ans.delete_vlan(host, vlan_id)
    
Creative Commons Attribution 3.0 License

Except where otherwise noted, this document is licensed under Creative Commons Attribution 3.0 License. See all OpenStack Legal Documents.