From b9ec900b12bebe7045a4a1a44553cef8a86e1dda Mon Sep 17 00:00:00 2001
From: Simone Spinelli <simone.spinelli@geant.org>
Date: Mon, 15 May 2023 13:11:40 +0000
Subject: [PATCH] Remove get_facts from iptrunk role + variuos little things

---
 iptrunks.yaml                                 |  1 +
 .../tasks/iptrunks_deploy_object.yaml         |  1 +
 .../iptrunks/tasks/iptrunks_remove_trunk.yaml |  1 +
 roles/iptrunks/tasks/main.yml                 |  9 ++--
 roles/iptrunks/templates/trunk_deprovision.j2 | 12 ++---
 roles/iptrunks/vars/main.yml                  | 17 +++---
 roles/iptrunks_checks/README.md               | 38 ++++++++++++++
 roles/iptrunks_checks/defaults/main.yml       |  2 +
 roles/iptrunks_checks/handlers/main.yml       |  2 +
 roles/iptrunks_checks/meta/main.yml           | 52 +++++++++++++++++++
 roles/iptrunks_checks/tasks/check_isis.yaml   | 12 +++++
 roles/iptrunks_checks/tasks/check_ping.yaml   | 14 +++++
 roles/iptrunks_checks/tasks/main.yml          | 21 ++++++++
 roles/iptrunks_checks/tests/inventory         |  2 +
 roles/iptrunks_checks/tests/test.yml          |  5 ++
 roles/iptrunks_checks/vars/main.yml           | 31 +++++++++++
 16 files changed, 198 insertions(+), 22 deletions(-)
 create mode 100644 roles/iptrunks_checks/README.md
 create mode 100644 roles/iptrunks_checks/defaults/main.yml
 create mode 100644 roles/iptrunks_checks/handlers/main.yml
 create mode 100644 roles/iptrunks_checks/meta/main.yml
 create mode 100644 roles/iptrunks_checks/tasks/check_isis.yaml
 create mode 100644 roles/iptrunks_checks/tasks/check_ping.yaml
 create mode 100644 roles/iptrunks_checks/tasks/main.yml
 create mode 100644 roles/iptrunks_checks/tests/inventory
 create mode 100644 roles/iptrunks_checks/tests/test.yml
 create mode 100644 roles/iptrunks_checks/vars/main.yml

diff --git a/iptrunks.yaml b/iptrunks.yaml
index 39a70944..3ed18ab2 100644
--- a/iptrunks.yaml
+++ b/iptrunks.yaml
@@ -1,6 +1,7 @@
 - name: Manage iptrunks playbook
   hosts: all
   remote_user: admin
+  gather_facts: no
   vars_files:
     - /opt/ansible_inventory/group_vars/junos/device_access.yaml
   roles:
diff --git a/roles/iptrunks/tasks/iptrunks_deploy_object.yaml b/roles/iptrunks/tasks/iptrunks_deploy_object.yaml
index 6d4a5f6f..e74eea20 100644
--- a/roles/iptrunks/tasks/iptrunks_deploy_object.yaml
+++ b/roles/iptrunks/tasks/iptrunks_deploy_object.yaml
@@ -22,6 +22,7 @@
     format: text
     config_mode: "private"
     commit: yes
+    comment: "{{commit_comment}}"
   register:  response
   when: verb == "deploy" and not (dry_run | ansible.builtin.bool)
 
diff --git a/roles/iptrunks/tasks/iptrunks_remove_trunk.yaml b/roles/iptrunks/tasks/iptrunks_remove_trunk.yaml
index 348bb7f8..bc11df52 100644
--- a/roles/iptrunks/tasks/iptrunks_remove_trunk.yaml
+++ b/roles/iptrunks/tasks/iptrunks_remove_trunk.yaml
@@ -22,6 +22,7 @@
     format: set
     config_mode: "private"
     commit: yes
+    comment: "{{commit_comment}}"
   register:  response
   when: verb == "remove" and not (dry_run | ansible.builtin.bool)
 
