ansible-network / network-engine

This role provides the foundation for building network roles by providing modules and plugins that are common to all Ansible Network roles.
GNU General Public License v3.0
112 stars 53 forks source link

Fix validate_role_spec for nested spec #198

Closed ganeshrn closed 5 years ago

ganeshrn commented 5 years ago

Fixes https://github.com/ansible-network/network-engine/issues/189

eg Playbook:

---
- hosts: localhost
  connection: local
  gather_facts: no
  vars:
    mlag_setup:
      peer: test
      ipaddr: value
  tasks:
  - name: set role path
    set_fact:
      role_path: ../ansible-network.network-engine

  - name: import role
    import_role:
      name: ../ansible-network.network-engine

  - name: validate spec
    validate_role_spec:
      spec: mlag_setup_spec.yaml
    register: role_spec

  - name: set role params
    set_fact:
      "{{ item.key }}": "{{ item.value }}"
    loop: "{{ lookup('dict', role_spec.role_params, wantlist=True) }}"

  - name: print role variables
    debug:
      msg: "{{ mlag_setup }}"

Contents of spec file:

$ cat mlag_setup_spec.yaml
---
argument_spec:
  mlag_setup:
    type: dict
    options:
      state:
        type: str
        required: false
        default: present
        choices:
          - "present"
          - "absent"
        description:
          - "Desired state: present, absent"

      peer:
        type: str
        required: true
        description:
          - "The MLAG peer switch hostname (inventory_hostname)"

      vlan:
        type: int
        required: false
        default: "4094"
        description:
          - "VLAN to use for peer communication."
          - "VLAN is used to create the interface vlan for MLAG_CONTROL_PLANE."

      domain:
        type: int
        required: false
        default: "1"
        description:
          - "Represents the MLAG domain."
          - "The ID must be the same only for members of the same MLAG domain."
          - "Domain is used to create the MLAG_CONTROL_PLANE VRF with RD <site>:<domain>"

      site:
        type: int
        required: false
        default: "1"
        description:
          - "Represents the site where the domain is located."
          - "Site is used to create the MLAG_CONTROL_PLANE interface with address 169.254.<domain>.<site><device>"
          - "Site is used to create the MLAG_CONTROL_PLANE VRF with RD <site>:<domain>"

      device:
        type: int
        required: false
        default: "1"
        description:
          - "Unique ID to represent the device member of the MLAG domain"
          - "The ID is used to create the peer communication address 169.254.<domain>.<site><device>"

      linkagg:
        type: int
        required: false
        default: "1"
        description:
          - "Linkagg to use for the peer-to-peer communication."

      vlans:
        type: list
        required: false
        default:
          - "all"
        description:
          - "List of vlans member of the MLAG domain"
          - "VLANs are NOT created if they do not exist"

      interlinks:
        type: list
        required: false
        default:
          - "Ethernet1"
        description:
          - "List of physical interfaces used by linkagg as interlinks for the peer-to-peer communication"

      heartbeat:
        type: int
        default: "10000"
        required: false
        description:
          - "Timer for heartbeat in ms"

      reload_delay:
        type: int
        default: "150"
        required: false
        description:
          - "Delay to bring MLAGs up after reload."
          - "Helps to ensure that MLAGs comes up only when adjacency is done and tables are pre-populated."
          - "Prevents blackhole of traffic when device is converging and tables being populated after a reboot."

      ipv4:
        type: str
        required: true
        description:
          - "IPV4 address to use for the MLAG intercommunication"
        aliases:
        - ipaddr

      vrf:
        type: str
        required: false
        default: "mlag_control"
        description:
          - "The VRF name for the MLAG intercommunication"

      extension:
        type: dict
        required: false
        description:
          - "Extension of specs with platform-specific keys/values"

Ansible run output logs:

PLAY [localhost] ***********************************************************************************************************

TASK [set role path] *******************************************************************************************************
ok: [localhost]

TASK [validate spec] *******************************************************************************************************
ok: [localhost]

TASK [set role params] *****************************************************************************************************
ok: [localhost] => (item={'key': u'mlag_setup', 'value': {u'domain': 1, u'interlinks': [u'Ethernet1'], u'extension': None, u'vlan': 4094, 'ipaddr': u'value', u'site': 1, 'peer': u'test', u'state': u'present', u'vrf': u'mlag_control', u'reload_delay': 150, u'device': 1, u'heartbeat': 10000, u'vlans': [u'all'], u'linkagg': 1, u'ipv4': u'value'}})

TASK [print role variables] *********************************************************************************************************
ok: [localhost] => {
    "msg": {
        "device": 1,
        "domain": 1,
        "extension": null,
        "heartbeat": 10000,
        "interlinks": [
            "Ethernet1"
        ],
        "ipaddr": "value",
        "ipv4": "value",
        "linkagg": 1,
        "peer": "test",
        "reload_delay": 150,
        "site": 1,
        "state": "present",
        "vlan": 4094,
        "vlans": [
            "all"
        ],
        "vrf": "mlag_control"
    }
}

PLAY RECAP *****************************************************************************************************************
localhost                  : ok=4    changed=0    unreachable=0    failed=0    skipped=0
softwarefactory-project-zuul[bot] commented 5 years ago

Build failed.

softwarefactory-project-zuul[bot] commented 5 years ago

Build succeeded.

softwarefactory-project-zuul[bot] commented 5 years ago

Build succeeded (gate pipeline).