main.yml 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. # playbook.yml:
  2. ---
  3. - name: "common config"
  4. hosts: all
  5. remote_user: "{{ ssh_user }}"
  6. become: yes
  7. vars_files:
  8. - variables.yml
  9. vars:
  10. ansible_ssh_private_key_file: "{{ ssh_key }}"
  11. serial:
  12. - 1
  13. tasks:
  14. - name: Populate service facts
  15. service_facts:
  16. #Disable apparmor if present:
  17. - name: Stop and disable apparmor if present
  18. ansible.builtin.service:
  19. name: apparmor
  20. enabled: no
  21. state: stopped
  22. when: "'apparmor' in services"
  23. #set hostname:
  24. - name: change hostname to myserver
  25. hostname:
  26. name: "{{ machine_hostname }}"
  27. - name: add myself to /etc/hosts
  28. lineinfile:
  29. dest: /etc/hosts
  30. regexp: '^127\.0\.0\.1[ \t]+localhost'
  31. line: '127.0.0.1 localhost {{ machine_hostname }}'
  32. state: present
  33. - name: Set timezone to {{ timezone }}
  34. timezone:
  35. name: "{{ timezone }}"
  36. #update repos and install packages
  37. - name: Update repositories cache
  38. apt:
  39. update_cache: yes
  40. - name: Install a list of packages
  41. apt:
  42. pkg:
  43. - screen
  44. - htop
  45. - telnet
  46. - bind9
  47. - python
  48. - tinc
  49. - git
  50. - gpg
  51. #on debian 10 install haproxy 2.2 from external repos
  52. - name: on DEB10 add repo key for haproxy
  53. ansible.builtin.apt_key:
  54. url: https://haproxy.debian.net/bernat.debian.org.gpg
  55. state: present
  56. when: ansible_distribution == 'Debian' and ansible_distribution_version == '10'
  57. - name: on DEB10 add repo for haproxy
  58. ansible.builtin.apt_repository:
  59. repo: deb http://haproxy.debian.net buster-backports-2.2 main
  60. state: present
  61. filename: deb10_haproxy.list
  62. when: ansible_distribution == 'Debian' and ansible_distribution_version == '10'
  63. - name: DEB11 install haproxy
  64. apt:
  65. pkg:
  66. - haproxy
  67. when: ansible_distribution == 'Debian' and ansible_distribution_version == '11'
  68. # - name: "Check if listed package is installed or not on Debian Linux family"
  69. # command: dpkg-query -l haproxy
  70. # register: package_check
  71. #
  72. # - name: Delete content & directory
  73. # file:
  74. # state: absent
  75. # path: /etc/haproxy/
  76. # when: package_check is failed and ansible_distribution == 'Debian' and ansible_distribution_version == '10'
  77. # - name: DEB10 install haproxy
  78. ## shell: apt install haproxy=2.2.\*
  79. # shell: apt install haproxy=2.*
  80. # when: ansible_distribution == 'Debian' and ansible_distribution_version == '10'
  81. - name: DEB10 install haproxy
  82. apt:
  83. name: haproxy
  84. state: latest
  85. default_release: buster-backports
  86. when: ansible_distribution == 'Debian' and ansible_distribution_version == '10'
  87. - name: Create a directory /scripts if not present
  88. ansible.builtin.file:
  89. path: /scripts/
  90. state: directory
  91. mode: '0755'
  92. #Generate SSH key
  93. - name: check if the ssh has already been generated
  94. stat:
  95. path: "{{ hap_git_key }}"
  96. register: ssh_hap_key_exists
  97. - name: Generate an OpenSSH keypair
  98. openssh_keypair:
  99. path: "{{ hap_git_key }}"
  100. # type: ed25519
  101. type: rsa
  102. size: 4096
  103. state: present
  104. when: ssh_hap_key_exists.stat.exists == False
  105. - name: store the pubkey
  106. shell: cat "{{ hap_git_key }}.pub"
  107. register: cat_git_hap_key
  108. - name: Add ssh key to your host
  109. pause:
  110. prompt: "Please add the ssh pubkey to your git repo: {{ cat_git_hap_key.stdout }} ------ and then press ENTER"
  111. when: ssh_hap_key_exists.stat.exists == False
  112. #Sync git repo:
  113. - name: Git checkout
  114. ansible.builtin.git:
  115. repo: "{{ hap_git_repo }}"
  116. dest: "{{ hap_git_dest }}"
  117. key_file: "{{ hap_git_key }}"
  118. accept_hostkey: yes
  119. - name: Template a file to /etc/file.conf
  120. ansible.builtin.template:
  121. src: repo/git_hap-config_autoupdate.j2
  122. dest: "{{ hap_git_script }}"
  123. - name: execute first sync
  124. shell: "bash {{ hap_git_script }}"
  125. - name: Ensure a job that runs at 2 and 5 exists. Creates an entry like "0 5,2 * * ls -alh > /dev/null"
  126. ansible.builtin.cron:
  127. name: "sync haproxy config"
  128. minute: "*/2"
  129. job: "bash {{ hap_git_script }}"
  130. #BIND: register pubip:
  131. - name: retrieve your public ip
  132. shell: curl ifconfig.co/ip
  133. register: machine_pub_ip
  134. #BIND: add zonefile from template:
  135. - name: bind add file db."{{ zone }}.{{ domain }}"
  136. ansible.builtin.template:
  137. src: repo/bind_zone.j2
  138. dest: "/etc/bind/db.{{ zone }}.{{ domain }}"
  139. #BIND: configure bind to read the new zonefile:
  140. - name: Insert/Update "Match User" configuration block in /etc/ssh/sshd_config
  141. blockinfile:
  142. path: /etc/bind/named.conf
  143. block: |
  144. zone "{{ zone }}.{{ domain }}" {
  145. type master;
  146. file "/etc/bind/db.{{ zone }}.{{ domain }}";
  147. };
  148. #BIND restart:
  149. - name: Restart bind
  150. ansible.builtin.service:
  151. name: bind9
  152. state: restarted
  153. ##################################
  154. ### Demo
  155. - name: create directory for http server
  156. ansible.builtin.file:
  157. path: /tmp/httpserver/
  158. state: directory
  159. mode: '0755'
  160. - name: Template a file to /etc/file.conf
  161. ansible.builtin.template:
  162. src: repo/index.j2
  163. dest: "/tmp/httpserver/index.html"
  164. - name: launch test http server
  165. shell: "(cd /tmp/httpserver/; python3 -m http.server 8000 >/dev/null 2>&1 &)"
  166. async: 10
  167. poll: 0
  168. # shell: "cd /tmp/httpserver && python3 -m http.server 8000 &> /dev/null &"
  169. ####################################
  170. #END:
  171. #
  172. # - name: End message
  173. # ansible.builtin.debug:
  174. # msg:
  175. # - "-------------------------------------"
  176. # - "The configuration should be complete, you can run final_output.sh on your"
  177. # - "local machine to have the configuration to add to your DNS configuration."
  178. # - "Have fun!"