common.yml 3.3 KB

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