Ansible Condition


Can you please explain where that item.name come into the piture , whether we have called that variable or how please explain.

@ramkumar, A few comments…

  1. On the left side there is no variable named item and we don’t see with_items or loop. (Maybe it wasn’t captured by the screenshot.)
    a. In this case I would expect to see either with_items or loop referencing the defined variable packages, which is a list of dictionaries (each - starts a new dictionary), like this:
tasks:
- name: Install "{{ item.name}}" on Debian
  apt:
    name: "{{ item.name }}"
    state: present
  with_items: "{{ packages }}"
  1. When you feed a list into with_items or loop, the task is run multiple times, each time using one item from the list and setting the base variable name to item.
  2. On the right side you see this broken out into the equivalent (what actually happens).
    a. The first loop through the task it uses this first list item from packages:
  - name: nginx
    required: True
b. This is a dictionary and is automatically named `item` for this loop, so it actually looks like this:
item:
  name: nginx
  required: True
c. So, when you run through the task, `{{ item.name }}` is `nginx` and `{{ item.required }}` is `True`.
d. Once that task completes it moves to the next item in `packages` and runs through the task again...
  1. Unfortunately this is sort of a bad example because the yum and apt modules now recommend against using with_items and loop, and instead just listing all the packages to install…but then you can’t factor in the required variable in your logic (which isn’t demonstrated here anyway). Here is the recommended way to install multiple packages:
tasks:
- name: Install required packages on Debian
  apt:
    name:
      - nginx
      - mysql
      - apache2
    state: present