# roles/dhcp/tasks/main.yml
---

- name: Load distro-specific variables
  include_vars: "{{ item }}"
  with_first_found:
    - "{{ ansible_distribution }}.yml"
    - "{{ ansible_os_family }}.yml"
    - "{{ default }}.yml"
  tags: dhcp

- name: Install packages
  package:
    name: "{{ dhcp_packages }}"
    state: "{{ dhcp_packages_state }}"
  tags: dhcp

- name: Install includes
  copy:
    src: "{{ item }}"
    dest: "{{ dhcp_config_dir }}/{{ item | basename }}"
  with_items: "{{ dhcp_global_includes }}"
  when: dhcp_global_includes is defined
  ignore_errors: "{{ dhcp_global_includes_missing }}"
  tags: dhcp

- name: Set config directory perms
  file:
    path: "{{ dhcp_config | dirname }}"
    state: directory
    mode: 0755
  tags: dhcp

- name: Install config file
  template:
    src: etc_dhcp_dhcpd.conf.j2
    dest: "{{ dhcp_config }}"
    owner: root
    group: root
    mode: 0644
    validate: 'dhcpd -t -cf %s'
  notify: restart dhcp
  tags: dhcp

- name: "Ensure service is {{ dhcp_global_server_state | default('started') }}"
  service:
    name: "{{ dhcp_service }}"
    state: "{{ dhcp_global_server_state | default('started') }}"
    enabled: true
  tags: dhcp
