---
- name: Configure Hotspot Wi-Fi
  hosts: localhost
  become: yes

  tasks:

    - name: Create accesspoint directory
      command: mkdir /etc/accesspoint
      become: yes
      ignore_errors: yes

    - name: Set accesspoint config
      template:
        src: accesspoint.json
        dest: /etc/accesspoint/accesspoint.json
      become: yes

    - name: Set accesspoint hostapd
      template:
        src: hostapd.config
        dest: /etc/accesspoint/hostapd.config
      become: yes

    # ENABLE SHARE of HOST
    - name: Configure network interface
      lineinfile:
        path: /etc/network/interfaces
        line: "{{ item }}"
      loop:
        - "allow-hotplug wlan0"
        - "iface wlan0 inet static"
        - "address 192.168.1.1"
        - "netmask 255.255.255.0"

    # ENABLE SHARE of HOST
    - name: Set static IP address 
      command: ip address add 192.168.1.1/24 broadcast 192.168.1.255 dev wlan0

    - name: Restart networking service
      service:
        name: networking
        state: restarted

    - name: Start accesspoint
      command: pyaccesspoint --config start
      become: yes
      ignore_errors: yes

    - name: Enable Wi-Fi
      command: nmcli radio wifi on
      ignore_errors: yes
