common.yml 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # playbook.yml:
  2. ---
  3. - name: "common config"
  4. hosts: thismachine
  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. - debug:
  49. msg: The main interface is {{ ansible_default_ipv4.interface }}
  50. - name: Create a directory if it does not exist
  51. file:
  52. path: /scripts
  53. state: directory
  54. mode: '0755'
  55. - name: "Create user accounts and add users to groups"
  56. user:
  57. name: "{{ item }}"
  58. shell: "/bin/bash"
  59. with_items: "{{ users }}"
  60. - name: "Add authorized keys"
  61. authorized_key:
  62. user: "{{ item }}"
  63. key: "{{ lookup('file', 'keys/'+ item + '.key.pub') }}"
  64. with_items: "{{ users }}"
  65. - name: create rules.v4
  66. blockinfile:
  67. create: yes
  68. state: present
  69. dest: "/scripts/rules.v4"
  70. marker: "# {mark} ANSIBLE MANAGED BLOCK #"
  71. block: |
  72. # Generated by iptables-save v1.4.21 on Tue Nov 19 22:41:29 2019
  73. *filter
  74. :INPUT DROP [0:0]
  75. :FORWARD DROP [0:0]
  76. :OUTPUT ACCEPT [372:91728]
  77. :fail2ban-ssh - [0:0]
  78. -A INPUT -i lo -j ACCEPT
  79. -A INPUT -p tcp -m multiport --dports 22 -j fail2ban-ssh
  80. -A INPUT -i {{ ansible_default_ipv4.interface }} -p tcp -m tcp --dport 22 -j ACCEPT
  81. -A INPUT -i {{ ansible_default_ipv4.interface }} -p icmp -m icmp --icmp-type 8 -j ACCEPT
  82. -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
  83. -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  84. -A INPUT -j DROP
  85. -A OUTPUT -o lo -j ACCEPT
  86. -A fail2ban-ssh -j RETURN
  87. COMMIT
  88. # Completed on Tue Nov 19 22:41:29 2019
  89. - name: create rules.v6
  90. blockinfile:
  91. create: yes
  92. state: present
  93. dest: "/scripts/rules.v6"
  94. marker: "# {mark} ANSIBLE MANAGED BLOCK #"
  95. block: |
  96. # Generated by ip6tables-save v1.4.21 on Tue Nov 19 22:58:08 2019
  97. *filter
  98. :INPUT DROP [0:0]
  99. :FORWARD DROP [0:0]
  100. :OUTPUT DROP [0:0]
  101. COMMIT
  102. # Completed on Tue Nov 19 22:58:08 2019
  103. - name: reload iptables v4
  104. action: shell /sbin/iptables-restore /scripts/rules.v4
  105. - name: reload iptables v4
  106. action: shell /sbin/ip6tables-restore /scripts/rules.v6
  107. - name: save iptables v4 rules
  108. shell: iptables-save > /etc/iptables/rules.v4
  109. - name: save iptables v6 rules
  110. shell: ip6tables-save > /etc/iptables/rules.v6
  111. - name: "Copy file with owner and permissions"
  112. copy:
  113. backup: yes
  114. src: "{{ playbook_dir }}/repo/bashrc"
  115. dest: /root/.bashrc
  116. owner: root
  117. group: root
  118. mode: '0644'
  119. - name: "ssh_hardening"
  120. hosts: thismachine
  121. connection: local
  122. roles:
  123. - ssh_hardening