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…
- On the left side there is no variable named
item
and we don’t seewith_items
orloop
. (Maybe it wasn’t captured by the screenshot.)
a. In this case I would expect to see eitherwith_items
orloop
referencing the defined variablepackages
, 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 }}"
- When you feed a list into
with_items
orloop
, the task is run multiple times, each time using one item from the list and setting the base variable name toitem
. - 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 frompackages
:
- 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...
- Unfortunately this is sort of a bad example because the
yum
andapt
modules now recommend against usingwith_items
andloop
, and instead just listing all the packages to install…but then you can’t factor in therequired
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