How to make a sum of counted files from node with ansible

Viewed 794

I have a process that takes file from an "in" folder and puts them in an "error" folder when the process failed. I have 2 clusters of 4 nodes where this process is running. I want to count files both, in "in" folder and "error" folder on each node, but I want to have to total count too by cluster

This is what I tried but it's not working

- name: Count files in Error and in
  hosts: cluster_data
  become: true
  gather_facts: no
  vars:
    error_path: "{{ ERROR_PATH }}"
    in_path: "{{ IN_PATH }}"
  tasks:
    - name: initiate total_files_error
      set_fact:
         total_files_error: 0
    - name: "count number of files in {{ error_path }} by node"
      files: 
        paths: "{{ error_path }}"
      register: count_files_error
    - name: print number of files in {{ error_path }} by node"
      debug: 
        msg: "{{ count_files_error.examined }}"
    - name: "count number of files in {{ error_path }} by cluster"
      set_fact:
        total_files_error: "{{ total_files_error + count_files_error.examined|int }}"
    - name: "print number of files in {{ error_path }} by cluster"
      debug: 
        msg: "{{ total_files_error }}"

I'd like to have the total_files_error count for each cluster_data1 and cluster_data2 but I don't think I understand well how set_fact works

This is that I have now :

TASK [initiate total_files_error] **********************************************
ok: [cluster_data11]
ok: [cluster_data12]
ok: [cluster_data21]
ok: [cluster_data22]

TASK [count number of files in /data/a2/error] *********************************
ok: [cluster_data21]
ok: [cluster_data12]
ok: [cluster_data11]
ok: [cluster_data22]

TASK [debug] *******************************************************************
ok: [cluster_data12] => {
    "msg": "504"
}
ok: [cluster_data21] => {
    "msg": "534"
}
ok: [cluster_data11] => {
    "msg": "1926"
}
ok: [cluster_data22] => {
    "msg": "5025"
}

TASK [set_fact] ****************************************************************
ok: [cluster_data12]
ok: [cluster_data21]
ok: [cluster_data11]
ok: [cluster_data22]

TASK [debug] *******************************************************************
ok: [cluster_data11] => {
    "msg": "1926"
}
ok: [cluster_data12] => {
    "msg": "504"
}
ok: [cluster_data21] => {
    "msg": "534"
}
ok: [cluster_data22] => {
    "msg": "5025"
}

This is what I expected:

TASK [initiate total_files_error] **********************************************
ok: [cluster_data11]
ok: [cluster_data12]
ok: [cluster_data21]
ok: [cluster_data22]

TASK [count number of files in /data/a2/error] *********************************
ok: [cluster_data21]
ok: [cluster_data12]
ok: [cluster_data11]
ok: [cluster_data22]

TASK [debug] *******************************************************************
ok: [cluster_data12] => {
    "msg": "504"
}
ok: [cluster_data21] => {
    "msg": "534"
}
ok: [cluster_data11] => {
    "msg": "1926"
}
ok: [cluster_data22] => {
    "msg": "5025"
}

TASK [set_fact] ****************************************************************
ok: [cluster_data12]
ok: [cluster_data21]
ok: [cluster_data11]
ok: [cluster_data22]

TASK [debug] *******************************************************************
ok: [cluster_data1] => {
    "msg": "2430"
}
ok: [cluster_data2] => {
    "msg": "5559"
}

I suppose I can do something with set_fact but I don't figure out how. Can you help me on that ?

Is there someone who can help me please?

Thx in advance

Regards, Winael

1 Answers

maybe this example will help you:

- name: count files
  find: 
    paths: {{ error_path }}
  register: file_count

- name: print file_count
  debug:
    msg: "{{ file_count.examined }}"
Related