1

I am trying to run an ansible script to execute a script using the shell module, and it completes the ansible playbook with a status of 'SUCCESS', but the script has not been executed for some reason.

Playbook:

- hosts: '{{ hosts }}'
  tasks:
  - name: Copy a dummy file to /tmp folder
    command: "cp /apps/tomcat-web/LICENSE /tmp/LICENSE-{{ ansible_date_time.iso8601 }}"

  - name: Start email service
    shell: ./start.sh >> /tmp/log.txt
    args:
      chdir: /email-service/

Console Output:

+ ansible-playbook deploy/test-ansible.yml --extra-vars hosts=mut

PLAY [mut] ****************************************************************

TASK [setup] *******************************************************************
ok: [ftp2]

TASK [Copy a dummy file to /tmp folder] ****************************************
changed: [ftp2]

TASK [Start email service] *****************************************************
changed: [ftp2]

PLAY RECAP *********************************************************************
ftp2                   : ok=3    changed=2    unreachable=0    failed=0   

Finished: SUCCESS

The first step in the playbook (creating a license file) is being done properly. But the second step (executing start.sh) is not being done. Can anyone explain? Will be much appreciated.

Note: The script's full path is /email-service/start.sh Thanks!

2
  • Execute playbook with -vvv to see what's happening with start.sh. Commented Sep 21, 2016 at 19:01
  • Thanks, see the new output below (comments for Xiong) Commented Sep 21, 2016 at 21:08

1 Answer 1

1
TASK [Start email service] *****************************************************
changed: [ftp2]

It certainly looks like your script is being run. However, perhaps it is not running successfully?

Three suggestions for helping debug it:

  1. Invoke ansible with more verbosity (more -vs).
  2. Redirect stderr into your log file as well (./start.sh >> /tmp/log.txt 2>&1)
  3. Add set -x into your script so that it prints out every command it runs.

You may also find set -euo pipefail useful in creating a well-behaved shell script that correctly exits with a failure code when encountering an error in its commands.


Separately from all of that, you should not be using command to copy a file; use the copy module instead:

  - name: Copy a dummy file to /tmp folder
    copy:
      src: /apps/tomcat-web/LICENSE
      dest: /tmp/LICENSE-{{ ansible_date_time.iso8601 }}
      remote_src: yes
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, I took your advice and did #1 and 2. But I don't think the additional output is of much help. See below:changed: [ftp2] => {"changed": true, "cmd": "./start.sh >> /tmp/log.txt 2>&1", "delta": "0:00:00.002725", "end": "2016-09-21 15:59:12.554926", "invocation": {"module_args": {"_raw_params": "./start.sh >> /tmp/log.txt 2>&1", "_uses_shell": true, "chdir": "/email-service/", "creates": null, "executable": null, "removes": null, "warn": true}, "module_name": "command"}, "rc": 0, "start": "2016-09-21 15:59:12.552201", "stderr": "", "stdout": "", "stdout_lines": [], "warnings": []} SUCCESS
The log file didn't get any new entries. But when I introduced an intentional error in the playbook, it does go to the log file now. Also, I am unable to understand how to introduce set -x and set -euo pipefail in the command. Can you tell me where I should be adding it?
Put those right at the top of the script, just below the hashbang, as the first command you execute.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.