Skip to Content

10 ways to use list in Ansible

In Ansible, a list refers to an ordered collection of items, much like an array or a list in other programming languages. Lists are used to keep related elements grouped together for easier management and operation.

You can define a list in Ansible as follows:

fruits:
  - Apple
  - Banana
  - Orange

In this example, “fruits” is a list that contains three items: “Apple”, “Banana”, and “Orange”. You can use lists in Ansible for various purposes, such as looping over a set of values in a task using the ‘loop’ keyword.

Get Your Free Linux training!

Join our free Linux training and discover the power of open-source technology. Enhance your skills and boost your career! Learn Linux for Free!

List iteration: Use the with_items keyword to iterate over a list in a playbook, for example:

- name: List iteration
  debug:
    var: item
  with_items:
    - item1
    - item2
    - item3

 

This will output the values of the item variable, which will be each element in the list one by one.

List concatenation: Use the + operator to concatenate two or more lists, for example:

- name: List concatenation
  debug:
    var: my_list1 + my_list2
  vars:
    my_list1:
      - item1
      - item2
    my_list2:
      - item3
      - item4

This will output the combined list [item1, item2, item3, item4].

List indexing: Use the square brackets [] to access a specific element in a list by its index, for example:

- name: List indexing
  debug:
    var: my_list[1]
  vars:
    my_list:
      - item1
      - item2
      - item3

This will output the value of the element at index 1 in the list, which is item2.

List length: Use the length filter to get the number of elements in a list, for example:

- name: List length
  debug:
    var: my_list | length
  vars:
    my_list:
      - item1
      - item2
      - item3

This will output the length of the list, which is 3.

List appending: Use the append method to add an element to the end of a list, for example:

- name: List appending
  debug:
    var: my_list
  vars:
    my_list:
      - item1
      - item2
  tasks:
    - name: Append item3 to my_list
      set_fact:
        my_list: "{{ my_list + ['item3'] }}"

This will output the updated list after appending item3 to the end of it, which is [item1, item2, item3].

List merging: Use the combine filter to merge two or more lists into a single list, for example:

- name: List merging
  debug:
    var: my_list1 | combine(my_list2)
  vars:
    my_list1:
      - item1
      - item2
    my_list2:
      - item3
      - item4

This will output the merged list [item1, item2, item3, item4].

List removing: Use the difference filter to remove elements from a list, for example:

- name: List removing
  debug:
    var: my_list1 | difference(my_list2)
  vars:
    my_list1:
      - item1
      - item2
      - item3
    my_list2:
      - item2
      - item3

This will output the list after removing elements that are also present in my_list2, which is [item1].

List checking: Use the in keyword to check if an element is present in a list, for example:

- name: List checking if item2 is in my_list
  debug:
    var: "'item2' in my_list"
  vars:
    my_list:
      - item1
      - item2
      - item3

This will output True as item2 is present in my_list.

List filtering: Use the select filter to filter a list based on a condition, for example:

- name: List filtering
  debug:
    var: my_list | select("equalto", "item2")
  vars:
    my_list:
      - item1
      - item2
      - item3

This will output the elements in my_list that are equal to item2, which is [item2].

List sorting: Use the sort filter to sort a list in ascending or descending order, for example:

- name: List sorting
  debug:
    var: my_list | sort(reverse=true)
  vars:
    my_list:
      - item3
      - item1
      - item2

This will output the sorted list in descending order, which is [item3, item2, item1].

Daniel Lim

Monday 11th of December 2023

cool. I really like these examples.