common.yml 4.0 KB

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