SPR
SPR

Reputation: 15

Ansible to checking port status on network device

I have to check 2 ports status. There are many possibilities. They can be 2 up, port 38 can be up but port 37 can be down and coversly, they can be 2 down. I have to give messege about in which configuration are these port ex. (port 37,38 are up) Msg: Two ports are up etc. I made something like this:

    - name: CHECKING PORT 37,38
      raw: diag switch-controller switch-info lldp neighbors-summary
      register: PORT

    - set_fact:
        id: "{{ PORT.stdout | regex_search(regexp) }}"
      vars:
        regexp: '(port37.*Up)'
      register: PORT37 


    - set_fact:
        id: "{{ PORT.stdout | regex_search(regexp) }}"
      vars:
        regexp: '(port38.*Down)'
      register: PORT38 

Actually i'm stuck on this how to make messege for all this possibilities someone have any idea? It will be easier if i will know how value are taking set_fact when it not find specific string from regexp

PORT VALUE: VALUE:

Upvotes: 0

Views: 840

Answers (3)

SPR
SPR

Reputation: 15

I figured it out simpler

  - name: CHECKING PORT 37,38
      raw: diag switch-controller switch-info lldp neighbors-summary
      register: PORT
      no_log: true

    - set_fact:
        id: "{{ PORT.stdout | regex_search(regexp) }}"
      vars:
        regexp: '(port37.*Up)'
      register: port37
      no_log: true
    - set_fact:
        id: "{{ PORT.stdout | regex_search(regexp) }}"
      vars:
        regexp: '(port38.*Up)'
      register: port38
      no_log: true
      
    - debug:
        msg: "Port 37 is down  and port 38 is down"
      when: (port37.ansible_facts.id | length == 0) and (port38.ansible_facts.id | length == 0)

    - debug:
        msg: "Port 37 is up and port 38 is down"
      when: (port37.ansible_facts.id | length > 0) and (port38.ansible_facts.id | length == 0)

    - debug:
        msg: "Port 38 is up and port 37 is down"
      when: (port38.ansible_facts.id | length > 0) and (port37.ansible_facts.id | length == 0)

    - debug:
        msg: "Port 38 is up and port 37 is up"
      when: (port38.ansible_facts.id | length > 0) and (port37.ansible_facts.id | length > 0)


Thank @Frenchy for help

Upvotes: 0

Frenchy
Frenchy

Reputation: 17007

this playbook display a message following the status of the port:

- hosts: localhost
  gather_facts: no
  vars:
    PORT: 
      stdout: |
        port01    Up    others
        port12    Down     others
        port37    Up   others
        port38    Down   others
  tasks:   
    - name: loop over
      debug: 
        msg: >-
            {%- if  out.1 == "Up" -%}
            port {{ out.0 }} has a status Up
            {%- elif out.1 == "Down" -%}
            port {{ out.0 }} has a status Down
            {%- else -%}
            port {{ out.0 }} has a status {{ out.1 }}
            {%- endif -%}      
      vars:
        port_to_check: 37
        regexp: 'port({{port_to_check}})\s*([^\s]+)'
        out: "{{ PORT.stdout | regex_search(regexp) | split() }}"

result:

ok: [localhost] => {
    "msg": "port port37 has a status Up"
}

so you could adapt the ouput as you want...

Upvotes: 1

Related Questions