davidban77 / ansible-collection-gns3

Ansible Collection for GNS3 Server REST API using gns3fy
https://galaxy.ansible.com/davidban77/gns3
MIT License
38 stars 20 forks source link
ansible gns3 gns3-api gns3-server netdevops playbook

Ansible Module for GNS3

Ansible-Galaxy collections repository for GNS3 Server REST API using gns3fy - see the docs.

Installation

For the module to be used you need to have installed gns3fy

pip install gns3fy

This collections is packaged under ansible-galaxy, so to install it you need mazer from Ansible Projects:

mazer install davidban77.gns3

Features

Modules

These are the modules provided with this collection:

Examples: using the module

Here are some examples of how to use the module.

Get server version

---
- host: localhost
  # Call the collections to use the respective modules
  collections:
    - davidban77.gns3
  vars:
    gns3_url: http://localhost
  tasks:
    - name: Get the server facts
      gns3_facts:
        url: "{{ gns3_url }}"
        port: 3080
        get_images: all
        get_compute_ports: yes
      register: result

    - debug: var=result

Get nodes_inventory information from a project

---
- host: localhost
  # Call the collections to use the respective modules
  collections:
    - davidban77.gns3
  vars:
    gns3_url: http://localhost
  tasks:
    - name: Get the server version
      gns3_nodes_inventory:
        url: "{{ gns3_url }}"
        project_name: lab_example
      register: result

    - debug: var=result

Manipulate GNS3 projects

---
# Open a GNS3 project
- name: Start lab
  gns3_project:
    url: "{{ gns3_url }}"
    state: opened
    project_name: lab_example

# Stop all nodes inside an open project
- name: Stop nodes
  gns3_project:
    url: "{{ gns3_url }}"
    state: opened
    project_name: lab_example
    nodes_state: stopped
    nodes_strategy: all
    poll_wait_time: 5

# Open a GNS3 project and start nodes one by one with a delay of 10sec between them
- name: Start nodes one by one
  gns3_project:
    url: "{{ gns3_url }}"
    state: opened
    project_name: lab_example
    nodes_state: started
    nodes_strategy: one_by_one
    nodes_delay: 10

# Close a GNS3 project
- name: Stop lab
  gns3_project:
    url: "{{ gns3_url }}"
    state: closed
    project_id: "UUID-SOMETHING-1234567"

Create and delete projects

---
# Create a GNS3 project given nodes and links specifications
- name: Create a project
  gns3_project:
    url: "{{ gns3_url }}"
    state: present
    project_name: new_lab
    nodes_spec:
        - name: alpine-1
          template: alpine
        - name: alpine-2
          template: alpine
    links_spec:
        - ['alpine-1', 'eth0', 'alpine-2', 'eth1']

# Delete a GNS3 project
- name: Delete project
  gns3_project:
    url: "{{ gns3_url }}"
    state: absent
    project_name: new_lab

Examples: using the roles

There are also some convinient roles that you can use to manage your labs. Here is an example playbook:

main.yml

- hosts: localhost
  tasks:
    - import_role:
        name: create_lab
      when: execute == "create"
    - import_role:
        name: delete_lab
      when: execute == "delete"

This way you can call and switch the behaviour of the playbook:

Create the lab

ansible-playbook main.yml -e execute=create

Or delete the lab

ansible-playbook main.yml -e execute=delete

Here is the example variable file which specifies the naming convention used. You can see that the variable names come from the module itself with only gns3_

---
gns3_url: "http://dev_gns3server"
gns3_project_name: test_ansible
gns3_nodes_spec:
    - name: veos-1
      template: "vEOS-4.21.5F"
    - name: veos-2
      template: "vEOS-4.21.5F"
    - name: ios-1
      template: "IOU-15.4"
    - name: ios-2
      template: "IOU-15.4"
gns3_nodes_strategy: one_by_one
gns3_links_spec:
    - ["veos-1", "Ethernet1", "veos-2", "Ethernet1"]
    - ["veos-1", "Ethernet2", "ios-1", "Ethernet1/0"]
    - ["veos-2", "Ethernet2", "ios-2", "Ethernet1/0"]
    - ["ios-1", "Ethernet1/2", "ios-2", "Ethernet1/2"]

More examples

For more examples like create an /etc/network/interfaces file for an alpine docker node to configure its network interfaces, or restore a project to an specific snapshot, you can go to the test/playbooks directory.