<p>First - <a href="https://devops.stackexchange.com/users/16819/caxcaxcoatl">@caxcaxcoatl</a> is completly right - your playbook is not Ansible, it is a bash script inside Ansible and that isn't Ansible.</p>
<p>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 <code>curl</code>, <code>telnet</code> 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 <code>/portal-client/cm/misalud/</code>.</p>
<p>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.):</p>
<pre><code>- 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
</code></pre>

CC BY-SA 4.0
2021-01-16T17:45:03.893Z
2021-01-16T17:45:03.893Z
TRW
<p>I execute an Ansible playbook with  an <code>ansible</code> user. The playbook executes properly, with no errors in the application log, no errors in the Ansible log. This <code>.war</code> usually deploys with user &quot;tomcat&quot; when the deploy is manual.</p>
<p>Could this be the cause of the problem? Execute playbook with different users</p>
<p>This is my playbook (syntax is correct):</p>
<pre class="lang-yaml prettyprint-override"><code>- name: Deploy war
  hosts: grupo1
  tasks:
    - name: Stop Tomcat
      command: systemctl stop tomcat

    - name: Delete old war1
      command: rm -rf /opt/tomcat/catalina_base/webapps.old/portal-admin
      args:
        warn: false

    - name: Delete old war
      command: rm -rf /opt/tomcat/catalina_base/webapps.old/portal-client
      args:
        warn: false

    - name: Copy the old war
      copy:
        src: /opt/tomcat/catalina_base/webapps/portal-client
        remote_src: true
        dest: /opt/tomcat/catalina_base/webapps.old/

    - name: Copy the old war2
      copy:
        src: /opt/tomcat/catalina_base/webapps/portal-admin
        remote_src: true
        dest: /opt/tomcat/catalina_base/webapps.old/


    - name: delte the war fileclient
      shell: /bin/rm -rf /opt/tomcat/catalina_base/webapps/portal-client/*
      args:
        warn: false

    - name: deploy warclient
      command: unzip /tmp/portal-client.war -d /opt/tomcat/catalina_base/webapps/portal-client/
        args:
          warn: false

    - name: delete the war fileadmin
      shell: /bin/rm -rf /opt/tomcat/catalina_base/webapps/portal-admin/*
      args:
        warn: false

    - name: deploy waradmin
      command: unzip /tmp/portal-admin.war -d /opt/tomcat/catalina_base/webapps/portal-admin/
      args:
        warn: false

    - name: Start tomcat
      command: systemctl start tomcat


</code></pre>
<p>The error when I execute the playbook is</p>
<pre><code>Proxy Error
The proxy server received an invalid response from an upstream server.
The proxy server could not handle the request GET /portal-client/cm/misalud/.

Reason: Error reading from remote server
</code></pre>

CC BY-SA 4.0
2021-01-10T04:17:34.487Z
There are some problems with your question and with your approach to Ansible.  On the question, it's not really clear what is the error, as  this is an Ansible question and you say there were no errors on the playbook; also, the error you pasted does not look to come from Ansible.  Please describe what is the actual error you're facing, and add logs
caxcaxcoatl
CC BY-SA 4.0
2021-01-10T04:22:40.777Z
On the approach to Ansible,  you're using `command` and `shell` all around, for stuff  you could be using Ansible modules for.  For removing stuff, for example, use `file` module with `state: absent`; for service stop/start, use the `service` module instead of `systemctl`.  Check also `unarchive`.  The use of modules makes your playbook more readable and more portable.  `shell`, `command` and similar modules should be used only when the functionality is not covered by Ansible.
caxcaxcoatl
<ansible><deployment>
Error 502 before execute Ansible playbook
Andrew De leon
# Error 502 before execute Ansible playbook

(Asked by: username_0 on 2020-12-02)

I execute an Ansible playbook with  an ansible user. The playbook executes properly, with no errors in the application log, no errors in the Ansible log. This .war usually deploys with user "tomcat" when the deploy is manual.
Could this be the cause of the problem? Execute playbook with different users
This is my playbook (syntax is correct):
- name: Deploy war
  hosts: grupo1
  tasks:
    - name: Stop Tomcat
      command: systemctl stop tomcat

    - name: Delete old war1
      command: rm -rf /opt/tomcat/catalina_base/webapps.old/portal-admin
      args:
        warn: false

    - name: Delete old war
      command: rm -rf /opt/tomcat/catalina_base/webapps.old/portal-client
      args:
        warn: false

    - name: Copy the old war
      copy:
        src: /opt/tomcat/catalina_base/webapps/portal-client
        remote_src: true
        dest: /opt/tomcat/catalina_base/webapps.old/

    - name: Copy the old war2
      copy:
        src: /opt/tomcat/catalina_base/webapps/portal-admin
        remote_src: true
        dest: /opt/tomcat/catalina_base/webapps.old/


    - name: delte the war fileclient
      shell: /bin/rm -rf /opt/tomcat/catalina_base/webapps/portal-client/*
      args:
        warn: false

    - name: deploy warclient
      command: unzip /tmp/portal-client.war -d /opt/tomcat/catalina_base/webapps/portal-client/
        args:
          warn: false

    - name: delete the war fileadmin
      shell: /bin/rm -rf /opt/tomcat/catalina_base/webapps/portal-admin/*
      args:
        warn: false

    - name: deploy waradmin
      command: unzip /tmp/portal-admin.war -d /opt/tomcat/catalina_base/webapps/portal-admin/
      args:
        warn: false

    - name: Start tomcat
      command: systemctl start tomcat



The error when I execute the playbook is
Proxy Error
The proxy server received an invalid response from an upstream server.
The proxy server could not handle the request GET /portal-client/cm/misalud/.

Reason: Error reading from remote server

**Tags:** ansible, deployment

> **Comments:**
> **username_1**: There are some problems with your question and with your approach to Ansible.  On the question, it's not really clear what is the error, as  this is an Ansible question and you say there were no errors on the playbook; also, the error you pasted does not look to come from Ansible.  Please describe what is the actual error you're facing, and add logs  
> **username_1**: On the approach to Ansible,  you're using `command` and `shell` all around, for stuff  you could be using Ansible modules for.  For removing stuff, for example, use `file` module with `state: absent`; for service stop/start, use the `service` module instead of `systemctl`.  Check also `unarchive`.  The use of modules makes your playbook more readable and more portable.  `shell`, `command` and similar modules should be used only when the functionality is not covered by Ansible.  

---

## 1 answers

### Answer by username_2 on 2021-01-16. Score: 2

First - @username_1 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 "/opt/tomcat/catalina_base/webapps"
    webapps_old: "/opt/tomcat/catalina_base/webapps.old"
  tasks:
    - name: "Stop Tomcat"
      systemd:
        name: tomcat
        state: stopped

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

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

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

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

    - name: "Start Tomcat"
      systemd:
        name: tomcat
        state: started


---

