maplegrid.yml 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. # playbook.yml:
  2. ---
  3. - name: "maplegrid"
  4. hosts: localhost
  5. connection: local
  6. vars_files:
  7. - maplegrid_variables.yml
  8. ###
  9. tasks:
  10. #Add elastic repo for v7.x
  11. - name: Add elastic repo key
  12. shell: curl -fsSL https://artifacts.elastic.co/GPG-KEY-elasticsearch | apt-key add -
  13. - name: install packages
  14. apt:
  15. pkg:
  16. - apt-transport-https
  17. - python-pip
  18. - python3-pip
  19. - name: add repo for elastic v7.x
  20. shell: echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" | tee /etc/apt/sources.list.d/elastic-7.x.list
  21. - name: Install filebeat
  22. apt:
  23. name: filebeat
  24. update_cache: yes
  25. - name: Setup alternate SSH port
  26. lineinfile:
  27. dest: "/etc/ssh/sshd_config"
  28. regexp: "^Port"
  29. line: "Port 22222"
  30. - name: Setup alternate SSH port in fail2ban jail
  31. lineinfile:
  32. dest: "/etc/fail2ban/jail.conf"
  33. regexp: '^port = ssh'
  34. line: "port = 22222"
  35. - name: Restart fail2ban
  36. service:
  37. name: fail2ban
  38. state: restarted
  39. enabled: yes
  40. #PSHITT:
  41. - name: Install pip extensions
  42. shell: pip install python-daemon argparse paramiko
  43. - name: Git checkout
  44. git:
  45. repo: 'https://github.com/regit/pshitt'
  46. dest: /srv/pshitt
  47. - name: Add configuration block in /etc/systemd/system/pshitt.service
  48. blockinfile:
  49. create: yes
  50. dest: /etc/systemd/system/pshitt.service
  51. block: |
  52. [Unit]
  53. Description=pshitt service
  54. Wants=network-online.target
  55. After=network-online.target
  56. [Service]
  57. ExecStart=/srv/pshitt/pshitt.py -p 22 -k /etc/ssh/ssh_host_rsa_key -o /var/log/pshitt/data.json -l /var/log/pshitt/log.log
  58. [Install]
  59. WantedBy=multi-user.target
  60. - name: Creates directory
  61. file:
  62. path: /var/log/pshitt
  63. state: directory
  64. #configure iptables:
  65. - name: Creates directory
  66. file:
  67. path: /scripts
  68. state: directory
  69. - name: create rules.v4
  70. blockinfile:
  71. create: yes
  72. state: present
  73. dest: "/scripts/rules.v4"
  74. marker: "# {mark} ANSIBLE MANAGED BLOCK #"
  75. block: |
  76. # Generated by iptables-save v1.4.21 on Tue Nov 19 22:41:29 2019
  77. *filter
  78. :INPUT DROP [0:0]
  79. :FORWARD DROP [0:0]
  80. :OUTPUT ACCEPT [372:91728]
  81. :fail2ban-ssh - [0:0]
  82. -A INPUT -i lo -j ACCEPT
  83. -A INPUT -p tcp -m multiport --dports 22 -j fail2ban-ssh
  84. -A INPUT -i {{ ansible_default_ipv4.interface }} -p tcp -m tcp --dport 22 -j ACCEPT
  85. -A INPUT -i {{ ansible_default_ipv4.interface }} -p tcp -m tcp --dport 22222 -j ACCEPT
  86. -A INPUT -i {{ ansible_default_ipv4.interface }} -p icmp -m icmp --icmp-type 8 -j ACCEPT
  87. -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
  88. -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  89. -A INPUT -j DROP
  90. -A OUTPUT -o lo -j ACCEPT
  91. -A fail2ban-ssh -j RETURN
  92. COMMIT
  93. # Completed on Tue Nov 19 22:41:29 2019
  94. - name: reload iptables v4
  95. action: shell /sbin/iptables-restore /scripts/rules.v4
  96. - name: save iptables v4 rules
  97. shell: iptables-save > /etc/iptables/rules.v4
  98. #restart services on new ports:
  99. - name: Restart pshitt
  100. service:
  101. name: pshitt
  102. state: restarted
  103. enabled: yes
  104. - name: Restart sshd
  105. service:
  106. name: ssh
  107. state: restarted
  108. enabled: yes
  109. #Filebeat:
  110. - name: delete file
  111. ignore_errors: yes
  112. file:
  113. state: absent
  114. path: /etc/filebeat/filebeat.yml
  115. - name: Ansible create file if it doesn't exist example
  116. ignore_errors: yes
  117. file:
  118. path: "/etc/filebeat/filebeat.yml"
  119. state: touch
  120. - name: create rules.v4
  121. blockinfile:
  122. create: yes
  123. state: present
  124. dest: "/etc/filebeat/filebeat.yml"
  125. marker: "# {mark} ANSIBLE MANAGED BLOCK #"
  126. block: |
  127. filebeat.inputs:
  128. - type: log
  129. enabled: true
  130. paths:
  131. - /var/log/pshitt/data.json
  132. json.keys_under_root: true
  133. processors:
  134. - drop_fields:
  135. fields: ["beat", "source", "prospector", "offset", "host", "log", "input", "event", "fileset" ]
  136. - add_fields:
  137. target: ''
  138. fields:
  139. pshitt_host: {{ filebeat_hostname }}
  140. output.logstash:
  141. hosts: ["{{ logstash_ip }}:{{ logstash_port }}"]
  142. #Filebeat service logging:
  143. logging.level: info
  144. logging.to_files: true
  145. logging.files:
  146. path: /var/log/filebeat
  147. name: filebeat
  148. keepfiles: 7
  149. permissions: 0640
  150. - name: Restart filebeat
  151. service:
  152. name: filebeat
  153. state: restarted
  154. enabled: yes