Ansible: Appending items to a List

From Wiki
Revision as of 20:37, 13 March 2024 by Ebasso (talk | contribs) (Created page with "Simple task to append items to a list <nowiki> - name: Append items to a list hosts: localhost gather_facts: false vars: mylist: [] tasks: - name: Append list with additional mapping ansible.builtin.set_fact: mylist: "{{ mylist + [{'name': 'blabla', 'mode': 1}] }}" - name: Append list with additional mapping ansible.builtin.set_fact: mylist: "{{ mylist + [{'name': 'yoyoyo', 'mode': 2}] }}" - name: Print the updated...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Simple task to append items to a list

- name: Append items to a list
  hosts: localhost
  gather_facts: false
  vars:
    mylist: []
  tasks:
    - name: Append list with additional mapping
      ansible.builtin.set_fact:
        mylist: "{{ mylist + [{'name': 'blabla', 'mode': 1}] }}"

    - name: Append list with additional mapping
      ansible.builtin.set_fact:
        mylist: "{{ mylist + [{'name': 'yoyoyo', 'mode': 2}] }}"

    - name: Print the updated list
      ansible.builtin.debug:
        msg: "{{ item.name }}"
      with_items:
        - "{{ mylist }}"

See also