Skip to Content

Ansible Lists 101: A Beginner’s Guide to Managing and Manipulating Data

what is list in Ansible

In Ansible, a list is a data structure that holds an ordered collection of items. Lists in Ansible are defined using YAML syntax, which is a human-readable data serialization format.

Lists in Ansible can contain various types of data, such as strings, numbers, booleans, and even other complex data structures like dictionaries or other lists. Lists are commonly used to store and manipulate sets of values, such as configuration parameters, file paths, or groups of hosts.

Here’s an example of a list in Ansible:

my_list:
  - item1
  - item2
  - item3

In this example, a list named my_list is defined, and it contains three string items: item1, item2, and item3.

Items in a list are separated by a hyphen (-) followed by a space, and the list is enclosed in square brackets ([]).

Lists in Ansible can be used in various ways, such as passing them as arguments to modules, using them in loops or conditionals, and storing them in variables for later use. They provide a flexible way to organize and manipulate data in Ansible playbooks and roles.

Create a list in Ansible

In Ansible, you can create a list using the YAML syntax. Here’s an example of how you can create a list.

- name: Example Playbook
  hosts: localhost
  gather_facts: no
  tasks:
    - name: Create a List
      vars:
        my_list:
          - item1
          - item2
          - item3
      debug:
        var: my_list

In this example, a list named my_list is defined using the vars keyword, and it contains three items: item1, item2, and item3. The debug module is then used to display the value of my_list using the var parameter.

You can also create a list dynamically using other Ansible modules, such as add_host, set_fact, or by referencing variables from inventory or other sources. Here’s an example using the set_fact module:

- name: Example Playbook
  hosts: localhost
  gather_facts: no
  tasks:
    - name: Create a List
      set_fact:
        my_list: 
          - "{{ var1 }}"
          - "{{ var2 }}"
          - "{{ var3 }}"
      vars:
        var1: item1
        var2: item2
        var3: item3
    - name: Display List
      debug:
        var: my_list

In this example, the set_fact module is used to create a list my_list using the values of three variables var1, var2, and var3. The debug module is then used to display the value of my_list.

How to add items to list in Ansible

In Ansible, you can add items to a list using the append method of the list filter. The list filter provides a way to manipulate lists in Ansible by allowing you to perform various operations, including adding items to an existing list. Here’s an example:

- name: Example Playbook
  hosts: localhost
  gather_facts: no
  vars:
    my_list:
      - item1
      - item2
  tasks:
    - name: Display Original List
      debug:
        var: my_list

    - name: Add Item to List
      set_fact:
        my_list: "{{ my_list + ['item3'] }}"

    - name: Display Updated List
      debug:
        var: my_list

In this example, a list named my_list is defined with two initial items, item1 and item2. The debug module is then used to display the original list.

The set_fact module is then used to add an item, item3, to the my_list list using the + operator, which concatenates the existing list with a new list containing the item to be added. The updated list is stored back into the my_list variable.

Finally, the debug module is used again to display the updated list, which should now include the added item item3.

Note that in Ansible, variables are immutable, meaning their values cannot be changed once set. Therefore, when adding an item to a list, you need to store the updated list in a new variable or overwrite the original variable with the updated list using the set_fact module or similar approaches.

how to get the items from list in Ansible

In Ansible, you can access the items in a list using indexing or loops. Here are some examples:

Using Indexing:

- name: Example Playbook
  hosts: localhost
  gather_facts: no
  vars:
    my_list:
      - item1
      - item2
      - item3
  tasks:
    - name: Access List Items by Index
      debug:
        var: my_list[0]  # Accesses the item at index 0 (first item)
    - name: Access List Items by Range
      debug:
        var: my_list[1:3]  # Accesses items at index 1 and 2 (second and third items)

In this example, a list my_list is defined with three items: item1, item2, and item3. The debug module is then used to access and display items from the list using indexing. For example, my_list[0] accesses the item at index 0 (the first item) and my_list[1:3] accesses items at index 1 and 2 (the second and third items) using a range.