diff --git a/roles/iptrunks/tasks/main.yml b/roles/iptrunks/tasks/main.yml
index acefc5b6..74ea9258 100644
--- a/roles/iptrunks/tasks/main.yml
+++ b/roles/iptrunks/tasks/main.yml
@@ -5,17 +5,16 @@
     msg: 
     - "Both 'verb' and 'config_object' are required."
     - "Allowed verbs: 'compile', 'deploy'. Use: -e 'verb=$verb'"
-    - "Allowed objects: 'trunk_interface', 'isis', 'isis_interface', 'ldp_interface', 'lldp_interface'. Use: -e 'config_object=$config_object'"
-    - "'$trunk_id' must be defined for this role to work."
+    - "Allowed objects: 'all', 'trunk_interface', 'isis', 'isis_interface', 'ldp_interface', 'lldp_interface'. Use: -e 'config_object=$config_object'"
   when: ((verb is not defined) or (config_object is not defined)) or (verb not in verbs) or (config_object not in config_objects)
     #when: ((verb is not defined) or (config_object is not defined) or (trunk_id is not defined)) or (verb not in verbs) or (config_object not in config_objects)
 
 - meta: end_play
   when: ((verb is not defined) or (config_object is not defined)) or (verb not in verbs) or (config_object not in config_objects)
 
-- name: Gather_facts
-  junipernetworks.junos.junos_facts:
-    gather_subset: all
+    #- name: Gather_facts
+    #  junipernetworks.junos.junos_facts:
+    #    gather_subset: all
 
 - set_fact:
     opid: "{{ lookup('community.general.random_string', length=18, special=false) }}"
diff --git a/roles/iptrunks/templates/trunk_deprovision.j2 b/roles/iptrunks/templates/trunk_deprovision.j2
index 4d17cb89..732a4897 100644
--- a/roles/iptrunks/templates/trunk_deprovision.j2
+++ b/roles/iptrunks/templates/trunk_deprovision.j2
@@ -1,5 +1,4 @@
 {% for trunk in trunks %}
-  {% if trunk.id == trunk_id  %}
     {% if inventory_hostname == trunk.config.nodeA.name  %}
     {% set local= trunk.config.nodeA %}
     {% endif %}
@@ -7,13 +6,12 @@
     {% set local= trunk.config.nodeB %}
     {% endif %}
     {% if local is defined  %}
-      delete protocols isis interface {{ local.ae_name }}
-      delete protocols ldp interface {{ local.ae_name }}
-      delete interfaces {{ local.ae_name }}
+delete protocols isis interface {{ local.ae_name }}
+delete protocols ldp interface {{ local.ae_name }}.0
+delete interfaces {{ local.ae_name }}
       {% for iface in local.members %}
-      delete interfaces {{ iface }}
-      delete protocols lldp interface {{ iface }}
+delete interfaces {{ iface }}
+delete protocols lldp interface {{ iface }}
       {% endfor %}
     {% endif %}
-  {% endif %}
 {% endfor %}
diff --git a/roles/iptrunks/vars/main.yml b/roles/iptrunks/vars/main.yml
index 6ec01588..df840cd7 100644
--- a/roles/iptrunks/vars/main.yml
+++ b/roles/iptrunks/vars/main.yml
@@ -8,6 +8,7 @@ verbs:
   - "deploy"
   - "remove"
 config_objects:
+  - "all"
   - "trunk_interface"
   - "isis"
   - "isis_interface"
@@ -15,17 +16,17 @@ config_objects:
   - "lldp_interface"
   - "trunk_deprovision"
 
-wfo_trunk: "{{wfo_trunk_json|from_json}}"
+wfo_trunk: "{{wfo_trunk_json}}"
+config_object: "{{object}}"
 
