How to Use List Variables in Ansible Playbooks
Ansible is a powerful automation tool that allows users to manage and configure systems at scale. One of the core features of Ansible is the use of variables to handle differences between systems, environments, or configurations. In this post, we’ll explore how to work with list variables in Ansible playbooks, offering practical examples to help you master this useful concept.

What Are List Variables in Ansible?
List variables in Ansible are a way to store multiple values under a single variable name. These values can be organized as an itemized list or within square brackets, separated by commas. Using list variables enables you to manage a collection of related items efficiently, making it easier to scale your automation tasks.
Defining List Variables in Ansible Playbooks
To start using list variables in Ansible, you can define them in a playbook, inventory file, reusable roles, or directly from the command line. Below, we’ll walk through an example where we define a list variable in a playbook and reference it to perform automation tasks.
Example 1: Basic List Variable Usage
Here’s a simple example where we define a list variable in an Ansible playbook and then reference it within the playbook.
# cat listvariable.yaml
---
- hosts: all
vars:
teams:
- csk
- srh
- mi
- rcb
tasks:
- name: Print list variables
debug:
msg: "{{ teams[0] }}"
In this example, the list variable teams contains the names of four cricket teams. The playbook prints the first item in the list (csk). To run this playbook, execute the following command:
# ansible-playbook -i hosts listvariable.yaml
The output will display the first item from the list:
TASK [Print list variables] ***********************************************************************
ok: [192.168.1.2] => {
"msg": "csk"
}
Example 2: Looping Through List Variables
You can also loop through all the items in a list variable using Ansible’s with_items directive. This is particularly useful when you need to perform the same task for each item in the list.
# cat listvar.yaml
---
- hosts: all
vars:
teams:
- csk
- srh
- mi
- rcb
tasks:
- name: Print list variables
debug:
msg: "{{ item }}"
with_items:
- "{{ teams }}"
When running this playbook, Ansible will loop through each item in the teams list and print them one by one:
# ansible-playbook -i hosts listvar.yaml
Output:
TASK [Print list variables] ***********************************************************************
ok: [192.168.1.2] => (item=csk) => {
"msg": "csk"
}
ok: [192.168.1.2] => (item=srh) => {
"msg": "srh"
}
ok: [192.168.1.2] => (item=mi) => {
"msg": "mi"
}
ok: [192.168.1.2] => (item=rcb) => {
"msg": "rcb"
}
This example demonstrates the power of list variables in Ansible, making it easy to handle multiple values dynamically within your automation tasks.
Best Practices for Using List Variables in Ansible
- Use Clear Names: Ensure that your list variable names are descriptive and reflect the data they store. For example, instead of using a generic name like
list, use something specific liketeams. - Maintain Consistency: When working with list variables, make sure the data type of each item remains consistent. This helps avoid errors during playbook execution.
- Loop Efficiently: Use the
with_itemsdirective or the newerloopfunctionality to iterate through lists. This ensures tasks are applied consistently to all items in the list.
Optimizing Your Automation Strategy with ZippyOPS
While managing list variables is a crucial aspect of Ansible playbooks, optimizing your overall automation and cloud infrastructure is just as important. ZippyOPS provides consulting, implementation, and managed services for DevOps, DevSecOps, DataOps, Cloud, Automated Ops, AIOps, MLOps, Microservices, and more.
With ZippyOPS, you can streamline your deployment processes, enhance security with DevSecOps best practices, and automate infrastructure scaling in the cloud. Whether you’re automating Ansible playbooks or looking for end-to-end infrastructure management, ZippyOPS has the tools and expertise you need.
For more information on how ZippyOPS can enhance your infrastructure and automation strategy, check out our services and solutions.
Conclusion
List variables in Ansible offer a flexible and efficient way to manage multiple values within your automation tasks. By incorporating best practices for defining and using list variables, you can simplify your playbooks and ensure smoother execution. Additionally, leveraging ZippyOPS’ expert services can take your automation to the next level, enhancing scalability, security, and performance across your entire infrastructure.
For more insights into optimizing your automation with Ansible and cloud solutions, visit our products or explore our YouTube channel at ZippyOPS YouTube.
Ready to optimize your automation strategy? Get in touch with us at sales@zippyops.com.



