Home > Enterprise >  Ansible - Run a task once over a list
Ansible - Run a task once over a list

Time:09-29

I'm trying to write a task that should be executed once over a list, for example:

- name: Create something that has an attribute with more than one values
  some_module:
    some_attribute: "{{ item }}"
  run_once: true
  loop:
    - a
    - b

But the task written like this will be executed two time, like

- name: Create something that has an attribute with more than one values
  some_module:
    some_attribute: "a"
  run_once: true

- name: Create something that has an attribute with more than one values
  some_module:
    some_attribute: "b"
  run_once: true

What I like to obtain is this:

- name: Create something that has an attribute with more than one values
  some_module:
    some_attribute:
      - a
      - b
  run_once: true

where "some_attribute" is also a list.

How can I achieve this?

Thanks

CodePudding user response:

But the task written like this will be executed two time

Right, that is expected behavior and working as it was designed.

How can I achieve this? ... where some_attribute is also a list.

You can't, unless the module which you are like to use is supporting list as some_attribute. A good example for this is the yum module – Manages packages with the yum package manager and the Notes

When used with a loop: each package will be processed individually, it is much more efficient to pass the list directly to the name option.

You'll find some more examples and test playbooks with measurements under the my Similar Q&A links. They'll show the behavior in more detail and explain why this is.

Similar Q&A


... unless you are not providing more background information, details and describe what you try to achieve, at least the module name, a better answer can't be given.

  • Related