What are the different between 2 ways for loop as below

Could you explain what are the different between 2 ways wrote loop as below?
Thank you so much.

- name: Install a list of packages
  ansible.builtin.yum:
    name:
      - nginx
      - postgresql
      - postgresql-server
    state: present
- name: Install a list of packages
  ansible.builtin.yum:
    name="{{ item }}" 
    state: present
    loop:
        - nginx
        - postgresql
        - postgresql-server

The former executes yum once and will install the 3 items as arguments to the yum binary, resulting in 1 execution of yum.

The latter executes yum {{ item }} and will install the 3 items by sending a single package to yum, 3 times, resulting in 3 executions of yum.

4 Likes
yum install -y nginx postgresql postgresql-server

vs.

yum install -y nginx
yum install -y postgresql 
yum install -y postgresql-server
3 Likes

I want to make something more clear: What you’re showing are not 2 different ways to write a loop.

See the answers from @sirjaren and @kurokobo why the first task isn’t a loop.

3 Likes