restart_server.yml

This is the playbook fired by the check_website.yml rulebook.

It will detemine the hostname from the ansible_eda variable and restart the host. If it doen't respoind to that correctly, it will start a redeployment of that host and redeploy the site data through a gitlab pipeline.

So whatever the reason it goes down,it will be up again in 5 minutes tops.

Ensure to have a credential added to the job_template, that defines the variables needed to use the infra.aap_configuration collection and that defines a user with enough rights to start the deployment job_template.

---

- name: Set the servername
  hosts: localhost
  gather_facts: false
  become: true

  tasks:

    - name: Set the url_var
      ansible.builtin.set_fact:
        url_var: "{{ ansible_eda['event']['url_check']['url'] }}"

    - name: Set the host to recover
      ansible.builtin.set_fact:
        server_name: "{{ url_var | urlsplit('hostname') }}"

    - name: Set the git site branch to dev
      ansible.builtin.set_fact:
        _branch: dev
      when: "'dev' in server_name"

    - name: Set the git site branch to master
      ansible.builtin.set_fact:
        _branch: master
      when: "'prod' in server_name"

    - name: Get the hostlist from proxmox cluster
      community.proxmox.proxmox_vm_info:
        api_host: proxmox01.homelab
        api_user: <username>
        api_password: <password>
        api_port: 8006
      register: _vm_info

    - name: Recover if VM is present
      when: server_name in _vm_info
      block:
        - name: Stop container with force
          community.proxmox.proxmox:
            hostname: "{{ server_name }}"
            api_user: <username>
            api_password: <password>
            api_host: proxmox01.homelab
            api_port: 8006
            force: true
            state: stopped

        - name: Start the container
          community.proxmox.proxmox:
            hostname: "{{ server_name }}"
            api_user: <username>
            api_password: <password>
            api_host: proxmox01.homelab
            api_port: 8006
            state: started

Back