common.yml 3.5 KB

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