diff --git a/main.yaml b/main.yaml index ce1eb46..53f801b 100644 --- a/main.yaml +++ b/main.yaml @@ -1,25 +1,29 @@ - name: Ansible Network Acceessibility Check hosts: localhost ignore_errors: true - vars_files: + vars_files: - port_definition.yaml tasks: - name: print var - debug: + debug: var: port_definitions - name: Defining fact initialization - include_tasks: "tasks/init_phases.yaml port_definition={{item_port}}" - with_items: "{{port_definitions}}" + include: "tasks/init_phases.yaml" + vars: + port_definition: "{{item_port}}" + with_items: "{{ port_definitions }}" loop_control: loop_var: item_port run_once: true - name: Testing Ports - include_tasks: "tasks/start_port_check.yaml port_definition={{item_port}}" + include: "tasks/start_port_check.yaml" + vars: + port_definition: "{{item_port}}" with_items: "{{port_definitions}}" - loop_control: + loop_control: loop_var: item_port run_once: true @@ -34,4 +38,3 @@ # - name: ensure tmp folder doesnot exist for clean start # shell: | # rm -rf /tmp/ansible_network_health_check* - diff --git a/tasks/init_phases.yaml b/tasks/init_phases.yaml index 848ebf8..0cfde06 100644 --- a/tasks/init_phases.yaml +++ b/tasks/init_phases.yaml @@ -3,7 +3,9 @@ var: port_definition - name: looping to servers - include_tasks: "loop_phase_to_server.yaml check_to_host={{item_to_host}}" + include: "loop_phase_to_server.yaml" + vars: + check_to_host: "{{item_to_host}}" with_items: "{{groups[port_definition['to_group']]}}" loop_control: loop_var: item_to_host diff --git a/tasks/loop_phase_to_server.yaml b/tasks/loop_phase_to_server.yaml index 60605b0..1b03314 100644 --- a/tasks/loop_phase_to_server.yaml +++ b/tasks/loop_phase_to_server.yaml @@ -12,7 +12,7 @@ - name: loop from clients - include_tasks: tasks/loop_phase_from_clients.yaml + include: tasks/loop_phase_from_clients.yaml vars: check_from_host: "{{item_from_host}}" with_items: "{{groups[port_definition['from_group']]}}" diff --git a/tasks/server_check.yaml b/tasks/server_check.yaml index efa2d49..3748d9d 100644 --- a/tasks/server_check.yaml +++ b/tasks/server_check.yaml @@ -2,16 +2,25 @@ # hosts: ansible_network_health_check_to # gather_facts: yes # tasks: + - name: Register result of tests +# when: check_to_host == inventory_hostname + delegate_to: "{{check_to_host}}" + shell: "cat /tmp/ansible_network_health_check_to/{{test_protocol}}_{{test_to_ip}}_{{test_port}}_OUTPUT" + register: test_reuslts + + - name: Register result of tests + debug: + var: test_reuslts + - name: Check server status # when: check_to_host == inventory_hostname - delegated_to: "{{check_to_host}}" - with_lines: "cat /tmp/ansible_network_health_check_to/{{test_protocol}}_{{test_to_ip}}_{{test_port}}_OUTPUT" - include_tasks: tasks/register_test_result.yaml - vars: + include: tasks/register_test_result.yaml + vars: test_result_string: "{{item}}" + with_items: "{{test_reuslts.stdout_lines}}" - name: Ensure no existing process running - delegated_to: "{{check_to_host}}" + delegate_to: "{{check_to_host}}" # when: check_to_host == inventory_hostname shell: | for PIDFILE in $(ls /tmp/ansible_network_health_check_to/*PID); do @@ -20,9 +29,8 @@ exit 0 - name: ensure tmp folder doesnot exist for clean start - delegated_to: "{{check_to_host}}" + delegate_to: "{{check_to_host}}" # when: check_to_host == inventory_hostname file: path: /tmp/ansible_network_health_check_to/ state: absent - diff --git a/tasks/start_port_check.yaml b/tasks/start_port_check.yaml index b2df5eb..97005fa 100644 --- a/tasks/start_port_check.yaml +++ b/tasks/start_port_check.yaml @@ -3,7 +3,9 @@ var: port_definition - name: start to as server - include_tasks: "start_to_servers.yaml check_to_host={{item_to_host}}" + include: "start_to_servers.yaml" + vars: + check_to_host: "{{item_to_host}}" with_items: "{{groups[port_definition['to_group']]}}" loop_control: loop_var: item_to_host diff --git a/tasks/start_to_servers.yaml b/tasks/start_to_servers.yaml index 6d5dfe0..3c8aa1e 100644 --- a/tasks/start_to_servers.yaml +++ b/tasks/start_to_servers.yaml @@ -9,16 +9,16 @@ var: check_to_host - name: start tcp server - include_tasks: tcp_to_server.yaml + include: tcp_to_server.yaml when: port_definition['type']|lower == 'tcp' - name: start udp server - include_tasks: udp_to_server.yaml + include: udp_to_server.yaml when: port_definition['type']|lower == 'udp' - name: start from as client - TCP when: port_definition['type']|lower == 'tcp' - include_tasks: tasks/tcp_from_clients.yaml + include: tasks/tcp_from_clients.yaml vars: check_from_host: "{{item_from_host}}" ansible_network_health_check_port: "{{test_port}}" @@ -29,7 +29,7 @@ - name: start from as client - UDP when: port_definition['type']|lower == 'udp' - include_tasks: tasks/udp_from_clients.yaml + include: tasks/udp_from_clients.yaml vars: check_from_host: "{{item_from_host}}" ansible_network_health_check_port: "{{test_port}}" @@ -38,5 +38,5 @@ loop_control: loop_var: item_from_host - - name: check server result - tcp - include_tasks: tasks/server_check.yaml + - name: check server result - tcp + include: tasks/server_check.yaml diff --git a/tasks/tcp_to_server.yaml b/tasks/tcp_to_server.yaml index 7ecd4ba..2fd58c6 100644 --- a/tasks/tcp_to_server.yaml +++ b/tasks/tcp_to_server.yaml @@ -5,17 +5,16 @@ kill -9 $(cat /tmp/ansible_network_health_check_to/{{test_protocol}}_{{test_to_ip}}_{{test_port}}_PID) exit 0 - - name: ensure tmp folder doesnot exist for clean start # when: check_to_host == inventory_hostname delegate_to: "{{check_to_host}}" - file: + file: path: /tmp/ansible_network_health_check_to/ state: absent - - name: ensure folder exist + - name: ensure folder exist # when: check_to_host == inventory_hostname - file: + file: path: /tmp/ansible_network_health_check_to/ state: directory delegate_to: "{{check_to_host}}" @@ -38,7 +37,7 @@ delegate_to: "{{check_to_host}}" # when: check_to_host == inventory_hostname shell: | - cd /tmp/ansible_network_health_check_to/ + cd /tmp/ansible_network_health_check_to/ python /tmp/ansible_network_health_check_to/tcpServer.py {{test_to_ip}} {{test_port}} /tmp/ansible_network_health_check_to/{{test_protocol}}_{{test_to_ip}}_{{test_port}}_OUTPUT & PID=$! echo $PID > /tmp/ansible_network_health_check_to/{{test_protocol}}_{{test_to_ip}}_{{test_port}}_PID @@ -52,10 +51,19 @@ async: 45 poll: 0 + - name: register PID of tcpServer.py + delegate_to: "{{check_to_host}}" + shell: "cat /tmp/ansible_network_health_check_to/{{test_protocol}}_{{test_to_ip}}_{{test_port}}_PID" + register: PID_tcpServer + + - name: debug only + debug: + var: PID_tcpServer + - name: make sure file Exist delegate_to: "{{check_to_host}}" # when: check_to_host == inventory_hostname stat: - path: "/proc/{{lookup('file', '/tmp/ansible_network_health_check_to/'+test_protocol+'_'+test_to_ip+'_'+test_port+'_PID') }}/status" + path: "/proc/{{PID_tcpServer.stdout}}/status" register: stat_result failed_when: not stat_result.stat.exists diff --git a/tasks/udp_to_server.yaml b/tasks/udp_to_server.yaml index 3cf1404..ba0e033 100644 --- a/tasks/udp_to_server.yaml +++ b/tasks/udp_to_server.yaml @@ -10,14 +10,14 @@ - name: ensure tmp folder doesnot exist for clean start delegate_to: "{{check_to_host}}" # when: check_to_host == inventory_hostname - file: + file: path: /tmp/ansible_network_health_check_to/ state: absent - - name: ensure folder exist + - name: ensure folder exist delegate_to: "{{check_to_host}}" # when: check_to_host == inventory_hostname - file: + file: path: /tmp/ansible_network_health_check_to/ state: directory @@ -32,7 +32,7 @@ delegate_to: "{{check_to_host}}" # when: check_to_host == inventory_hostname shell: | - cd /tmp/ansible_network_health_check_to/ + cd /tmp/ansible_network_health_check_to/ python /tmp/ansible_network_health_check_to/udpServer.py {{test_to_ip}} {{test_port}} /tmp/ansible_network_health_check_to/{{test_protocol}}_{{test_to_ip}}_{{test_port}}_OUTPUT & PID=$! echo $PID > /tmp/ansible_network_health_check_to/{{test_protocol}}_{{test_to_ip}}_{{test_port}}_PID @@ -46,10 +46,19 @@ async: 45 poll: 0 + - name: register PID of udpServer.py + delegate_to: "{{check_to_host}}" + shell: "cat /tmp/ansible_network_health_check_to/{{test_protocol}}_{{test_to_ip}}_{{test_port}}_PID" + register: PID_udpServer + + - name: debug only + debug: + var: PID_udpServer + - name: make sure file Exist delegate_to: "{{check_to_host}}" # when: check_to_host == inventory_hostname stat: - path: "/proc/{{lookup('file', '/tmp/ansible_network_health_check_to/'+test_protocol+'_'+test_to_ip+'_'+test_port+'_PID') }}/status" + path: "/proc/{{PID_udpServer.stdout}}/status" register: stat_result failed_when: not stat_result.stat.exists