main.yml 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. ---
  2. - name: Install dhcp & dns packages
  3. dnf:
  4. name:
  5. - dhcp-server
  6. - unbound
  7. update_cache: no
  8. state: present
  9. - name: Make services autorestart themselves on failure
  10. template:
  11. src: "{{ item }}.service.j2"
  12. dest: /etc/systemd/system/{{ item }}.service
  13. owner: root
  14. group: root
  15. mode: '0644'
  16. loop:
  17. - dhcpd
  18. - unbound
  19. notify:
  20. - Restart {{ item }}
  21. - Reload systemd services
  22. - name: Enable dhcpd and unbound services
  23. systemd:
  24. name: "{{ item }}"
  25. enabled: yes
  26. loop:
  27. - dhcpd
  28. - unbound
  29. - name: Set home as default zone
  30. shell:
  31. cmd: firewall-cmd --set-default-zone=home
  32. register: setdefaultzone
  33. changed_when: "'Warning: ZONE_ALREADY_SET' not in setdefaultzone.stderr"
  34. failed_when: "'success' not in setdefaultzone.stdout"
  35. - name: Open ports
  36. firewalld:
  37. service: "{{ item }}"
  38. zone: home
  39. permanent: yes
  40. state: enabled
  41. immediate: yes
  42. loop:
  43. - dhcp
  44. - dns
  45. - name: Copy dhcpd.conf
  46. template:
  47. src: dhcpd.conf.j2
  48. dest: /etc/dhcp/dhcpd.conf
  49. owner: root
  50. group: root
  51. mode: '0644'
  52. notify:
  53. - Restart dhcpd
  54. - name: Copy unbound.conf
  55. template:
  56. src: unbound.conf.j2
  57. dest: /etc/unbound/unbound.conf
  58. owner: root
  59. group: unbound
  60. mode: '0644'
  61. notify:
  62. - Restart unbound
  63. - name: Copy unbound resolution files
  64. template:
  65. src: "{{ item }}.j2"
  66. dest: /etc/unbound/local.d/{{ item }}
  67. owner: root
  68. group: unbound
  69. mode: '0640'
  70. loop:
  71. - lan-name-resolution.conf
  72. - plug-onion-addresses.conf
  73. - server.home.conf
  74. - local.conf
  75. - home-lan.conf
  76. notify:
  77. - Restart unbound
  78. - name: Check adblock config file
  79. stat:
  80. path: /etc/unbound/local.d/ad-servers.conf
  81. register: adservers_conf
  82. - set_fact:
  83. adservers_conf_age_in_days: "{{ (lookup('pipe', 'date +%s')|int - adservers_conf.stat.ctime|int) / 86400 }}"
  84. when:
  85. - adservers_conf.stat.exists
  86. - name: Download fresh adblock config
  87. get_url:
  88. url: 'https://pgl.yoyo.org/adservers/serverlist.php?hostformat=unbound&mimetype=plaintext'
  89. dest: /etc/unbound/local.d/ad-servers.conf
  90. owner: root
  91. group: unbound
  92. mode: '0644'
  93. when:
  94. - not adservers_conf.stat.exists or adservers_conf_age_in_days|int > 30
  95. notify:
  96. - Restart unbound
  97. - name: Update /etc/hosts
  98. template:
  99. src: hosts.j2
  100. dest: /etc/hosts
  101. owner: root
  102. group: root
  103. mode: '0644'
  104. - set_fact:
  105. ethernet: "{{ (ansible_interfaces | reject('search', 'podman') | list | sort)[0] }}"
  106. - name: Configure static IP on {{ ethernet }}
  107. lineinfile:
  108. path: /etc/sysconfig/network-scripts/ifcfg-{{ ethernet }}
  109. regexp: "{{ item.regexp | default(omit) }}"
  110. line: "{{ item.line }}"
  111. loop:
  112. - { regexp: 'BOOTPROTO=', line: 'BOOTPROTO="none"' }
  113. - { regexp: 'IPADDR=', line: 'IPADDR="10.0.0.2"' }
  114. - { regexp: 'PREFIX=', line: 'PREFIX="24"' }
  115. - { regexp: 'GATEWAY=' ,line: 'GATEWAY="10.0.0.1"' }
  116. - { regexp: 'DNS1=', line: 'DNS1="{{ dns_primary }}"' }
  117. - { regexp: 'DNS2=', line: 'DNS2="{{ dns_secondary }}"' }
  118. - { regexp: 'IPV4_FAILURE_FATAL=', line: 'IPV4_FAILURE_FATAL="yes"' }
  119. - { regexp: 'IPV6INIT=', line: 'IPV6INIT="NO"' }
  120. - { regexp: 'ZONE=', line: 'ZONE="home"' }
  121. loop_control:
  122. label: "{{ item.line }}"
  123. notify:
  124. - Restart NetworkManager