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

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

问题描述

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

当我使用file模块时,如图所示,这非常慢.创建甚至检查每个目录是否存在大约需要半秒钟,在我遇到的情况下,这很容易总计几分钟.

管道启用.

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

有人问了类似的问题解决方案

一种选择是使用命令创建"构造.

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

幂等性,命令模块和无工具的工具".

在此答案的注释中以及在线程中讨论了命令模块的问题幂等性,并优选了ansible模块的本机幂等性.

"我知道shell和命令,而mkdir -p恰好是幂等的.仍然我更喜欢ansible管理目录状态的方法."

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

命令创建构造是幂等的.仅当 item 不存在时才执行 command .而且,我认为,在类似的情况下,应该最好使用此构造,因为它可以完全满足需要,可以快速且易于理解.这符合最低限度的模块化软件开发规范,这对于可持续发展是必不可少的. >

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天全站免登陆