如何使用ansible从节点中计算计数文件的总和

时间:2019-06-24 14:06:07

标签: ansible

我有一个进程,该进程从“ in”文件夹中获取文件,并在该进程失败时将其放入“ error”文件夹中。 我有2个由4个节点组成的集群,该进程正在运行。 我想同时计算每个节点上“ in”文件夹和“ error”文件夹中的文件,但是我也必须按群集来总计总数

这是我尝试过的方法,但是不起作用

- 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 }}"

我想对每个cluster_data1cluster_data2进行total_files_error计数,但我认为我不太了解set_fact的工作原理

这就是我现在拥有的:

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"
}

这是我的期望:

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"
}

我想我可以用set_fact做些什么,但是我不知道怎么做。你能帮我吗?

请问有人可以帮助我吗?

提前谢谢

关于, 威纳尔(Winael)

1 个答案:

答案 0 :(得分:1)

也许这个例子可以帮助您:

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

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