【问题标题】:How do I check whether a given directory is empty with Ansible?如何使用 Ansible 检查给定目录是否为空?
【发布时间】:2019-06-06 13:41:06
【问题描述】:

我正在尝试实现一个角色,在继续执行剧本的其余部分之前检查给定目录是否为空。

我已经尝试过这段代码,但我遇到了一个错误,我不确定正确的实现。

- name: Check if d folder is empty before proceeding
  find:
   paths: c/d/
   patterns: "*.*"
   register: filesFound

- fail:
     msg: The d folder is not empty. 
  when: filesFound.matched > 0

- debug:
    msg: "The d folder is empty. Proceeding."

这是我得到的错误:

fatal FAILED! =>
{"changed": false,
"module_stderr": "Exception calling \"Create\" with \"1\" argument(s):
\"At line:4 char:21
def _ansiballz_main():
An expression was expected after '('.
At line:12 char:27
except (AttributeError, OSError):
Missing argument in parameter list.
At line:14 char:7
if scriptdir is not None:
Missing '(' after 'if' in if statement.
At line:21 char:7
if sys.version_info < (3,):
Missing '(' after 'if' in if statement.
At line:21 char:30
if sys.version_info < (3,):
Missing expression after ','.
At line:21 char:25
if sys.version_info < (3,):
The '<' operator is reserved for future use.
At line:23 char:32
MOD_DESC = ('.py', 'U', imp.PY_SOURCE)
Missing expression after ','.
At line:23 char:33
MOD_DESC = ('.py', 'U', imp.PY_SOURCE)                                 Unexpected token 'imp.PY_SOURCE' in expression or statement.
At line:23 char:32
MOD_DESC = ('.py', 'U', imp.PY_SOURCE)
Missing closing ')' in expression.
At line:23 char:46
MOD_DESC = ('.py', 'U', imp.PY_SOURCE)
Unexpected token ')' in expression or statement.
Not all parse errors were reported.  Correct the reported errors and try again.\"
At line:6 char:1
$exec_wrapper = [ScriptBlock]::Create($split_parts[0])
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo          : NotSpecified: (:) [], MethodInvocationException\r\n    + FullyQualifiedErrorId : ParseException\r\n \r\nThe expression after '&' in a pipeline element produced an object that was not valid. It must result in a command
name, a script block, or a CommandInfo object.
At line:7 char:2
&$exec_wrapper
~~~~~~~~~~~~~\
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : BadExpression
", "module_stdout": "", "msg": "MODULE FAILURE
See stdout/stderr for the exact error", "rc": 1}

【问题讨论】:

  • 在您的代码中存在缩进错误。寄存器应与查找处于同一级别。此外,您发布的错误似乎并不完整
  • @HermanTheGermanHesse 谢谢,我已经用完整的错误更新了错误。我已经尝试了你的两个建议,但仍然遇到同样的错误
  • 您是否有机会尝试在 Windows 上运行它?

标签: ansible yaml


【解决方案1】:

你是在正确的方式,但正如@HermanTheGermanHesse 提到的,有一个识别错误,试试这个:

- name: Check if d folder is empty before proceeding
  find:
    paths: 'c/d/'
  register: filesFound

- fail:
     msg: 'The d folder is not empty.'
  when: filesFound.matched > 0

- debug:
    msg: 'The d folder is empty. Proceeding.'

【讨论】:

    【解决方案2】:

    您可以使用shell module

    - name: Register contents of /a/b/c
      shell: ls -la
      args:
        chdir: /a/b/c
      register: contents
    
    - name: do stuff when not empty
      debug:
        msg: "There are elements inside!"
      when: contents["stdout_lines"] | length > 2
    

    【讨论】:

      【解决方案3】:

      Jeremy Canfield 启发的解决方案,它检查目录是否包含文件:

      - name: Check if d folder is empty before proceeding
        stat:
          path: c/d/*
        register: filesFound
      
      - fail:
           msg: The d folder is not empty. 
        when: filesFound.stat.exists == true
      
      - debug:
          msg: The d folder is empty. Proceeding.
        when: filesFound.stat.exists == false
      

      【讨论】:

        猜你喜欢
        • 2011-11-21
        • 2018-11-20
        • 2018-12-23
        • 1970-01-01
        • 1970-01-01
        • 2015-02-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多