使用带有文件模块的 ansible 创建目录列表很慢 [英] Creating a list of directories with ansible with file module is slow

查看:22
本文介绍了使用带有文件模块的 ansible 创建目录列表很慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个角色,必须创建一个目录列表.哪些目录最终出现在列表中,取决于在运行时评估的几个条件.

当我使用 file 模块时,如图所示,这很慢.创建甚至检查每个目录的存在大约需要半秒,在我遇到的场景中很容易加起来几分钟.

流水线已启用.

- 名称:创建目录文件:路径:{{ item }}"状态:目录with_items:- "{{ 目录 }}"什么时候:- 目录已定义

有人问过类似的问题 here,但是使用 synchronizeunarchive,正如建议的那样,对于事先不知道的目录似乎很尴尬,因为要同步的目录结构,首先必须在本地主机上的某个地方创建.

是否有其他替代方法可以解决这个问题,我可能错过了?

我知道 shellcommandmkdir -p 恰好是幂等的.我仍然更喜欢一种方法,其中 ansible 管理目录的状态.

解决方案

一个选项是使用命令创建"构造.

- 名称:创建目录命令:mkdir -p "{{ item }}";参数:创建:{{ item }}"循环:{{目录}}"

幂等性、命令模块和工具无策略".

无论是在对此答案的评论中还是在 线程中 讨论命令模块的幂等性问题,优先考虑 ansible 模块的原生幂等性.

<块引用>

"我知道 shell 和 command 并且 mkdir -p 恰好是幂等的.我仍然更喜欢一种方式,其中 ansible 管理目录的状态."

您应该提到使用本机文件模块的这种松散的幂等特性."

命令创建结构是幂等的.command 仅在 item 不存在时执行.此外,在我看来,这种结构在类似情况下应该是首选,因为它完全符合需要、速度快且易于理解.这符合可持续发展所必需的极简、模块化软件开发规范.>

I have a role, that has to create a list of directories. Which directories end up in the list, depends on several conditions that are evaluated at runtime.

When I use the file module, like shown, this is very slow. Creating and even checking for the existence of every directory takes about half a second, which easily sums up to minutes in the scenarios I encounter.

Pipelining is enabled.

- name: Create directories
  file:
    path:   "{{ item }}"
    state:  directory
  with_items:
    - "{{ dirs }}"
  when:
    - dirs is defined

A similar question has been asked here, however using synchronize or unarchive, as suggested there, seems very awkward for directories not known in advance, as the directory structure to be synchronized, first has to be created on the local host somewhere.

Are there other alternatives to solve this, I might have missed?

EDIT: I am aware of shell and command and mkdir -p happens to be idempotent. Still I would prefer a way, where ansible manages the state of the directories.

解决方案

An option would be to use the "command creates" construct.

- name: Create directories
  command: mkdir -p "{{ item }}"
  args:
    creates: "{{ item }}"
  loop: "{{ dirs }}"

Idempotency, command module and "tools no policy".

Both in the comments to this answer and in the thread mentioned in the question idempotency of the command module is discussed and native idempotency of ansible modules preferred.

" I am aware of shell and command and mkdir -p happens to be idempotent. Still I would prefer a way, where ansible manages the state of the directories."

"You should mention that this loose idempotent feature of using native file module."

The command creates construct is idempotent. The command is executed only if the item does not exist. Moreover, in my opinion, this construct shall be preferred in similar situations because it does exactly what is needed, does it fast and is easy to understand. This complies with the norms of minimalist, modular software development which are necessary for sustainable development.

这篇关于使用带有文件模块的 ansible 创建目录列表很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