BacklogZero®

Ansible in a Minute · Ansible Core ·

Combine list and dictionary variables

Short answer

Join two lists with + and merge two dictionaries with the combine filter.

Subscribe for updates

Get an email whenever we publish new guides and blog posts. Unsubscribe anytime.

By subscribing you agree we may email you about Ansible in a Minute and related Level Up Labs updates. We store your address in the same lead sheet as our contact form.

Request

merge two fruit lists with plus and two profile dicts with combine

Example

A quick heads-up: Every environment is different. Review and test this example before you run it on systems you care about.

YAML
---
- name: Combine list and dictionary variables
  hosts: all
  gather_facts: false
  vars:
    list1:
      - apple
      - banana
    list2:
      - peach
      - plum
    dict1:
      name: Leeroy Jenkins
      age: 25
    dict2:
      location: Galway
      country: Ireland
  tasks:
    - name: Combine list1 and list2
      ansible.builtin.set_fact:
        merged_list: "{{ list1 + list2 }}"

    - name: Combine dict1 and dict2
      ansible.builtin.set_fact:
        merged_dict: "{{ dict1 | ansible.builtin.combine(dict2) }}"

    - name: Print the merged values
      ansible.builtin.debug:
        msg: "{{ merged_list }} / {{ merged_dict.name }} in {{ merged_dict.country }}"

How it works

list1 + list2 creates one list. combine copies keys from dict2 onto dict1. Matching keys from the right-hand dictionary replace the left-hand values.

When to use it

  • Building one package list from two sources
  • Merging a shared profile with a host-specific overlay

Requirements

  • Ansible: >=2.14

Validation

  • Syntax validated
  • Not execution-tested
  • Last verified 2026-09-28

Notes

+ concatenates lists. It does not merge dictionaries. Use combine for mappings. Later keys win. For a recursive merge of nested dictionaries, pass recursive=True to combine.

Reference

Built for every Ansible use case in every organization

BacklogZero can generate this kind of automation in your environment, just like it did here.

Get BacklogZero today →

Related guides

← Back to Ansible in a Minute

Combine list and dictionary variables · Level Up Labs