-side_a_ipv4_address: "{{ wfo_trunk.iptrunk.iptrunk_ipv4_network| ipaddr('net') | ipaddr('address')}}/31}}"
-side_a_ipv6_address: "{{ wfo_trunk.iptrunk.iptrunk_ipv4_network| ipaddr('net') | ipaddr('1') | ipaddr('address')}}/126}}"
-side_b_ipv4_address: "{{ wfo_trunk.iptrunk.iptrunk_ipv6_network| ipaddr('net') | ipaddr('1') | ipaddr('address')}}/31}}"
-side_b_ipv6_address: "{{ wfo_trunk.iptrunk.iptrunk_ipv6_network| ipaddr('net') | ipaddr('2') | ipaddr('address')}}/126}}"
+side_a_ipv4_address: "{{ wfo_trunk.iptrunk.iptrunk_ipv4_network| ipaddr('net') | ipaddr('address')}}/31"
+side_a_ipv6_address: "{{ wfo_trunk.iptrunk.iptrunk_ipv6_network| ipaddr('net') | ipaddr('1') | ipaddr('address')}}/126"
+side_b_ipv4_address: "{{ wfo_trunk.iptrunk.iptrunk_ipv4_network| ipaddr('net') | ipaddr('1') | ipaddr('address')}}/31"
+side_b_ipv6_address: "{{ wfo_trunk.iptrunk.iptrunk_ipv6_network| ipaddr('net') | ipaddr('2') | ipaddr('address')}}/126"
 
 
 trunks:
   - id: "{{wfo_trunk.iptrunk.geant_s_sid}}"
-    name: AMS2-LON-IPTRUNK-100G
     config:
       common:
         description: "{{wfo_trunk.iptrunk.iptrunk_description}}"
@@ -37,8 +38,6 @@ trunks:
         name: "{{wfo_trunk.iptrunk.iptrunk_sideA_node.device_fqdn}}"
         loopback: "{{wfo_trunk.iptrunk.iptrunk_sideA_node.device_lo_ipv4_address}}"
         ae_name: "{{wfo_trunk.iptrunk.iptrunk_sideA_ae_iface}}"
-          #ipv4_address: 62.40.98.164/31
-          #ipv6_address: 2001:798:cc::5/126
         ipv4_address: "{{side_a_ipv4_address}}" 
         ipv6_address: "{{side_a_ipv6_address}}"
         members: "{{wfo_trunk.iptrunk.iptrunk_sideA_ae_members}}"
@@ -48,8 +47,6 @@ trunks:
         name: "{{wfo_trunk.iptrunk.iptrunk_sideB_node.device_fqdn}}"
         loopback: "{{wfo_trunk.iptrunk.iptrunk_sideB_node.device_lo_ipv4_address}}"
         ae_name: "{{wfo_trunk.iptrunk.iptrunk_sideB_ae_iface}}"
-          #ipv4_address: 62.40.98.165/31
-          #ipv6_address: 2001:798:cc::6/126
         ipv4_address: "{{side_b_ipv4_address}}" 
         ipv6_address: "{{side_b_ipv6_address}}"
         members: "{{wfo_trunk.iptrunk.iptrunk_sideB_ae_members}}"
