common.yml 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. - debug:
  58. msg: The main interface is {{ ansible_default_ipv4.interface }}
  59. - name: Create a directory if it does not exist
  60. file:
  61. path: /scripts
  62. state: directory
  63. mode: '0755'
  64. - name: "Create user accounts and add users to groups"
  65. user:
  66. name: "{{ item }}"
  67. shell: "/bin/bash"
  68. with_items: "{{ users }}"
  69. - name: "Add authorized keys"
  70. authorized_key:
  71. user: "{{ item }}"
  72. key: "{{ lookup('file', 'keys/'+ item + '.key.pub') }}"
  73. with_items: "{{ users }}"
  74. - name: Fix Debian10's shitty executables paths
  75. lineinfile:
  76. dest: /etc/environment
  77. line: 'PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin"'
  78. state: present
  79. when: ansible_distribution == 'Debian' and ansible_distribution_major_version >= '10'
  80. - name: create rules.v4
  81. blockinfile:
  82. create: yes
  83. state: present
  84. dest: "/scripts/rules.v4"
  85. marker: "# {mark} ANSIBLE MANAGED BLOCK #"
  86. block: |
  87. # Generated by iptables-save v1.4.21 on Tue Nov 19 22:41:29 2019
  88. *filter
  89. :INPUT DROP [0:0]
  90. :FORWARD DROP [0:0]
  91. :OUTPUT ACCEPT [372:91728]
  92. :fail2ban-ssh - [0:0]
  93. -A INPUT -i lo -j ACCEPT
  94. -A INPUT -p tcp -m multiport --dports 22 -j fail2ban-ssh
  95. -A INPUT -i {{ ansible_default_ipv4.interface }} -p tcp -m tcp --dport 22 -j ACCEPT
  96. -A INPUT -i {{ ansible_default_ipv4.interface }} -p icmp -m icmp --icmp-type 8 -j ACCEPT
  97. -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
  98. -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  99. -A INPUT -j DROP
  100. -A OUTPUT -o lo -j ACCEPT
  101. -A fail2ban-ssh -j RETURN
  102. COMMIT
  103. # Completed on Tue Nov 19 22:41:29 2019
  104. - name: create rules.v6
  105. blockinfile:
  106. create: yes
  107. state: present
  108. dest: "/scripts/rules.v6"
  109. marker: "# {mark} ANSIBLE MANAGED BLOCK #"
  110. block: |
  111. # Generated by ip6tables-save v1.4.21 on Tue Nov 19 22:58:08 2019
  112. *filter
  113. :INPUT DROP [0:0]
  114. :FORWARD DROP [0:0]
  115. :OUTPUT DROP [0:0]
  116. COMMIT
  117. # Completed on Tue Nov 19 22:58:08 2019
  118. - name: reload iptables v4
  119. action: shell /sbin/iptables-restore /scripts/rules.v4
  120. - name: reload iptables v4
  121. action: shell /sbin/ip6tables-restore /scripts/rules.v6
  122. - name: save iptables v4 rules
  123. shell: iptables-save > /etc/iptables/rules.v4
  124. - name: save iptables v6 rules
  125. shell: ip6tables-save > /etc/iptables/rules.v6
  126. - name: "Copy file with owner and permissions"
  127. copy:
  128. backup: yes
  129. src: "{{ playbook_dir }}/repo/bashrc"
  130. dest: /root/.bashrc
  131. owner: root
  132. group: root
  133. mode: '0644'
  134. # Set vm.swappiness to 5 in /etc/sysctl.conf
  135. - name: "Set swappiness to zero in sysctl.conf"
  136. sysctl:
  137. name: vm.swappiness
  138. value: '0'
  139. state: present
  140. reload: yes
  141. sysctl_file: /etc/sysctl.conf
  142. - name: Disable IPv6 with sysctl
  143. sysctl: name={{ item }} value=1 state=present reload=yes
  144. with_items:
  145. - net.ipv6.conf.all.disable_ipv6
  146. - net.ipv6.conf.default.disable_ipv6
  147. - net.ipv6.conf.lo.disable_ipv6
  148. - name: "ssh_hardening"
  149. hosts: localhost
  150. connection: local
  151. roles:
  152. - ssh_hardening