Fix spacing issues in YAML files in tutorials/
[demo.git] / tutorials / vFWDT / playbooks / vfw-sink / latest / ansible / distributetrafficcheck / site.yml
1 ---
2 - hosts: vfw-sink
3   gather_facts: no
4   remote_user: ubuntu
5   vars:
6     time_measure: 30
7   tasks:
8
9   - name: Install grepcidr
10     apt:
11       name: grepcidr
12     become: yes
13
14   - include_vars: "{{ ConfigFileName }}"
15   - debug: var="trafficpresence"
16     failed_when: "'trafficpresence' is not defined"
17
18   - name: Get fw interface
19     shell: |
20       IP_PPNET=$(cat /etc/network/interfaces |grep address | awk '{print $2}' | grepcidr -f /opt/config/protected_private_net_cidr.txt)
21       cat /etc/network/interfaces | grep $IP_PPNET -B1 | grep iface | awk '{print $2}'
22     register: fw_ppnet_iface
23
24   - name: Traffic check if trafficpresence is TRUE
25     when: trafficpresence == true
26     block:
27     - name: Traffic check if trafficpresence is TRUE
28       raw: |
29         #!/bin/bash
30         COL=$(netstat -i|grep lo |awk '{print NF}')
31         if [ $COL -eq 11 ] ; then NCOL=7
32           elif [  $COL -eq 12 ] ; then NCOL=8
33           else echo "bad NETSTAT version"
34         fi
35         TXOK_INITIAL=$(netstat -i | fgrep "{{ fw_ppnet_iface.stdout }}" | awk -v col=$NCOL '{print $col}')
36         sleep 0.1
37         for i in {1..{{ time_measure }}}
38         do
39           TXOK_CURRENT=$(netstat -i | fgrep "{{ fw_ppnet_iface.stdout }}" | awk -v col=$NCOL '{print $col}')
40           if [ $TXOK_CURRENT -gt $TXOK_INITIAL ] ; then
41             echo 'traffic present'
42             break
43           fi
44           sleep 1
45           echo $TXOK_CURRENT
46         done
47         if [ $TXOK_CURRENT -eq $TXOK_INITIAL ] ; then
48           echo 'traffic absent'
49           exit 1
50         fi
51       register: traffic_check
52       ignore_errors: yes
53     - debug:
54         msg: 'traffic absent {{ traffic_check.stdout_lines }} '
55       when: traffic_check.rc == 1
56       failed_when: traffic_check.rc == 1
57     - debug:
58         msg: 'traffic present {{ traffic_check.stdout_lines }} '
59       when: traffic_check.rc == 0
60
61   - name: Traffic check if trafficpresence is FALSE
62     when: trafficpresence == false
63     block:
64     - name: Traffic check trafficpresence is FALSE
65       raw: |
66         #!/bin/bash
67         COL=$(netstat -i|grep lo |awk '{print NF}')
68         if [ $COL -eq 11 ] ; then NCOL=7
69         elif [  $COL -eq 12 ] ; then NCOL=8
70           else echo "bad NETSTAT version"
71         fi
72         for i in {1..{{ time_measure }}}
73         do
74           TXOK_INITIAL=$(netstat -i | fgrep "{{ fw_ppnet_iface.stdout }}" | awk -v col=$NCOL '{print $col}')
75           sleep 1
76           TXOK_CURRENT=$(netstat -i | fgrep "{{ fw_ppnet_iface.stdout }}" | awk -v col=$NCOL '{print $col}')
77           if [ $TXOK_CURRENT -eq $TXOK_INITIAL ] ; then
78             echo 'traffic absent'
79             break
80           fi
81         done
82         if [ $TXOK_CURRENT -gt $TXOK_INITIAL ] ; then
83           echo 'traffic present'
84           exit 1
85         fi
86       register: traffic_check
87       ignore_errors: yes
88     - debug:
89         msg: 'traffic absent {{ traffic_check.stdout_lines }} '
90       when: traffic_check.rc == 0
91     - debug:
92         msg: 'traffic present {{ traffic_check.stdout_lines }} '
93       when: traffic_check.rc == 1
94       failed_when: traffic_check.rc == 1