Error 502 before execute Ansible playbook
First - @caxcaxcoatl is completly right - your playbook is not Ansible, it is a bash script inside Ansible and that isn't Ansible.
Your error doesn't have something to do with your playbook, but with that, what the playbook is doing. You're redeploying an application and then the proxy in front of the Tomcat cannot read the response from your Tomcat. So your deployment is wrong, has some failures, what ever. You should have a look at your Tomcat HTTP response (without the Proxy in front) direct on the machine (with curl, telnet or whatever). Maybe you should also check the logs of Tomcat to get the error. The proxy sends the error, because the Tomcat is answering with a 500 HTTP error code, when the proxy hits /portal-client/cm/misalud/.
To give you a hint, how a playbook could look like (of course this wasn't the question, and it could be done much better then this with includes for a unspecific WAR and just call include_tasks on all available WARs in the main playbook, or with a Ansible module written in Python just giving the params of the new WAR etc.):
- name: Deploy war
  hosts: grupo1
  vars:
    webapps_cur &quot;/opt/tomcat/catalina_base/webapps&quot;
    webapps_old: &quot;/opt/tomcat/catalina_base/webapps.old&quot;
  tasks:
    - name: &quot;Stop Tomcat&quot;
      systemd:
        name: tomcat
        state: stopped

    - name: &quot;Delete old Backup WARs&quot;
      file:
        path: &quot;{{ webapps_old }}/{{ item }}&quot;
        state: absent
      with_items:
        - &quot;portal-admin&quot;
        - &quot;portal-client&quot;

    - name: &quot;Backup the current WARs&quot;
      copy:
        src: &quot;{{ webapps_cur }}/{{ item }}&quot;
        dest: &quot;{{ webapps_old }}/{{ item }}&quot;
        remote_src: true
      with_items:
        - &quot;portal-admin&quot;
        - &quot;portal-client&quot;

    - name: &quot;Delete the current WARs&quot;
      file:
        path: &quot;{{ webapps_cur }}/{{ item }}&quot;
        state: absent
      with_items:
        - &quot;portal-admin&quot;
        - &quot;portal-client&quot;

    - name: &quot;Deploy new WARs&quot;
      unarchive:
        src: &quot;/tmp/{{ item }}.war&quot;
        dest: &quot;{{ webapps_cur }}/{{ item }}&quot;
        user: &quot;tomcat&quot;
        group: &quot;tomcat&quot;
      with_items:
        - &quot;portal-admin&quot;
        - &quot;portal-client&quot;

    - name: &quot;Start Tomcat&quot;
      systemd:
        name: tomcat
        state: started