common.yml 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # playbook.yml:
  2. ---
  3. - name: "common config"
  4. hosts: thismachine
  5. connection: local
  6. vars_files:
  7. - variables.yml
  8. tasks:
  9. - fail: msg="The user in the list 'users' in variables.yml, has to be set to somethings else than CHANGEME"
  10. when: '"CHANGEME" in users'
  11. - fail: msg="The variable 'hostname' in variables.yml, has to be set to somethings else than CHANGEME"
  12. when: '"CHANGEME" in hostname'
  13. - fail: msg="The variable 'tinc_vpn' in variables.yml, has to be set to somethings else than CHANGEME"
  14. when: '"CHANGEME" in tinc_vpn'
  15. ###
  16. - name: change hostname to myserver
  17. hostname:
  18. name: "{{ hostname }}"
  19. - name: add myself to /etc/hosts
  20. lineinfile:
  21. dest: /etc/hosts
  22. regexp: '^127\.0\.0\.1[ \t]+localhost'
  23. line: '127.0.0.1 localhost {{ hostname }}'
  24. state: present
  25. - name: Set timezone to {{ timezone }}
  26. timezone:
  27. name: {{ timezone }}
  28. - name: Update repositories cache
  29. apt:
  30. update_cache: yes
  31. - name: Install a list of packages
  32. apt:
  33. pkg:
  34. - htop
  35. - iotop
  36. - glances
  37. - screen
  38. - sysstat
  39. - git
  40. - nmap
  41. - ntp
  42. - tinc
  43. - fail2ban
  44. - iptables-persistent
  45. - debug:
  46. msg: The main interface is {{ ansible_default_ipv4.interface }}
  47. - name: Create a directory if it does not exist
  48. file:
  49. path: /scripts
  50. state: directory
  51. mode: '0755'
  52. - name: "Create user accounts and add users to groups"
  53. user:
  54. name: "{{ item }}"
  55. shell: "/bin/bash"
  56. with_items: "{{ users }}"
  57. - name: "Add authorized keys"
  58. authorized_key:
  59. user: "{{ item }}"
  60. key: "{{ lookup('file', 'keys/'+ item + '.key.pub') }}"
  61. with_items: "{{ users }}"
  62. - name: create rules.v4
  63. blockinfile:
  64. create: yes
  65. state: present
  66. dest: "/scripts/rules.v4"
  67. marker: "# {mark} ANSIBLE MANAGED BLOCK #"
  68. block: |
  69. # Generated by iptables-save v1.4.21 on Tue Nov 19 22:41:29 2019
  70. *filter
  71. :INPUT DROP [0:0]
  72. :FORWARD DROP [0:0]
  73. :OUTPUT ACCEPT [372:91728]
  74. :fail2ban-ssh - [0:0]
  75. -A INPUT -i lo -j ACCEPT
  76. -A INPUT -p tcp -m multiport --dports 22 -j fail2ban-ssh
  77. -A INPUT -i {{ ansible_default_ipv4.interface }} -p tcp -m tcp --dport 22 -j ACCEPT
  78. -A INPUT -i {{ ansible_default_ipv4.interface }} -p icmp -m icmp --icmp-type 8 -j ACCEPT
  79. -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
  80. -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  81. -A INPUT -j DROP
  82. -A OUTPUT -o lo -j ACCEPT
  83. -A fail2ban-ssh -j RETURN
  84. COMMIT
  85. # Completed on Tue Nov 19 22:41:29 2019
  86. - name: create rules.v6
  87. blockinfile:
  88. create: yes
  89. state: present
  90. dest: "/scripts/rules.v6"
  91. marker: "# {mark} ANSIBLE MANAGED BLOCK #"
  92. block: |
  93. # Generated by ip6tables-save v1.4.21 on Tue Nov 19 22:58:08 2019
  94. *filter
  95. :INPUT DROP [0:0]
  96. :FORWARD DROP [0:0]
  97. :OUTPUT DROP [0:0]
  98. COMMIT
  99. # Completed on Tue Nov 19 22:58:08 2019
  100. - name: reload iptables v4
  101. action: shell /sbin/iptables-restore -! < /scripts/rules.v4
  102. - name: reload iptables v4
  103. action: shell /sbin/ip6tables-restore -! < /scripts/rules.v6
  104. - name: save iptables v4 rules
  105. shell: iptables-save > /etc/iptables/rules.v4
  106. - name: save iptables v6 rules
  107. shell: ip6tables-save > /etc/iptables/rules.v6
  108. - name: "Copy file with owner and permissions"
  109. copy:
  110. backup: yes
  111. src: "{{ playbook_dir }}/repo/bashrc"
  112. dest: /root/.bashrc
  113. owner: root
  114. group: root
  115. mode: '0644'
  116. - name: "ssh_hardening"
  117. hosts: thismachine
  118. connection: local
  119. roles:
  120. - ssh_hardening