common.yml 4.4 KB

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