name: Ansible Apply because of host_vars change
on:
  push:
    branches:
      - main
    paths:
      - ansible/host_vars/*.alexpires.me.yml
      - ansible/host_vars/*.a13labs.me.yml
      - ansible/host_vars/*.a13labs.pt.yml
jobs:
  ansible-apply:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Setup environment
        id: setup_env
        uses: ./.github/actions/setup-env

      - name: Setup Ansible environment
        uses: ./.github/actions/setup-ansible-env
        with:
          project-id: ${{ secrets.BW_PROJECT_ID }}
          organization-id: ${{ secrets.BW_ORGANIZATION_ID }}
          access-token: ${{ secrets.BW_ACCESS_TOKEN }}
          ansible-user: ${{ secrets.ANSIBLE_USER }}
          trusted-hosts: ${{ secrets.TRUSTED_HOSTS }}

      - name: Apply the playbook that triggered the workflow
        env:
          BW_PROJECT_ID: ${{ secrets.BW_PROJECT_ID }}
          BW_ORGANIZATION_ID: ${{ secrets.BW_ORGANIZATION_ID }}
          BW_ACCESS_TOKEN: ${{ secrets.BW_ACCESS_TOKEN }}
          ANSIBLE_USER: ${{ secrets.ANSIBLE_USER }}
          SSH_AUTH_SOCK: /tmp/ssh_agent.sock
          CHANGED_FILES: ${{ steps.setup_env.outputs.CHANGED_FILES }}
        run: |
          cd ansible
          sectool -f ../sectool.json exec ansible-playbook -u $ANSIBLE_USER playbook_fail2ban.yml
          sectool -f ../sectool.json exec ansible-playbook -u $ANSIBLE_USER playbook_geoip.yml
          sectool -f ../sectool.json exec ansible-playbook -u $ANSIBLE_USER playbook_duo_security.yml
          sectool -f ../sectool.json exec ansible-playbook -u $ANSIBLE_USER playbook_encryption.yml
          sectool -f ../sectool.json exec ansible-playbook -u $ANSIBLE_USER playbook_microk8s.yml
          sectool -f ../sectool.json exec ansible-playbook -u $ANSIBLE_USER playbook_gitea.yml
          sectool -f ../sectool.json exec ansible-playbook -u $ANSIBLE_USER playbook_nextcloud.yml
          sectool -f ../sectool.json exec ansible-playbook -u $ANSIBLE_USER playbook_websites.yml
          sectool -f ../sectool.json exec ansible-playbook -u $ANSIBLE_USER playbook_m3uproxy.yml
          sectool -f ../sectool.json exec ansible-playbook -u $ANSIBLE_USER playbook_monitoring.yml
          sectool -f ../sectool.json exec ansible-playbook -u $ANSIBLE_USER playbook_rustdesk.yml
