Creating a list of directories with ansible with file module is slow

Viewed 1407

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.

1 Answers

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.

Related