common.yml 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. # playbook.yml:
  2. ---
  3. - name: "common config"
  4. hosts: localhost
  5. connection: local
  6. vars_files:
  7. - variables.yml
  8. tasks:
  9. - name: "check the variable: users"
  10. fail: msg="The user in the list 'users' in variables.yml, has to be set to somethings else than CHANGEME"
  11. when: '"CHANGEME" in users'
  12. - name: "check the variable: hostname"
  13. fail: msg="The variable 'hostname' in variables.yml, has to be set to somethings else than CHANGEME"
  14. when: '"CHANGEME" in hostname'
  15. # - name: "check the variable: tinc_vpn"
  16. # fail: msg="The variable 'tinc_vpn' in variables.yml, has to be set to somethings else than CHANGEME"
  17. # when: '"CHANGEME" in tinc_vpn'
  18. ###
  19. - name: change hostname to myserver
  20. hostname:
  21. name: "{{ hostname }}"
  22. - name: add myself to /etc/hosts
  23. lineinfile:
  24. dest: /etc/hosts
  25. regexp: '^127\.0\.0\.1[ \t]+localhost'
  26. line: '127.0.0.1 localhost {{ hostname }}'
  27. state: present
  28. - name: Set timezone to {{ timezone }}
  29. timezone:
  30. name: "{{ timezone }}"
  31. - name: Update repositories cache
  32. apt:
  33. update_cache: yes
  34. - name: Install a list of packages
  35. apt:
  36. pkg:
  37. - htop
  38. - iotop
  39. # - glances
  40. - screen
  41. - sysstat
  42. - git
  43. - nmap
  44. - ntp
  45. - tinc
  46. - fail2ban
  47. - iptables-persistent
  48. - ssh
  49. - locales-all
  50. - curl
  51. - wget
  52. - net-tools
  53. - apt-transport-https
  54. - ca-certificates
  55. - gnupg
  56. - lsb-release
  57. - rsync
  58. - telnet
  59. - debug:
  60. msg: The main interface is {{ ansible_default_ipv4.interface }}
  61. - name: Create a directory if it does not exist
  62. file:
  63. path: /scripts
  64. state: directory
  65. mode: '0755'
  66. - name: "Create user accounts and add users to groups"
  67. user:
  68. name: "{{ item }}"
  69. shell: "/bin/bash"
  70. with_items: "{{ users }}"
  71. - name: "Add authorized keys"
  72. authorized_key:
  73. user: "{{ item }}"
  74. key: "{{ lookup('file', 'keys/'+ item + '.key.pub') }}"
  75. with_items: "{{ users }}"
  76. - name: Fix Debian10's shitty executables paths
  77. lineinfile:
  78. dest: /etc/environment
  79. line: 'PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin"'
  80. state: present
  81. when: ansible_distribution == 'Debian' and ansible_distribution_major_version >= '10'
  82. - name: create rules.v4
  83. blockinfile:
  84. create: yes
  85. state: present
  86. dest: "/scripts/rules.v4"
  87. marker: "# {mark} ANSIBLE MANAGED BLOCK #"
  88. block: |
  89. # Generated by iptables-save v1.4.21 on Tue Nov 19 22:41:29 2019
  90. *filter
  91. :INPUT DROP [0:0]
  92. :FORWARD DROP [0:0]
  93. :OUTPUT ACCEPT [372:91728]
  94. :fail2ban-ssh - [0:0]
  95. -A INPUT -i lo -j ACCEPT
  96. -A INPUT -p tcp -m multiport --dports 22 -j fail2ban-ssh
  97. -A INPUT -i {{ ansible_default_ipv4.interface }} -p tcp -m tcp --dport 22 -j ACCEPT
  98. -A INPUT -i {{ ansible_default_ipv4.interface }} -p icmp -m icmp --icmp-type 8 -j ACCEPT
  99. -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
  100. -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  101. -A INPUT -j DROP
  102. -A OUTPUT -o lo -j ACCEPT
  103. -A fail2ban-ssh -j RETURN
  104. COMMIT
  105. # Completed on Tue Nov 19 22:41:29 2019
  106. - name: create rules.v6
  107. blockinfile:
  108. create: yes
  109. state: present
  110. dest: "/scripts/rules.v6"
  111. marker: "# {mark} ANSIBLE MANAGED BLOCK #"
  112. block: |
  113. # Generated by ip6tables-save v1.4.21 on Tue Nov 19 22:58:08 2019
  114. *filter
  115. :INPUT DROP [0:0]
  116. :FORWARD DROP [0:0]
  117. :OUTPUT DROP [0:0]
  118. COMMIT
  119. # Completed on Tue Nov 19 22:58:08 2019
  120. - name: reload iptables v4
  121. action: shell /sbin/iptables-restore /scripts/rules.v4
  122. - name: reload iptables v4
  123. action: shell /sbin/ip6tables-restore /scripts/rules.v6
  124. - name: save iptables v4 rules
  125. shell: iptables-save > /etc/iptables/rules.v4
  126. - name: save iptables v6 rules
  127. shell: ip6tables-save > /etc/iptables/rules.v6
  128. - name: "Copy file with owner and permissions"
  129. copy:
  130. backup: yes
  131. src: "{{ playbook_dir }}/repo/bashrc"
  132. dest: /root/.bashrc
  133. owner: root
  134. group: root
  135. mode: '0644'
  136. - name: "Register if we are running baremetal (none), virtualized (kvm) or container (lxc)"
  137. command: systemd-detect-virt --container
  138. register: systemd_detect_virt
  139. # Set vm.swappiness to 1 in /etc/sysctl.conf
  140. - name: "Set swappiness to zero in sysctl.conf"
  141. sysctl:
  142. name: vm.swappiness
  143. value: '1'
  144. state: present
  145. reload: yes
  146. sysctl_file: /etc/sysctl.conf
  147. when: systemd_detect_virt.stdout == "none"
  148. - name: "Set nf_conntrack_max to 131072 in sysctl.conf, suitable for max 4gb of ram, conntrack_max = RAMSIZE (in bytes)/16384/2 = 4*1024*1024*1024/16384/2 = 4*32768 = 131072"
  149. sysctl:
  150. name: net.netfilter.nf_conntrack_max
  151. value: '131072'
  152. state: present
  153. reload: yes
  154. sysctl_file: /etc/sysctl.conf
  155. #source: https://support.huaweicloud.com/intl/en-us/trouble-ecs/ecs_trouble_0324.html
  156. when: systemd_detect_virt.stdout == "none"
  157. - name: Disable IPv6 with sysctl
  158. sysctl: name={{ item }} value=1 state=present reload=yes
  159. with_items:
  160. - net.ipv6.conf.all.disable_ipv6
  161. - net.ipv6.conf.default.disable_ipv6
  162. - net.ipv6.conf.lo.disable_ipv6
  163. when: systemd_detect_virt.stdout == "none"
  164. - ansible.builtin.include_role:
  165. name: ssh_hardening
  166. - name: "copy iptables disable script to/scripts"
  167. copy:
  168. backup: yes
  169. src: "{{ playbook_dir }}/repo/disable_iptables.sh"
  170. dest: /scripts/disable_iptables.sh
  171. owner: root
  172. group: root
  173. mode: '0744'