Using Loops:

- name: Example Playbook
  hosts: localhost
  gather_facts: no
  vars:
    my_list:
      - item1
      - item2
      - item3
  tasks:
    - name: Access List Items Using Loop
      debug:
        var: item
      loop: "{{ my_list }}"

In this example, a list my_list is defined with three items. The debug module is then used to loop through the list using a loop construct and access each item in the list. The loop iterates over my_list, and the current item is accessed using the loop variable item, which contains the value of the current item in each iteration.

These are two common ways to access items from a list in Ansible. You can choose the method that best fits your use case based on your specific requirements and the context of your playbook.

how to get the first item in ansible list

In Ansible, you can access the first item in a list by using the 0 index. Here’s an example playbook that demonstrates how to do this:

- name: Accessing the first item in a list
  hosts: localhost
  gather_facts: no
  tasks:
    - name: Define a list
      set_fact:
        my_list: ["item1", "item2", "item3"]

    - name: Access the first item in the list
      debug:
        var: my_list[0]
        

In this example, the set_fact module is used to define a list called my_list with three items. Then, the debug module is used to print out the first item in the list using the [0] index. The output will be the value of the first item in the list, in this case, “item1”. Note that the index in Ansible lists is zero-based, meaning that the first item is at index 0, the second item is at index 1, and so on.

how to get the length of a list in ansible

In Ansible, you can get the length of a list using the length filter. The length filter calculates the number of items in a list and returns the result as an integer.

Here’s an example playbook that demonstrates how to use the length filter to get the length of a list:


- name: Get the length of a list
  hosts: localhost
  gather_facts: no
  tasks:
    - name: Define a list
      set_fact:
        my_list: ["item1", "item2", "item3"]

    - name: Get the length of the list
      debug:
        var: my_list | length

In this example, the set_fact module is used to define a list called my_list with three items. Then, the debug module is used to print out the length of the list using the length filter with the syntax my_list | length. The output will be the length of the list, in this case, 3.

how to use filters in Ansible list

Filters in Ansible provide a way to manipulate data, including lists, by applying various operations and transformations to the data. Here are some examples of how you can use filters with lists in Ansible:

map filter: The map filter allows you to apply a filter or transformation to each item in a list and return a new list with the results. Here’s an example:

- name: Example Playbook
  hosts: localhost
  gather_facts: no
  vars:
    my_list:
      - item1
      - item2
      - item3
  tasks:
    - name: Use map filter to convert items to uppercase
      debug:
        var: my_list | map('upper') | list

In this example, the map filter is used to apply the upper filter to each item in the my_list list, which converts the items to uppercase. The list filter is then used to convert the result back to a list.

select filter: The select filter allows you to filter a list based on a condition and return a new list with only the items that meet the condition. Here’s an example:

- name: Example Playbook
  hosts: localhost
  gather_facts: no
  vars:
    my_list:
      - item1
      - item2
      - item3
  tasks:
    - name: Use select filter to filter items containing "item"
      debug:
        var: my_list | select("search", "item") | list

In this example, the select filter is used to filter the my_list list and return only the items that contain the string “item” using the “search” test. The list filter is then used to convert the result back to a list.

union filter: The union filter allows you to merge multiple lists into a single list. Here’s an example:

- name: Example Playbook
  hosts: localhost
  gather_facts: no
  vars:
    my_list1:
      - item1
      - item2
    my_list2:
      - item2
      - item3
  tasks:
    - name: Use union filter to merge two lists
      debug:
        var: my_list1 | union(my_list2) | list

In this example, the union filter is used to merge the my_list1 and my_list2 lists into a single list, eliminating duplicates. The list filter is then used to convert the result back to a list.

These are just a few examples of how you can use filters with lists in Ansible. Ansible provides a wide range of built-in filters that you can use to manipulate data in your playbooks and roles. You can refer to the Ansible documentation for a complete list of filters and their usage.

henry

Sunday 2nd of July 2023

Thanks a lot for the detailed info. It saves me a lot of time.