title: Shell script execution is not working in remote server Ansible (previous tasks executed successfully)
body: <p>I am not able to execute shell script remotely in Ansible. However, there are previous tasks in the same role (filebeat) that are executed in remote server successfully. I am running the following in local server 172.28.28.6 server to install and run filebeat in remote server 172.28.28.81 </p>
  
  <p><strong>Playbook: install-filebeat.yml:</strong></p>
  
  <pre><code> hosts: filebeat-servers
    remote_user: wwwadm
    sudo: yes
    roles:
    - { role: /vagrant/roles/filebeat}
  </code></pre>
  
  <p><strong>Role filebeat: main.yml:</strong></p>
  
  <pre><code>---
  # tasks file for filebeat
  - name: "Extract Filebeat"
    unarchive:
      src: "{{ tmp_artifact_cache }}/{{ filebeat_archive }}"
      remote_src: yes
      dest: "{{ filebeat_root_dir }}"
      extra_opts: ['--transform=s,/*[^/]*,{{ filebeat_ver }},i', '--show-stored-names']
    become: yes
    become_user: "{{ filebeat_install_as }}"
    when: not ansible_check_mode 
    tags: [ 'filebeat' ]
  
  - name: Configure Filebeat
    template: 
      src: "filebeat.yml.j2"
      dest: "{{ filebeat_install_dir }}/filebeat.yml"
      mode: 0775
    become: yes
    become_user: "{{ filebeat_install_as }}"
    tags: [ 'filebeat' ]
  
  - name: 'Filebeat startup script'
    template: 
      src: "startup.sh.j2" 
      dest: "{{ filebeat_install_dir }}/bin/startup.sh" 
      mode: 0755
    become: yes
    become_user: "{{ filebeat_install_as }}"
    tags: [ 'filebeat', 'start' ]
  
  #This one does not get executed at all:
  - name: "Start Filebeat"
    # shell: "{{ filebeat_install_dir }}/bin/startup.sh"
    command: "sh {{ filebeat_install_dir }}/bin/startup.sh"
    become: yes
    become_user: "{{ filebeat_install_as }}" 
  </code></pre>
  
  <p><strong>defaults:</strong></p>
  
  <pre><code># defaults file for filebeat
  filebeat_ver: "6.6.0"
  filebeat_archive: "filebeat-{{ filebeat_ver }}-linux-x86_64.tar.gz"
  filebeat_archive_checksum : "sha1:d38d8fea7e9915582720280eb0118b7d92569b23"
  filebeat_url: "https://artifacts.elastic.co/downloads/beats/filebeat/{{ filebeat_archive }}"
  
  filebeat_root_dir: "{{ apps_home }}/filebeat"
  filebeat_data_dir: "{{ apps_data }}/filebeat"
  filebeat_log_dir: "{{ apps_logs }}/filebeat"
  filebeat_install_dir: "{{ filebeat_root_dir }}/{{ filebeat_ver }}"
  
  filebeat_cert_dir: "/etc/pki/tls/certs"
  filebeat_ssl_certificate_file: "logstash.crt"
  filebeat_ssl_key_file: "logstash.key"
  
  filebeat_install_as: "{{ install_user | default('wwwadm') }}"
  
  filebeat_set_as_current: yes
  filebeat_force_clean_install: no
  
  filebeat_java_home: "{{ sw_home }}/jdk"
  </code></pre>
  
  <p><strong>inventory/local/hosts:</strong></p>
  
  <pre><code>localhost ansible_connection=local
  
  [filebeat-servers]
  172.28.28.81 ansible_user=vagrant ansible_connection=ssh
  </code></pre>
  
  <p>Filebeat is installed and changes are done in the remote server except the last step which is the execution of shell script</p>
  
  <p>When running the playbook as follows:</p>
  
  <pre><code>ansible-playbook -i /vagrant/inventory/local install-filebeat.yml -vvv
  </code></pre>
  
  <p>Getting the following output related to the shell execution:</p>
  
  <pre><code>TASK [/vagrant/roles/filebeat : Start Filebeat] ***************************************************************************************************************************************************************
  task path: /vagrant/roles/filebeat/tasks/main.yml:184
  &lt;172.28.28.81&gt; ESTABLISH SSH CONNECTION FOR USER: vagrant
  &lt;172.28.28.81&gt; SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=vagrant -o ConnectTimeout=10 -o ControlPath=/home/vagrant/.ansible/cp/f66f05c055 172.28.28.81 '/bin/sh -c '"'"'echo ~vagrant &amp;&amp; sleep 0'"'"''
  &lt;172.28.28.81&gt; (0, '/home/vagrant\n', '')
  &lt;172.28.28.81&gt; ESTABLISH SSH CONNECTION FOR USER: vagrant
  &lt;172.28.28.81&gt; SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=vagrant -o ConnectTimeout=10 -o ControlPath=/home/vagrant/.ansible/cp/f66f05c055 172.28.28.81 '/bin/sh -c '"'"'( umask 77 &amp;&amp; mkdir -p "` echo /var/tmp/ansible-tmp-1550178583.24-35955954120606 `" &amp;&amp; echo ansible-tmp-1550178583.24-35955954120606="` echo /var/tmp/ansible-tmp-1550178583.24-35955954120606 `" ) &amp;&amp; sleep 0'"'"''
  &lt;172.28.28.81&gt; (0, 'ansible-tmp-1550178583.24-35955954120606=/var/tmp/ansible-tmp-1550178583.24-35955954120606\n', '')
  Using module file /usr/lib/python2.7/site-packages/ansible/modules/commands/command.py
  &lt;172.28.28.81&gt; PUT /home/vagrant/.ansible/tmp/ansible-local-13658UX7cBC/tmpFzf2Ll TO /var/tmp/ansible-tmp-1550178583.24-35955954120606/AnsiballZ_command.py
  &lt;172.28.28.81&gt; SSH: EXEC sftp -b - -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=vagrant -o ConnectTimeout=10 -o ControlPath=/home/vagrant/.ansible/cp/f66f05c055 '[172.28.28.81]'
  &lt;172.28.28.81&gt; (0, 'sftp&gt; put /home/vagrant/.ansible/tmp/ansible-local-13658UX7cBC/tmpFzf2Ll /var/tmp/ansible-tmp-1550178583.24-35955954120606/AnsiballZ_command.py\n', '')
  &lt;172.28.28.81&gt; ESTABLISH SSH CONNECTION FOR USER: vagrant
  &lt;172.28.28.81&gt; SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=vagrant -o ConnectTimeout=10 -o ControlPath=/home/vagrant/.ansible/cp/f66f05c055 172.28.28.81 '/bin/sh -c '"'"'setfacl -m u:wwwsvr:r-x /var/tmp/ansible-tmp-1550178583.24-35955954120606/ /var/tmp/ansible-tmp-1550178583.24-35955954120606/AnsiballZ_command.py &amp;&amp; sleep 0'"'"''
  &lt;172.28.28.81&gt; (0, '', '')
  &lt;172.28.28.81&gt; ESTABLISH SSH CONNECTION FOR USER: vagrant
  &lt;172.28.28.81&gt; SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=vagrant -o ConnectTimeout=10 -o ControlPath=/home/vagrant/.ansible/cp/f66f05c055 -tt 172.28.28.81 '/bin/sh -c '"'"'sudo -H -S -n -u wwwsvr /bin/sh -c '"'"'"'"'"'"'"'"'echo BECOME-SUCCESS-ntzchfzqggiteuqwzpiurlloddbdhevp; /usr/bin/python /var/tmp/ansible-tmp-1550178583.24-35955954120606/AnsiballZ_command.py'"'"'"'"'"'"'"'"' &amp;&amp; sleep 0'"'"''
  Escalation succeeded
  &lt;172.28.28.81&gt; (0, '\r\n{"changed": true, "end": "2019-02-14 13:09:44.800191", "stdout": "Starting Filebeat", "cmd": ["sh", "/apps_ux/filebeat/6.6.0/bin/startup.sh"], "rc": 0, "start": "2019-02-14 13:09:43.792122", "stderr": "+ export JAVA_HOME=/sw_ux/jdk\\n+ JAVA_HOME=/sw_ux/jdk\\n+ echo \'Starting Filebeat\'\\n+ /apps_ux/filebeat/6.6.0/bin/filebeat -c /apps_ux/filebeat/6.6.0/config/filebeat.yml -path.home /apps_ux/filebeat/6.6.0 -path.config /apps_ux/filebeat/6.6.0/config -path.data /apps_data/filebeat -path.logs /apps_data/logs/filebeat", "delta": "0:00:01.008069", "invocation": {"module_args": {"warn": true, "executable": null, "_uses_shell": false, "_raw_params": "sh /apps_ux/filebeat/6.6.0/bin/startup.sh", "removes": null, "argv": null, "creates": null, "chdir": null, "stdin": null}}}\r\n', 'Shared connection to 172.28.28.81 closed.\r\n')
  &lt;172.28.28.81&gt; ESTABLISH SSH CONNECTION FOR USER: vagrant
  &lt;172.28.28.81&gt; SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=vagrant -o ConnectTimeout=10 -o ControlPath=/home/vagrant/.ansible/cp/f66f05c055 172.28.28.81 '/bin/sh -c '"'"'rm -f -r /var/tmp/ansible-tmp-1550178583.24-35955954120606/ &gt; /dev/null 2&gt;&amp;1 &amp;&amp; sleep 0'"'"''
  &lt;172.28.28.81&gt; (0, '', '')
  changed: [172.28.28.81] =&gt; {
      "changed": true,
      "cmd": [
          "sh",
          "/apps_ux/filebeat/6.6.0/bin/startup.sh"
      ],
      "delta": "0:00:01.008069",
      "end": "2019-02-14 13:09:44.800191",
      "invocation": {
          "module_args": {
              "_raw_params": "sh /apps_ux/filebeat/6.6.0/bin/startup.sh",
              "_uses_shell": false,
              "argv": null,
              "chdir": null,
              "creates": null,
              "executable": null,
              "removes": null,
              "stdin": null,
              "warn": true
          }
      },
      "rc": 0,
      "start": "2019-02-14 13:09:43.792122",
      "stderr": "+ export JAVA_HOME=/sw_ux/jdk\n+ JAVA_HOME=/sw_ux/jdk\n+ echo 'Starting Filebeat'\n+ /apps_ux/filebeat/6.6.0/bin/filebeat -c /apps_ux/filebeat/6.6.0/config/filebeat.yml -path.home /apps_ux/filebeat/6.6.0 -path.config /apps_ux/filebeat/6.6.0/config -path.data /apps_data/filebeat -path.logs /apps_data/logs/filebeat",
      "stderr_lines": [
          "+ export JAVA_HOME=/sw_ux/jdk",
          "+ JAVA_HOME=/sw_ux/jdk",
          "+ echo 'Starting Filebeat'",
          "+ /apps_ux/filebeat/6.6.0/bin/filebeat -c /apps_ux/filebeat/6.6.0/config/filebeat.yml -path.home /apps_ux/filebeat/6.6.0 -path.config /apps_ux/filebeat/6.6.0/config -path.data /apps_data/filebeat -path.logs /apps_data/logs/filebeat"
      ],
      "stdout": "Starting Filebeat",
      "stdout_lines": [
          "Starting Filebeat"
      ]
  }
  META: ran handlers
  META: ran handlers
  
  PLAY RECAP ****************************************************************************************************************************************************************************************************
  172.28.28.81              : ok=18   changed=7    unreachable=0    failed=0
  </code></pre>
  
  <p>On remote server:</p>
  
  <pre><code>[6.6.0:vagrant]$ cd bin
  [bin:vagrant]$ ls -ltr
  total 36068
  -rwxr-xr-x. 1 wwwadm wwwadm 36927014 Jan 24 02:30 filebeat
  -rwxr-xr-x. 1 wwwadm wwwadm      478 Feb 14 12:54 startup.sh
  [bin:vagrant]$ pwd
  /apps_ux/filebeat/6.6.0/bin
  [bin:vagrant]$ more startup.sh
  #!/usr/bin/env bash
  set -x
  
  export JAVA_HOME="/sw_ux/jdk"
  
  #To save pid into a file is an open feature: https://github.com/elastic/logstash/issues/3577. There is no -p flag for filebeat to save the pid and then kill it.
  echo 'Starting Filebeat'
  /apps_ux/filebeat/6.6.0/bin/filebeat -c /apps_ux/filebeat/6.6.0/config/filebeat.yml -path.home /apps_ux/filebeat/6.6.0 -path.config /apps_ux/filebeat/6.6.0/config -path.data /apps_data/filebeat -path.logs /a
  pps_data/logs/filebeat &amp;
  </code></pre>
  
  <p>No process running found by executing ps command</p>
  
  <pre><code>[bin:vagrant]$ ps -fea | grep filebeat | grep -v grep
  </code></pre>
  
  <p>However, if I connect to the remote server, I am able to run filebeat by executing the script with the user wwwadm and filebeat starts successfully:</p>
  
  <pre><code>[bin:wwwadm]$ pwd
  /apps_ux/filebeat/6.6.0/bin
  
  [bin:wwwadm]$ id
  uid=778(wwwadm) gid=778(wwwadm) groups=778(wwwadm) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
  
  [bin:wwwadm]$ ./startup.sh
  + export JAVA_HOME=/sw_ux/jdk
  + JAVA_HOME=/sw_ux/jdk
  + echo 'Starting Filebeat'
  Starting Filebeat
  + /apps_ux/filebeat/6.6.0/bin/filebeat -c /apps_ux/filebeat/6.6.0/config/filebeat.yml -path.home /apps_ux/filebeat/6.6.0 -path.config /apps_ux/filebeat/6.6.0/config -path.data /apps_data/filebeat -path.logs /apps_data/logs/filebeat
  
  [bin:wwwadm]$ ps -fea | grep filebeat | grep -v grep
  wwwadm   19160     1  0 15:12 pts/0    00:00:00 /apps_ux/filebeat/6.6.0/bin/filebeat -c /apps_ux/filebeat/6.6.0/config/filebeat.yml -path.home /apps_ux/filebeat/6.6.0 -path.config /apps_ux/filebeat/6.6.0/config -path.data /apps_data/filebeat -path.logs /apps_data/logs/filebeat
  </code></pre>
  
  <p>Thanks</p>