diff --git a/roles/iptrunks_checks/README.md b/roles/iptrunks_checks/README.md
new file mode 100644
index 00000000..225dd44b
--- /dev/null
+++ b/roles/iptrunks_checks/README.md
@@ -0,0 +1,38 @@
+Role Name
+=========
+
+A brief description of the role goes here.
+
+Requirements
+------------
+
+Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required.
+
+Role Variables
+--------------
+
+A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well.
+
+Dependencies
+------------
+
+A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles.
+
+Example Playbook
+----------------
+
+Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too:
+
+    - hosts: servers
+      roles:
+         - { role: username.rolename, x: 42 }
+
+License
+-------
+
+BSD
+
+Author Information
+------------------
+
+An optional section for the role authors to include contact information, or a website (HTML is not allowed).
diff --git a/roles/iptrunks_checks/defaults/main.yml b/roles/iptrunks_checks/defaults/main.yml
new file mode 100644
index 00000000..1c88a7ad
--- /dev/null
+++ b/roles/iptrunks_checks/defaults/main.yml
@@ -0,0 +1,2 @@
+---
+# defaults file for iptrunks_checks
diff --git a/roles/iptrunks_checks/handlers/main.yml b/roles/iptrunks_checks/handlers/main.yml
new file mode 100644
index 00000000..6106d35f
--- /dev/null
+++ b/roles/iptrunks_checks/handlers/main.yml
@@ -0,0 +1,2 @@
+---
+# handlers file for iptrunks_checks
diff --git a/roles/iptrunks_checks/meta/main.yml b/roles/iptrunks_checks/meta/main.yml
new file mode 100644
index 00000000..c572acc9
--- /dev/null
+++ b/roles/iptrunks_checks/meta/main.yml
@@ -0,0 +1,52 @@
+galaxy_info:
+  author: your name
+  description: your role description
+  company: your company (optional)
+
+  # If the issue tracker for your role is not on github, uncomment the
+  # next line and provide a value
+  # issue_tracker_url: http://example.com/issue/tracker
+
+  # Choose a valid license ID from https://spdx.org - some suggested licenses:
+  # - BSD-3-Clause (default)
+  # - MIT
+  # - GPL-2.0-or-later
+  # - GPL-3.0-only
+  # - Apache-2.0
+  # - CC-BY-4.0
+  license: license (GPL-2.0-or-later, MIT, etc)
+
+  min_ansible_version: 2.1
+
+  # If this a Container Enabled role, provide the minimum Ansible Container version.
+  # min_ansible_container_version:
+
+  #
+  # Provide a list of supported platforms, and for each platform a list of versions.
+  # If you don't wish to enumerate all versions for a particular platform, use 'all'.
+  # To view available platforms and versions (or releases), visit:
+  # https://galaxy.ansible.com/api/v1/platforms/
+  #
+  # platforms:
+  # - name: Fedora
+  #   versions:
+  #   - all
+  #   - 25
+  # - name: SomePlatform
+  #   versions:
+  #   - all
+  #   - 1.0
+  #   - 7
+  #   - 99.99
+
+  galaxy_tags: []
+    # List tags for your role here, one per line. A tag is a keyword that describes
+    # and categorizes the role. Users find roles by searching for tags. Be sure to
+    # remove the '[]' above, if you add tags to this list.
+    #
+    # NOTE: A tag is limited to a single word comprised of alphanumeric characters.
+    #       Maximum 20 tags per role.
+
+dependencies: []
+  # List your role dependencies here, one per line. Be sure to remove the '[]' above,
+  # if you add dependencies to this list.
diff --git a/roles/iptrunks_checks/tasks/check_isis.yaml b/roles/iptrunks_checks/tasks/check_isis.yaml
new file mode 100644
index 00000000..541ea3c4
--- /dev/null
+++ b/roles/iptrunks_checks/tasks/check_isis.yaml
@@ -0,0 +1,12 @@
+- name: Get status of ISIS
+  juniper_junos_command:
+    commands:
+      - show isis adjacency
+      - show isis interface "{{trunks[0].config.nodeA.ae_name}}".0
+  register: response
+- name: Show status of ISIS djacencies
+  debug:
+    msg: "{{ response.results[0].stdout_lines }}"
+- name: Show status of ISIS interfaces
+  debug:
+    msg: "{{ response.results[1].stdout_lines }}"
diff --git a/roles/iptrunks_checks/tasks/check_ping.yaml b/roles/iptrunks_checks/tasks/check_ping.yaml
new file mode 100644
index 00000000..8fdf9e8c
--- /dev/null
+++ b/roles/iptrunks_checks/tasks/check_ping.yaml
@@ -0,0 +1,14 @@
+- name: Ping "{{trunks[0].config.nodeB.name}} on {{trunks[0].config.nodeB.ipv4_address.split('/')[0]}}"
+  juniper_junos_ping:
+    dest: "{{trunks[0].config.nodeB.ipv4_address.split('/')[0]}}"
+    count: 1000
+    size: 9000
+    rapid: yes
+  register: response
+- name: Print the packet_loss percentage from the response.
+  debug:
+    var: response
+- name: If packet loss is not zero, than this check fails
+  fail:
+    msg: "{{ response.changed }}"
+  when: response.packet_loss != "0"
diff --git a/roles/iptrunks_checks/tasks/main.yml b/roles/iptrunks_checks/tasks/main.yml
new file mode 100644
index 00000000..a3d4c654
--- /dev/null
+++ b/roles/iptrunks_checks/tasks/main.yml
@@ -0,0 +1,21 @@
+---
+# tasks file for iptrunks
+- name: Print the usage
+  debug:
+    msg:
+    - "Allowed checks: 'ping', 'isis'"
+  when: (check is not defined) or (check is in checks)
+
+- meta: end_play
+  when: (check is not defined) or (check is in checks)
+
+- name: Gather_facts
+  junipernetworks.junos.junos_facts:
+    gather_subset: all
+
+
+- include_tasks: check_isis.yaml
+  when: check == "isis"
+
+- include_tasks: check_ping.yaml
+  when: check == "ping"
diff --git a/roles/iptrunks_checks/tests/inventory b/roles/iptrunks_checks/tests/inventory
new file mode 100644
index 00000000..878877b0
--- /dev/null
+++ b/roles/iptrunks_checks/tests/inventory
@@ -0,0 +1,2 @@
+localhost
+
diff --git a/roles/iptrunks_checks/tests/test.yml b/roles/iptrunks_checks/tests/test.yml
new file mode 100644
index 00000000..3c586d57
--- /dev/null
+++ b/roles/iptrunks_checks/tests/test.yml
@@ -0,0 +1,5 @@
+---
+- hosts: localhost
+  remote_user: root
+  roles:
+    - iptrunks_checks
diff --git a/roles/iptrunks_checks/vars/main.yml b/roles/iptrunks_checks/vars/main.yml
new file mode 100644
index 00000000..4a852c70
--- /dev/null
+++ b/roles/iptrunks_checks/vars/main.yml
@@ -0,0 +1,31 @@
+---
+# vars file for iptrunks_checks
+
+trunks:
+  - id: "{{wfo_trunk.iptrunk.geant_s_sid}}"
+    name: AMS2-LON-IPTRUNK-100G
+    config:
+      common:
+        description: "{{wfo_trunk.iptrunk.iptrunk_description}}"
+        speed: "{{wfo_trunk.iptrunk.iptrunk_speed}}"
+        is_leased_line: false
+        isis_metric: '9000'
+        minimum_links: 1
+      nodeA:
+        name: "{{wfo_trunk.iptrunk.iptrunk_sideA_node.device_fqdn}}"
+        loopback: "{{wfo_trunk.iptrunk.iptrunk_sideA_node.device_lo_ipv4_address}}"
+        ae_name: "{{wfo_trunk.iptrunk.iptrunk_sideA_ae_iface}}"
+        ipv4_address: "{{side_a_ipv4_address}}"
+        ipv6_address: "{{side_a_ipv6_address}}"
+        members: "{{wfo_trunk.iptrunk.iptrunk_sideA_ae_members}}"
+        members_descriptions: "{{wfo_trunk.iptrunk.iptrunk_sideA_ae_members_description}}"
+        port_sid: "{{wfo_trunk.iptrunk.iptrunk_sideA_ae_geant_a_sid}}"
+      nodeB:
+        name: "{{wfo_trunk.iptrunk.iptrunk_sideB_node.device_fqdn}}"
+        loopback: "{{wfo_trunk.iptrunk.iptrunk_sideB_node.device_lo_ipv4_address}}"
+        ae_name: "{{wfo_trunk.iptrunk.iptrunk_sideB_ae_iface}}"
+        ipv4_address: "{{side_b_ipv4_address}}"
+        ipv6_address: "{{side_b_ipv6_address}}"
+        members: "{{wfo_trunk.iptrunk.iptrunk_sideB_ae_members}}"
+        members_descriptions: "{{wfo_trunk.iptrunk.iptrunk_sideB_ae_members_description}}"
+        port_sid: "{{wfo_trunk.iptrunk.iptrunk_sideB_ae_geant_a_sid}}"
-- 
GitLab