2

I am running an ansible-playbook which is doing running tar command to zip a directory. Following is the ansible task.

  - name: tar the old code
    command: tar -czf {{ansible_date_time.date}}.tar.gz /home/ubuntu/my-folder

The above gives the following error.

"warnings": use unarchive module rather than running tar stderr: tar: Removing leading '/' from member names tar: /home/ubuntu/my-folder/xyz.log: file change as we read it

I also tried with option --ignore-failed-read but it didn't zipped the directory but ran the rest of the tasks successfully.

  - name: tar the old code
    command: tar -czf {{ansible_date_time.date}}.tar.gz /home/ubuntu/my-folder  --ignore-failed-read

Since this task is in between other tasks, the tasks which has to be run after this one fails.

ansible doesn't give module to tar the code. only unarchive module is there to unzip the directory.

udondan
  • 57,263
  • 20
  • 190
  • 175
Ajeet Khan
  • 8,582
  • 8
  • 42
  • 65
  • 1
    Possible duplicate of [tar: file changed as we read it](http://stackoverflow.com/questions/20318852/tar-file-changed-as-we-read-it) – udondan Jan 27 '16 at 03:22
  • I don't think this is an Ansible issue. The command you run fails, so it is a pure `tar` issue. – udondan Jan 27 '16 at 03:24
  • The warning regarding `unarchive` is just a stupidity of Ansible. It scans for the first word of a command and complains if it matches a pre-defined dict... You can get rid of it by using `"\`which tar\` ..."` instead of `tar ...` – udondan Jan 27 '16 at 03:26
  • @udondan I know it's tar issue but to get rid of it so that rest of the tasks should get completed. – Ajeet Khan Jan 27 '16 at 05:01
  • You should strip down the problem as much as possible. If you know the problem is related to `tar` it is not relevant how you use it - in this case in an Ansible task. Mentioning Ansible would only be useful if the command runs without problems when ran directly without Ansible. Or, if you simply want to ignore the error. If the latter, simply add `ignore_errors: yes` to your task. The task would still fail but following tasks would still be executed. http://docs.ansible.com/ansible/playbooks_error_handling.html – udondan Jan 27 '16 at 05:11
  • @udondan Problem is not solely with tar but how it interacts with Ansible - `ignore_errors: yes` is too strong and proposed answer with `failed_when: tar_result.rc > 1` is much better – kodstark May 07 '19 at 14:33

1 Answers1

1

The tar command will exit with a return code of 1 when it experiences the "file change as we read it" problem, and while I can't speak with too much authority as to how Ansible interprets that, I'm assuming it will treat any non-zero return code as "failed." I worked around the issue by telling Ansible to redefine what it considers to be failure:

- name: tar the old code
  command: tar -czf {{ansible_date_time.date}}.tar.gz /home/ubuntu/my-folder
  register: tar_result
  failed_when: tar_result.rc > 1
ktower
  • 111
  • 2