common.yml 3.8 KB

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