I have an ansible playbook which is made up of two plays as shown below:
- First play validates everything on the localbox where I am running my ansible before invoking Play2. If my validation fails in Play1 then I don't want to start Play2 at all.
- Second play will only start if Play1 validation is successful and this play creates a directory on remoter server if it is not there and then copies
process.tar.gz
file in a particular directory on all remote servers.
Below is my playbook:
---
- name: Play 1
hosts: 127.0.0.1
tasks:
- name: check whether we have all the necessary files
shell: "ls files/ | wc -l"
retries: 10
delay: 10
register: number_of_files
until: "number_of_files.stdout == '10'"
- name: Total number of files
debug: msg="Total files {{ number_of_files.stdout }}"
- name: check whether all the files are generated within a minute difference
shell: "find files/ -mmin -1 -type f -print | wc -l"
register: file_time
failed_when: "file_time.stdout != '10'"
- name: Total number of files
debug: msg="Total files {{ file_time }}"
- name: check whether success file was generated
shell: "ls files/SUCCESS | wc -l"
register: success_file
failed_when: "success_file.stdout != '1'"
- name: Success file
debug: msg="{{ success_file }}"
- name: compress all the files in tar.gz
shell: "rm process.tar.gz; tar -czvf process.tar.gz -C files . --exclude='SUCCESS'"
- name: Play 2
hosts: ALL_HOSTS
serial: "{{ serial }}"
tasks:
- name: creates directory
file: path=/data/taks/files/ state=directory
- name: copy and untar process.tar.gz file
unarchive: src=process.tar.gz dest=/data/taks/files/
- name: sleep for few seconds
pause: seconds=20
I wanted to see if there is any better way to write my above ansible? Since I recently started working with ansible so not sure I am following all the best practices.. I am running ansible version ansible 2.4.3.0
. Also I always get a warning from ansible on this task:
- name: compress all the files in tar.gz
shell: "rm process.tar.gz; tar -czvf process.tar.gz -C files . --exclude='SUCCESS'"
Here is the warning:
[WARNING]: Consider using file module with state=absent rather than running rm
[WARNING]: Consider using unarchive module rather than running tar
Update:
Below is my path where my ansible playbook is sitting and from this directory only, I execute my PLAYBOOK1
. And files
directory contains all the files including SUCCESS
which I need to compress into tar.gz.
jenkins@machineA:~/jobs/processdata/workspace$ ls -lrth
total 145M
drwxr-xr-x 2 jenkins jenkins 4.0K Feb 19 17:36 files
-rw-r--r-- 1 jenkins root 1.6K Feb 19 19:32 PLAYBOOK1.yml
-rw-r--r-- 1 jenkins root 1.6K Feb 19 19:32 PLAYBOOK2.yml
Here is the error I am getting after I execute PLAYBOOK1
with your changes:
TASK [Check all files] **********************************************************************************************************************************************************************
fatal: [127.0.0.1]: FAILED! => {"changed": false, "examined": 0, "failed_when_result": true, "files": [], "matched": 0, "msg": "/files was skipped as it does not seem to be a valid directory or it cannot be accessed\n"}